1 36 37 40 41 package java2d.demos.Transforms; 42 43 import java.awt.*; 44 import java.awt.event.*; 45 import java.awt.geom.*; 46 import java.awt.image.*; 47 import javax.swing.*; 48 import javax.swing.event.*; 49 import javax.swing.border.*; 50 import java.util.Vector ; 51 import java2d.AnimatingControlsSurface; 52 import java2d.CustomControls; 53 import javax.swing.plaf.metal.MetalBorders.ButtonBorder; 54 55 import static java.awt.Color .*; 56 import static java.awt.Font .*; 57 58 62 public class TransformAnim extends AnimatingControlsSurface { 63 64 private static TexturePaint texture; 65 static { 66 BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); 67 Graphics2D gi = bi.createGraphics(); 68 gi.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 69 RenderingHints.VALUE_ANTIALIAS_ON); 70 gi.setColor(RED); 71 gi.fillOval(0,0,9,9); 72 texture = new TexturePaint(bi,new Rectangle(0,0,10,10)); 73 } 74 75 private static BasicStroke bs = new BasicStroke(6); 76 private static Font fonts[] = { 77 new Font ("Times New Roman", PLAIN, 48), 78 new Font ("serif", BOLD|ITALIC, 24), 79 new Font ("Courier", BOLD, 36), 80 new Font ("Arial", BOLD|ITALIC, 64), 81 new Font ("Helvetica", PLAIN, 52)}; 82 private static String strings[] = { 83 "Transformation", "Rotate", "Translate", "Shear", "Scale" }; 84 private static String imgs[] = { "duke.gif" }; 85 private static Paint paints[] = { 86 RED, BLUE, texture, GREEN, MAGENTA, ORANGE, PINK, CYAN, 87 new Color (0, 255, 0, 128), new Color (0, 0, 255, 128), 88 YELLOW, LIGHT_GRAY, WHITE}; 89 private Vector <ObjData> objDatas = new Vector <ObjData>(13); 90 private int numShapes, numStrings, numImages; 91 protected boolean doRotate = true; 92 protected boolean doTranslate = true; 93 protected boolean doScale = true; 94 protected boolean doShear; 95 96 97 public TransformAnim() { 98 setBackground(BLACK); 99 setStrings(1); 100 setImages(2); 101 setShapes(10); 102 setControls(new Component[] { new DemoControls(this) }); 103 setConstraints(new String [] { BorderLayout.EAST }); 104 } 105 106 107 public void setImages(int num) { 108 if (num < numImages) { 109 Vector <ObjData> v = new Vector <ObjData>(objDatas.size()); 110 for (ObjData objData : objDatas) { 111 if (objData.object instanceof Image) { 112 v.addElement(objData); 113 } 114 } 115 objDatas.removeAll(v); 116 v.setSize(num); 117 objDatas.addAll(v); 118 } else { 119 Dimension d = getSize(); 120 for (int i = numImages; i < num; i++) { 121 Object obj = getImage(imgs[i % imgs.length]); 122 ObjData objData = new ObjData(obj, BLACK); 123 objData.reset(d.width, d.height); 124 objDatas.addElement(objData); 125 } 126 } 127 numImages = num; 128 } 129 130 131 public void setStrings(int num) { 132 if (num < numStrings) { 133 Vector <ObjData> v = new Vector <ObjData>(objDatas.size()); 134 for (ObjData objData : objDatas) { 135 if (objData.object instanceof TextData) { 136 v.addElement(objData); 137 } 138 } 139 objDatas.removeAll(v); 140 v.setSize(num); 141 objDatas.addAll(v); 142 } else { 143 Dimension d = getSize(); 144 for (int i = numStrings; i < num; i++) { 145 int j = i % fonts.length; 146 int k = i % strings.length; 147 Object obj = new TextData(strings[k], fonts[j]); 148 ObjData objData = new ObjData(obj, paints[i%paints.length]); 149 objData.reset(d.width, d.height); 150 objDatas.addElement(objData); 151 } 152 } 153 numStrings = num; 154 } 155 156 157 public void setShapes(int num) { 158 if (num < numShapes) { 159 Vector <ObjData> v = new Vector <ObjData>(objDatas.size()); 160 for (ObjData objData : objDatas) { 161 if (objData.object instanceof Shape) { 162 v.addElement(objData); 163 } 164 } 165 objDatas.removeAll(v); 166 v.setSize(num); 167 objDatas.addAll(v); 168 } else { 169 Dimension d = getSize(); 170 for (int i = numShapes; i < num; i++) { 171 Object obj = null; 172 switch (i % 7) { 173 case 0 : obj = new GeneralPath(); break; 174 case 1 : obj = new Rectangle2D.Double(); break; 175 case 2 : obj = new Ellipse2D.Double(); break; 176 case 3 : obj = new Arc2D.Double(); break; 177 case 4 : obj = new RoundRectangle2D.Double(); break; 178 case 5 : obj = new CubicCurve2D.Double(); break; 179 case 6 : obj = new QuadCurve2D.Double(); break; 180 } 181 ObjData objData = new ObjData(obj, paints[i%paints.length]); 182 objData.reset(d.width, d.height); 183 objDatas.addElement(objData); 184 } 185 } 186 numShapes = num; 187 } 188 189 190 public void reset(int w, int h) { 191 for (ObjData objData : objDatas) { 192 objData.reset(w, h); 193 } 194 } 195 196 197 public void step(int w, int h) { 198 for (ObjData objData : objDatas) { 199 objData.step(w, h, this); 200 } 201 } 202 203 204 public void render(int w, int h, Graphics2D g2) { 205 for (ObjData objData : objDatas) { 206 g2.setTransform(objData.at); 207 g2.setPaint(objData.paint); 208 if (objData.object instanceof Image) { 209 g2.drawImage((Image) objData.object, 0, 0, this); 210 } else if (objData.object instanceof TextData) { 211 g2.setFont(((TextData) objData.object).font); 212 g2.drawString(((TextData) objData.object).string, 0, 0); 213 } else if (objData.object instanceof QuadCurve2D 214 || objData.object instanceof CubicCurve2D) 215 { 216 g2.setStroke(bs); 217 g2.draw((Shape) objData.object); 218 } else if (objData.object instanceof Shape) { 219 g2.fill((Shape) objData.object); 220 } 221 } 222 } 223 224 225 public static void main(String argv[]) { 226 createDemoFrame(new TransformAnim()); 227 } 228 229 230 static class TextData extends Object { 231 232 public String string; 233 public Font font; 234 235 public TextData(String str, Font font) { 236 string = str; 237 this.font = font; 238 } 239 } 240 241 242 static class ObjData extends Object { 243 Object object; 244 Paint paint; 245 static final int UP = 0; 246 static final int DOWN = 1; 247 double x, y; 248 double ix=5, iy=3; 249 int rotate; 250 double scale, shear; 251 int scaleDirection, shearDirection; 252 AffineTransform at = new AffineTransform(); 253 254 255 public ObjData(Object object, Paint paint) { 256 this.object = object; 257 this.paint = paint; 258 rotate = (int)(Math.random() * 360); 259 scale = Math.random() * 1.5; 260 scaleDirection = Math.random() > 0.5 ? UP : DOWN; 261 shear = Math.random() * 0.5; 262 shearDirection = Math.random() > 0.5 ? UP : DOWN; 263 } 264 265 266 public void reset(int w, int h) { 267 x = Math.random()*w; 268 y = Math.random()*h; 269 double ww = 20 + Math.random()*((w == 0 ? 400 : w)/4); 270 double hh = 20 + Math.random()*((h == 0 ? 300 : h)/4); 271 if (object instanceof Ellipse2D) { 272 ((Ellipse2D) object).setFrame(0, 0, ww, hh); 273 } else if (object instanceof Rectangle2D) { 274 ((Rectangle2D) object).setRect(0, 0, ww, ww); 275 } else if (object instanceof RoundRectangle2D) { 276 ((RoundRectangle2D) object).setRoundRect(0, 0, hh, hh, 20, 20); 277 } else if (object instanceof Arc2D) { 278 ((Arc2D) object).setArc(0, 0, hh, hh, 45, 270, Arc2D.PIE); 279 } else if (object instanceof QuadCurve2D) { 280 ((QuadCurve2D) object).setCurve(0, 0, w*.2, h*.4, w*.4, 0); 281 } else if (object instanceof CubicCurve2D) { 282 ((CubicCurve2D) object).setCurve(0,0,30,-60,60,60,90,0); 283 } else if (object instanceof GeneralPath) { 284 GeneralPath p = new GeneralPath(); 285 float size = (float) ww; 286 p.moveTo(- size / 2.0f, - size / 8.0f); 287 p.lineTo(+ size / 2.0f, - size / 8.0f); 288 p.lineTo(- size / 4.0f, + size / 2.0f); 289 p.lineTo(+ 0.0f, - size / 2.0f); 290 p.lineTo(+ size / 4.0f, + size / 2.0f); 291 p.closePath(); 292 object = p; 293 } 294 } 295 296 297 public void step(int w, int h, TransformAnim demo) { 298 at.setToIdentity(); 299 if (demo.doRotate) { 300 if ((rotate+=5) == 360) { 301 rotate = 0; 302 } 303 at.rotate(Math.toRadians(rotate), x, y); 304 } 305 at.translate(x, y); 306 if (demo.doTranslate) { 307 x += ix; 308 y += iy; 309 if (x > w) { 310 x = w - 1; 311 ix = Math.random() * -w/32 - 1; 312 } 313 if (x < 0) { 314 x = 2; 315 ix = Math.random() * w/32 + 1; 316 } 317 if (y > h ) { 318 y = h - 2; 319 iy = Math.random() * -h/32 - 1; 320 } 321 if (y < 0) { 322 y = 2; 323 iy = Math.random() * h/32 + 1; 324 } 325 } 326 if (demo.doScale && scaleDirection == UP) { 327 if ((scale += 0.05) > 1.5) { 328 scaleDirection = DOWN; 329 } 330 } else if (demo.doScale && scaleDirection == DOWN) { 331 if ((scale -= .05) < 0.5) { 332 scaleDirection = UP; 333 } 334 } 335 if (demo.doScale) { 336 at.scale(scale, scale); 337 } 338 if (demo.doShear && shearDirection == UP) { 339 if ((shear += 0.05) > 0.5) { 340 shearDirection = DOWN; 341 } 342 } else if (demo.doShear && shearDirection == DOWN) { 343 if ((shear -= .05) < -0.5) { 344 shearDirection = UP; 345 } 346 } 347 if (demo.doShear) { 348 at.shear(shear, shear); 349 } 350 } 351 } 353 354 355 static class DemoControls extends CustomControls implements ActionListener, ChangeListener { 356 357 TransformAnim demo; 358 JSlider shapeSlider, stringSlider, imageSlider; 359 Font font = new Font ("serif", Font.BOLD, 10); 360 JToolBar toolbar; 361 ButtonBorder buttonBorder = new ButtonBorder(); 362 363 public DemoControls(TransformAnim demo) { 364 super(demo.name); 365 this.demo = demo; 366 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 367 add(Box.createVerticalStrut(5)); 368 369 JToolBar bar = new JToolBar(JToolBar.VERTICAL); 370 bar.setFloatable(false); 371 shapeSlider = new JSlider(JSlider.HORIZONTAL,0,20,demo.numShapes); 372 shapeSlider.addChangeListener(this); 373 TitledBorder tb = new TitledBorder(new EtchedBorder()); 374 tb.setTitleFont(font); 375 tb.setTitle(String.valueOf(demo.numShapes) + " Shapes"); 376 shapeSlider.setBorder(tb); 377 shapeSlider.setOpaque(true); 378 shapeSlider.setPreferredSize(new Dimension(80,44)); 379 bar.add(shapeSlider); 380 bar.addSeparator(); 381 382 stringSlider = new JSlider(JSlider.HORIZONTAL,0,10,demo.numStrings); 383 stringSlider.addChangeListener(this); 384 tb = new TitledBorder(new EtchedBorder()); 385 tb.setTitleFont(font); 386 tb.setTitle(String.valueOf(demo.numStrings) + " Strings"); 387 stringSlider.setBorder(tb); 388 stringSlider.setOpaque(true); 389 stringSlider.setPreferredSize(new Dimension(80,44)); 390 bar.add(stringSlider); 391 bar.addSeparator(); 392 393 imageSlider = new JSlider(JSlider.HORIZONTAL,0,10,demo.numImages); 394 imageSlider.addChangeListener(this); 395 tb = new TitledBorder(new EtchedBorder()); 396 tb.setTitleFont(font); 397 tb.setTitle(String.valueOf(demo.numImages) + " Images"); 398 imageSlider.setBorder(tb); 399 imageSlider.setOpaque(true); 400 imageSlider.setPreferredSize(new Dimension(80,44)); 401 bar.add(imageSlider); 402 bar.addSeparator(); 403 add(bar); 404 405 toolbar = new JToolBar(); 406 toolbar.setFloatable(false); 407 addButton("T", "translate", demo.doTranslate); 408 addButton("R", "rotate", demo.doRotate); 409 addButton("SC", "scale", demo.doScale); 410 addButton("SH", "shear", demo.doShear); 411 add(toolbar); 412 } 413 414 415 public void addButton(String s, String tt, boolean state) { 416 JToggleButton b = (JToggleButton) toolbar.add(new JToggleButton(s)); 417 b.setFont(font); 418 b.setSelected(state); 419 b.setToolTipText(tt); 420 b.setFocusPainted(false); 421 b.setBorder(buttonBorder); 422 b.addActionListener(this); 423 } 424 425 426 public void actionPerformed(ActionEvent e) { 427 JToggleButton b = (JToggleButton) e.getSource(); 428 if (b.getText().equals("T")) { 429 demo.doTranslate = b.isSelected(); 430 } else if (b.getText().equals("R")) { 431 demo.doRotate = b.isSelected(); 432 } else if (b.getText().equals("SC")) { 433 demo.doScale = b.isSelected(); 434 } else if (b.getText().equals("SH")) { 435 demo.doShear = b.isSelected(); 436 } 437 if (demo.animating.thread == null) { 438 demo.repaint(); 439 } 440 } 441 442 443 public void stateChanged(ChangeEvent e) { 444 JSlider slider = (JSlider) e.getSource(); 445 int value = slider.getValue(); 446 TitledBorder tb = (TitledBorder) slider.getBorder(); 447 if (slider.equals(shapeSlider)) { 448 tb.setTitle(String.valueOf(value) + " Shapes"); 449 demo.setShapes(value); 450 } else if (slider.equals(stringSlider)) { 451 tb.setTitle(String.valueOf(value) + " Strings"); 452 demo.setStrings(value); 453 } else if (slider.equals(imageSlider)) { 454 tb.setTitle(String.valueOf(value) + " Images"); 455 demo.setImages(value); 456 } 457 if (demo.animating.thread == null) { 458 demo.repaint(); 459 } 460 slider.repaint(); 461 } 462 463 464 public Dimension getPreferredSize() { 465 return new Dimension(80,38); 466 } 467 468 469 public void run() { 470 Thread me = Thread.currentThread(); 471 while (thread == me) { 472 for (int i = 1; i < toolbar.getComponentCount(); i++) { 473 try { 474 thread.sleep(4444); 475 } catch (InterruptedException e) { return; } 476 ((AbstractButton) toolbar.getComponentAtIndex(i)).doClick(); 477 } 478 } 479 thread = null; 480 } 481 } } | Popular Tags |