1 36 37 40 41 package java2d.demos.Paint; 42 43 import static java.awt.Color .*; 44 import java.awt.*; 45 import java.awt.event.*; 46 import java.awt.image.BufferedImage ; 47 import java.awt.geom.AffineTransform ; 48 import java.awt.font.TextLayout ; 49 import java.awt.font.FontRenderContext ; 50 import javax.swing.*; 51 import java2d.AnimatingControlsSurface; 52 import java2d.CustomControls; 53 import javax.swing.plaf.metal.MetalBorders.ButtonBorder; 54 55 56 59 public class TextureAnim extends AnimatingControlsSurface { 60 61 public static final Color colorblend = new Color (0f, 0f, 1f, .5f); 62 protected static BufferedImage textureImg; 63 protected int bNum; 64 protected int tilesize; 65 private boolean newtexture; 66 private TexturePaint texture; 67 private Rectangle tilerect; 68 private boolean bouncesize = false; 69 private boolean bouncerect = true; 70 private boolean rotate = false; 71 private boolean shearx = false; 72 private boolean sheary = false; 73 private boolean showanchor = true; 74 private boolean quality = false; 75 private AnimVal w, h, x, y, rot, shx, shy; 76 private static Image img[] = new Image [2]; 77 78 79 public TextureAnim() { 80 img[0] = getImage("duke.gif"); img[1] = getImage("duke.png"); 83 textureImg = makeImage(32, 0); 84 tilesize = textureImg.getWidth(); 85 w = new AnimVal( 0, 200, 3, 10, tilesize); 86 h = new AnimVal( 0, 200, 3, 10, tilesize); 87 x = new AnimVal( 0, 200, 3, 10, 0); 88 y = new AnimVal( 0, 200, 3, 10, 0); 89 rot = new AnimVal(-360, 360, 5, 15, 0); 90 shx = new AnimVal( -50, 50, 3, 10, 0); 91 shy = new AnimVal( -50, 50, 3, 10, 0); 92 tilerect = new Rectangle(x.getInt(), y.getInt(), 93 w.getInt(), h.getInt()); 94 texture = new TexturePaint(textureImg, tilerect); 95 setControls(new Component[] { new DemoControls(this) }); 96 } 97 98 99 protected BufferedImage makeImage(int size, int num) { 100 newtexture = true; 101 switch (bNum = num) { 102 case 0 : return makeRGBImage(size); 103 case 1 : return makeGIFImage(size); 104 case 2 : return makePNGImage(size); 105 } 106 return null; 107 } 108 109 110 private BufferedImage makeRGBImage(int size) { 111 BufferedImage bi = new BufferedImage (size, size, BufferedImage.TYPE_INT_RGB); 112 Graphics2D big = bi.createGraphics(); 113 big.setColor(WHITE); 114 big.fillRect(0, 0, size, size); 115 for (int j = 0; j < size; j++) { 116 float RED = j / (float) size; 117 for (int i = 0; i < size; i++) { 118 float GREEN = i / (float) size; 119 big.setColor(new Color (1.0f - RED, 1.0f - GREEN, 0.0f, 1.0f)); 120 big.drawLine(i, j, i, j); 121 } 122 } 123 return bi; 124 } 125 126 127 private BufferedImage makeGIFImage(int d) { 128 BufferedImage bi = new BufferedImage (d, d, BufferedImage.TYPE_INT_RGB); 129 Graphics2D big = bi.createGraphics(); 130 big.drawImage(img[0], 0, 0, d, d, new Color (204, 204, 255), null); 131 return bi; 132 } 133 134 135 private BufferedImage makePNGImage(int d) { 136 BufferedImage bi = new BufferedImage (d, d, BufferedImage.TYPE_INT_RGB); 137 Graphics2D big = bi.createGraphics(); 138 big.drawImage(img[1], 0, 0, d, d, LIGHT_GRAY, null); 139 return bi; 140 } 141 142 143 public void reset(int width, int height) { 144 x.newlimits( -width/4, width/4 - w.getInt()); 145 y.newlimits(-height/4, height/4 - h.getInt()); 146 } 147 148 149 public void step(int width, int height) { 150 if (tilesize != textureImg.getWidth()) { 151 tilesize = textureImg.getWidth(); 152 } 153 if (bouncesize) { 154 w.anim(); 155 h.anim(); 156 x.newlimits( -width/4, width/4 - w.getInt()); 157 y.newlimits(-height/4, height/4 - h.getInt()); 158 } else { 159 if (w.getInt() != tilesize) { 160 w.set(tilesize); 161 x.newlimits(-width/4, width/4 - w.getInt()); 162 } 163 if (h.getInt() != tilesize) { 164 h.set(tilesize); 165 y.newlimits(-height/4, height/4 - h.getInt()); 166 } 167 } 168 if (bouncerect) { 169 x.anim(); 170 y.anim(); 171 } 172 if (newtexture || 173 x.getInt() != tilerect.x || 174 y.getInt() != tilerect.y || 175 w.getInt() != tilerect.width || 176 h.getInt() != tilerect.height) 177 { 178 newtexture = false; 179 int X = x.getInt(); 180 int Y = y.getInt(); 181 int W = w.getInt(); 182 int H = h.getInt(); 183 tilerect = new Rectangle(X, Y, W, H); 184 texture = new TexturePaint(textureImg, tilerect); 185 } 186 } 187 188 189 public void render(int width, int height, Graphics2D g2) { 190 191 g2.translate(width/2, height/2); 192 if (rotate) { 193 rot.anim(); 194 g2.rotate(Math.toRadians(rot.getFlt())); 195 } else { 196 rot.set(0); 197 } 198 if (shearx) { 199 shx.anim(); 200 g2.shear(shx.getFlt()/100, 0.0f); 201 } else { 202 shx.set(0); 203 } 204 if (sheary) { 205 shy.anim(); 206 g2.shear(0.0f, shy.getFlt()/100); 207 } else { 208 shy.set(0); 209 } 210 g2.setPaint(texture); 211 g2.fillRect(-1000, -1000, 2000, 2000); 212 if (showanchor) { 213 g2.setColor(BLACK); 214 g2.setColor(colorblend); 215 g2.fill(tilerect); 216 } 217 } 218 219 220 public static void main(String argv[]) { 221 createDemoFrame(new TextureAnim()); 222 } 223 224 225 static class AnimVal { 226 float curval; 227 float lowval; 228 float highval; 229 float currate; 230 float lowrate; 231 float highrate; 232 233 public AnimVal(int lowval, int highval, 234 int lowrate, int highrate) { 235 this.lowval = lowval; 236 this.highval = highval; 237 this.lowrate = lowrate; 238 this.highrate = highrate; 239 this.curval = randval(lowval, highval); 240 this.currate = randval(lowrate, highrate); 241 } 242 243 public AnimVal(int lowval, int highval, 244 int lowrate, int highrate, 245 int pos) { 246 this(lowval, highval, lowrate, highrate); 247 set(pos); 248 } 249 250 public float randval(float low, float high) { 251 return (float) (low + Math.random() * (high - low)); 252 } 253 254 public float getFlt() { 255 return curval; 256 } 257 258 public int getInt() { 259 return (int) curval; 260 } 261 262 public void anim() { 263 curval += currate; 264 clip(); 265 } 266 267 public void set(float val) { 268 curval = val; 269 clip(); 270 } 271 272 public void clip() { 273 if (curval > highval) { 274 curval = highval - (curval - highval); 275 if (curval < lowval) { 276 curval = highval; 277 } 278 currate = - randval(lowrate, highrate); 279 } else if (curval < lowval) { 280 curval = lowval + (lowval - curval); 281 if (curval > highval) { 282 curval = lowval; 283 } 284 currate = randval(lowrate, highrate); 285 } 286 } 287 288 public void newlimits(int lowval, int highval) { 289 this.lowval = lowval; 290 this.highval = highval; 291 clip(); 292 } 293 } 295 296 class DemoControls extends CustomControls implements ActionListener { 297 298 TextureAnim demo; 299 JToolBar toolbar; 300 JComboBox combo; 301 JMenu menu; 302 JMenuItem menuitems[]; 303 int iconSize = 20; 304 ButtonBorder buttonBorder = new ButtonBorder(); 305 306 public DemoControls(TextureAnim demo) { 307 super(demo.name); 308 this.demo = demo; 309 menuitems = new JMenuItem[3]; 310 add(toolbar = new JToolBar()); 311 toolbar.setFloatable(false); 312 addTool("BO", "bounce", true ); 313 addTool("SA", "show anchor", true ); 314 addTool("RS", "resize", false); 315 addTool("RO", "rotate", false); 316 addTool("SX", "shear x", false); 317 addTool("SY", "shear y", false); 318 add(combo = new JComboBox()); 319 combo.addActionListener(this); 320 combo.addItem( "8"); 321 combo.addItem("16"); 322 combo.addItem("32"); 323 combo.addItem("64"); 324 combo.addItem("80"); 325 combo.setSelectedIndex(2); 326 327 JMenuBar menuBar = new JMenuBar(); 328 menu = (JMenu) menuBar.add(new JMenu()); 329 for (int i = 0; i < 3; i++) { 330 BufferedImage bimg = demo.makeImage(iconSize, i); 331 TexturedIcon icon = new TexturedIcon(bimg); 332 menuitems[i] = menu.add(new JMenuItem(icon)); 333 menuitems[i].addActionListener(this); 334 } 335 menu.setIcon(menuitems[0].getIcon()); 336 add(menuBar); 337 demo.bNum = 0; 338 } 339 340 341 public void addTool(String str, String toolTip, boolean state) { 342 JToggleButton b = (JToggleButton) toolbar.add(new JToggleButton(str)); 343 b.setBorder(buttonBorder); 344 b.setFocusPainted(false); 345 b.setSelected(state); 346 b.setToolTipText(toolTip); 347 b.addActionListener(this); 348 int width = b.getPreferredSize().width; 349 Dimension prefSize = new Dimension(width, 21); 350 b.setPreferredSize(prefSize); 351 b.setMaximumSize(prefSize); 352 b.setMinimumSize(prefSize); 353 } 354 355 356 public void actionPerformed(ActionEvent e) { 357 Object obj = e.getSource(); 358 if (obj instanceof JComboBox) { 359 String selItem = (String ) combo.getSelectedItem(); 360 if (selItem != null) { 361 int size = Integer.parseInt(selItem); 362 demo.textureImg = demo.makeImage(size, demo.bNum); 363 } 364 } else if (obj instanceof JMenuItem) { 365 for (int i = 0; i < menuitems.length; i++) { 366 if (obj.equals(menuitems[i])) { 367 demo.textureImg = demo.makeImage(demo.tilesize, i); 368 menu.setIcon(menuitems[i].getIcon()); 369 break; 370 } 371 } 372 } else { 373 JToggleButton b = (JToggleButton) obj; 374 if (b.getText().equals("BO")) { 375 demo.bouncerect = b.isSelected(); 376 } else if (b.getText().equals("SA")) { 377 demo.showanchor = b.isSelected(); 378 } else if (b.getText().equals("RS")) { 379 demo.bouncesize = b.isSelected(); 380 } else if (b.getText().equals("RO")) { 381 demo.rotate = b.isSelected(); 382 } else if (b.getText().equals("SX")) { 383 demo.shearx = b.isSelected(); 384 } else if (b.getText().equals("SY")) { 385 demo.sheary = b.isSelected(); 386 } 387 } 388 if (demo.animating.thread == null) { 389 demo.repaint(); 390 } 391 } 392 393 public Dimension getPreferredSize() { 394 return new Dimension(200,41); 395 } 396 397 398 public void run() { 399 Thread me = Thread.currentThread(); 400 while (thread == me) { 401 for (int i = 2; i < toolbar.getComponentCount(); i++) { 402 try { 403 thread.sleep(4444); 404 } catch (InterruptedException e) { return; } 405 ((AbstractButton) toolbar.getComponentAtIndex(i)).doClick(); 406 } 407 } 408 thread = null; 409 } 410 411 412 class TexturedIcon implements Icon { 413 BufferedImage bi; 414 public TexturedIcon(BufferedImage bi) { 415 this.bi = bi; 416 } 417 public void paintIcon(Component c, Graphics g, int x, int y) { 418 Graphics2D g2 = (Graphics2D) g; 419 Rectangle r = new Rectangle(x, y, iconSize, iconSize); 420 g2.setPaint(new TexturePaint(bi, r)); 421 g2.fillRect(x, y, iconSize, iconSize); 422 g2.setColor(GRAY); 423 g2.draw3DRect(x, y, iconSize-1, iconSize-1, true); 424 } 425 public int getIconWidth() { return iconSize; } 426 public int getIconHeight() { return iconSize; } 427 } } } | Popular Tags |