1 4 package demo.sharededitor.models; 5 6 import java.awt.BasicStroke ; 7 import java.awt.Color ; 8 import java.awt.Graphics ; 9 import java.awt.Graphics2D ; 10 import java.awt.Image ; 11 import java.awt.Paint ; 12 import java.awt.Rectangle ; 13 import java.awt.Shape ; 14 import java.awt.Stroke ; 15 import java.awt.TexturePaint ; 16 import java.awt.image.BufferedImage ; 17 import java.io.ByteArrayInputStream ; 18 import java.io.ByteArrayOutputStream ; 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import javax.swing.ImageIcon ; 27 28 import demo.sharededitor.events.IListListener; 29 import demo.sharededitor.ui.IFillStyleConsts; 30 import demo.sharededitor.ui.ITexturable; 31 32 public abstract class BaseObject implements IFillStyleConsts 33 { 34 private List listeners; 35 36 public void addListener(IListListener listListener) 37 { 38 if (listeners == null) 39 listeners = Collections.synchronizedList(new ArrayList ()); 40 41 synchronized(listeners) { 42 if (!listeners.contains(listListener)) 43 listeners.add(listListener); 44 } 45 } 46 47 public void removeListener(IListListener listListener) 48 { 49 if ((listeners != null) && (listeners.contains(listListener))) 50 synchronized(listeners) { 51 listeners.remove(listListener); 52 } 53 } 54 55 protected void notifyListeners(Object obj) 56 { 57 if (listeners == null) return; 58 59 synchronized(listeners) { 60 Iterator i = listeners.iterator(); 61 while (i.hasNext()) 62 { 63 IListListener listListener = (IListListener) i.next(); 64 listListener.changed(this, this); 65 } 66 } 67 } 68 69 private boolean isReady() 70 { 71 return (getShape() != null); 72 } 73 74 public boolean isAt(int x, int y) 75 { 76 if (!isReady()) return false; 77 78 Shape shape = getShape(); 79 if (shape.contains(x, y)) return true; 80 81 Shape [] anchors = getAnchors(); 82 for (int i = 0; i < anchors.length; i++) 83 { 84 if (anchors[i].contains(x, y)) return true; 85 } 86 return false; 87 } 88 89 private int grabbedAnchor; 90 91 public synchronized void setGrabbedAnchorAt(int x, int y) 92 { 93 Shape [] anchors = getAnchors(); 94 for (int i = 0; i < anchors.length; i++) 95 { 96 if (anchors[i].contains(x, y)) 97 { 98 grabbedAnchor = i; 99 return; 100 } 101 } 102 grabbedAnchor = -1; 103 } 104 105 protected int grabbedAnchor() 106 { 107 return grabbedAnchor; 108 } 109 110 public boolean isAnchorGrabbed() 111 { 112 return (grabbedAnchor >= 0) && (grabbedAnchor < getAnchors().length); 113 } 114 115 public abstract void resize(int x, int y); 116 117 public abstract void move(int dx, int dy); 118 119 protected Shape [] getAnchors() 120 { 121 return new Shape [0]; 122 } 123 124 protected abstract Shape getShape(); 125 126 public void draw(Graphics2D g, boolean showAnchors) 127 { 128 Shape shape = getShape(); 129 Rectangle bounds = shape.getBounds(); 130 g.setColor(this.background); 131 if (FILLSTYLE_SOLID == this.fillstyle) g.fill(shape); 132 133 if ((FILLSTYLE_TEXTURED == this.fillstyle) 134 && (this instanceof ITexturable) && isTextured() 135 && (bounds.width > 0) && (bounds.height > 0)) 136 { 137 ImageIcon image = new ImageIcon (getTexture()); 138 BufferedImage texture = new BufferedImage (bounds.width, bounds.height, 139 BufferedImage.TYPE_INT_RGB); 140 Graphics tg = texture.getGraphics(); 141 tg 142 .drawImage(image.getImage(), 0, 0, bounds.width, bounds.height, 143 null); 144 Paint paint = new TexturePaint (texture, bounds); 145 g.setPaint(paint); 146 g.fill(shape); 147 } 148 149 g.setStroke(this.stroke); 150 g.setColor(this.foreground); 151 152 g.draw(shape); 153 154 Shape [] anchors = getAnchors(); 155 for (int i = 0; showAnchors && i < anchors.length; i++) 156 { 157 g.fill(anchors[i]); 158 g.setStroke(new BasicStroke (1)); 159 g.draw(anchors[i]); 160 } 161 } 162 163 private int fillstyle; 164 165 public synchronized void setFillStyle(int fillstyle) 166 { 167 this.fillstyle = fillstyle; 168 notifyListeners(this); 169 } 170 171 private Color foreground; 172 173 public synchronized void setForeground(Color color) 174 { 175 this.foreground = color; 176 notifyListeners(this); 177 } 178 179 private Color background; 180 181 public synchronized void setBackground(Color color) 182 { 183 this.background = color; 184 notifyListeners(this); 185 } 186 187 private Stroke stroke = new BasicStroke (); 188 189 public synchronized void setStroke(Stroke stroke) 190 { 191 this.stroke = stroke; 192 notifyListeners(this); 193 } 194 195 public static final BaseObject createObject(String name) 196 { 197 try 198 { 199 Class klass = Class 200 .forName("demo.sharededitor.models." + name); 201 return (BaseObject) klass.newInstance(); 202 } 203 catch (Exception ex) 204 { 205 throw new InternalError (ex.getMessage()); 206 } 207 } 208 209 private byte[] texture = null; 210 211 protected synchronized void setTexture(Image image) 212 { 213 try 214 { 215 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 216 ObjectOutputStream oos = new ObjectOutputStream (bos); 217 oos.writeObject(new ImageIcon (image)); 218 oos.flush(); 219 oos.close(); 220 texture = bos.toByteArray(); 221 } 222 catch (Exception ex) 223 { 224 throw new InternalError ("Unable to convert ImageIcon to byte[]"); 225 } 226 } 227 228 protected Image getTexture() 229 { 230 try 231 { 232 ByteArrayInputStream bis = new ByteArrayInputStream (texture); 233 ObjectInputStream ois = new ObjectInputStream (bis); 234 ImageIcon image = (ImageIcon ) ois.readObject(); 235 return image.getImage(); 236 } 237 catch (Exception ex) 238 { 239 throw new InternalError ("Unable to convert byte[] to Image"); 240 } 241 } 242 243 protected boolean isTextured() 244 { 245 return (texture != null); 246 } 247 248 public boolean isTransient() 249 { 250 return false; 251 } 252 253 public synchronized void selectAction(boolean flag) 254 { } 255 256 public synchronized void alternateSelectAction(boolean flag) 257 { } 258 } | Popular Tags |