1 36 37 40 41 package java2d.demos.Clipping; 42 43 import java.awt.*; 44 import java.awt.event.*; 45 import java.awt.geom.*; 46 import java.awt.image.BufferedImage ; 47 import javax.swing.*; 48 import java2d.AnimatingControlsSurface; 49 import java2d.CustomControls; 50 51 import static java.lang.Math.random ; 52 import static java.awt.Color .*; 53 54 55 58 public class ClipAnim extends AnimatingControlsSurface { 59 60 private static Image dimg, cimg; 61 private static Color redBlend = new Color (255, 0, 0, 120); 62 private static Color greenBlend = new Color ( 0, 255, 0, 120); 63 private static BasicStroke bs = new BasicStroke(20.0f); 64 static TexturePaint texture; 65 static { 66 BufferedImage bi = new BufferedImage (5, 5, BufferedImage.TYPE_INT_RGB); 67 Graphics2D big = bi.createGraphics(); 68 big.setBackground(YELLOW); 69 big.clearRect(0,0,5,5); 70 big.setColor(RED); 71 big.fillRect(0,0,3,3); 72 texture = new TexturePaint(bi,new Rectangle(0,0,5,5)); 73 } 74 private AnimVal animval[] = new AnimVal[3]; 75 protected boolean doObjects = true; 76 private Font originalFont = new Font("serif", Font.PLAIN, 12); 77 private Font font; 78 private GradientPaint gradient; 79 private int strX, strY; 80 private int dukeX, dukeY; 81 82 83 public ClipAnim() { 84 cimg = getImage("clouds.jpg"); 85 dimg = getImage("duke.gif"); 86 setBackground(WHITE); 87 animval[0] = new AnimVal(true); 88 animval[1] = new AnimVal(false); 89 animval[2] = new AnimVal(false); 90 setControls(new Component[] { new DemoControls(this) }); 91 } 92 93 94 public void reset(int w, int h) { 95 for (AnimVal a : animval) { 96 a.reset(w, h); 97 } 98 gradient = new GradientPaint(0,h/2,RED,w*.4f,h*.9f,YELLOW); 99 dukeX = (int) (w*.25 - dimg.getWidth (this)/2); 100 dukeY = (int) (h*.25 - dimg.getHeight(this)/2); 101 FontMetrics fm = getFontMetrics(originalFont); 102 double sw = fm.stringWidth("CLIPPING"); 103 double sh = fm.getAscent() + fm.getDescent(); 104 double sx = (w/2-30)/sw; 105 double sy = (h/2-30)/sh; 106 AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy); 107 font = originalFont.deriveFont(Tx); 108 fm = getFontMetrics(font); 109 strX = (int) (w*.75 - fm.stringWidth("CLIPPING")/2); 110 strY = (int) (h*.72 + fm.getAscent()/2); 111 } 112 113 114 public void step(int w, int h) { 115 for (AnimVal a : animval) { 116 if (a.isSelected) { 117 a.step(w, h); 118 } 119 } 120 } 121 122 123 public void render(int w, int h, Graphics2D g2) { 124 125 GeneralPath p1 = new GeneralPath(); 126 GeneralPath p2 = new GeneralPath(); 127 128 for (AnimVal a : animval) { 129 if (a.isSelected) { 130 double x = a.x; 131 double y = a.y; 132 double ew = a.ew; 133 double eh = a.eh; 134 p1.append(new Ellipse2D.Double(x, y, ew, eh ), false); 135 p2.append(new Rectangle2D.Double(x+5,y+5,ew-10,eh-10), false); 136 } 137 } 138 if (animval[0].isSelected || 139 animval[1].isSelected || 140 animval[2].isSelected) 141 { 142 g2.setClip(p1); 143 g2.clip(p2); 144 } 145 146 if (doObjects) { 147 int w2 = w/2; 148 int h2 = h/2; 149 g2.drawImage(cimg, 0, 0, w2, h2, null); 150 g2.drawImage(dimg, dukeX, dukeY, null); 151 152 g2.setPaint(texture); 153 g2.fillRect(w2, 0, w2, h2); 154 155 g2.setPaint(gradient); 156 g2.fillRect(0, h2, w2, h2); 157 158 g2.setColor(LIGHT_GRAY); 159 g2.fillRect(w2, h2, w2, h2); 160 g2.setColor(RED); 161 g2.drawOval(w2, h2, w2-1, h2-1); 162 g2.setFont(font); 163 g2.drawString("CLIPPING", strX, strY); 164 } else { 165 g2.setColor(LIGHT_GRAY); 166 g2.fillRect(0, 0, w, h); 167 } 168 } 169 170 171 public static void main(String argv[]) { 172 createDemoFrame(new ClipAnim()); 173 } 174 175 176 public class AnimVal { 177 double ix = 5.0; 178 double iy = 3.0; 179 double iw = 5.0; 180 double ih = 3.0; 181 double x, y; 182 double ew, eh; boolean isSelected; 184 185 public AnimVal(boolean isSelected) { 186 this.isSelected = isSelected; 187 } 188 189 190 public void step(int w, int h) { 191 x += ix; 192 y += iy; 193 ew += iw; 194 eh += ih; 195 196 if ( ew > w/2) { ew = w/2; iw = random() * -w/16 - 1; } 197 if ( ew < w/8) { ew = w/8; iw = random() * w/16 + 1; } 198 if ( eh > h/2) { eh = h/2; ih = random() * -h/16 - 1; } 199 if ( eh < h/8) { eh = h/8; ih = random() * h/16 + 1; } 200 201 if ((x+ew) > w ) { x = (w - ew)-1; ix = random() * -w/32 - 1; } 202 if ((y+eh) > h ) { y = (h - eh)-2; iy = random() * -h/32 - 1; } 203 if ( x < 0 ) { x = 2; ix = random() * w/32 + 1; } 204 if ( y < 0 ) { y = 2; iy = random() * h/32 + 1; } 205 } 206 207 208 public void reset(int w, int h) { 209 x = random()*w; 210 y = random()*h; 211 ew = (random()*w)/2; 212 eh = (random()*h)/2; 213 } 214 } 215 216 217 static class DemoControls extends CustomControls implements ActionListener { 218 219 ClipAnim demo; 220 JToolBar toolbar; 221 222 public DemoControls(ClipAnim demo) { 223 super(demo.name); 224 this.demo = demo; 225 add(toolbar = new JToolBar()); 226 toolbar.setFloatable(false); 227 addTool("Objects", true ); 228 addTool("Clip1", true ); 229 addTool("Clip2", false); 230 addTool("Clip3", false); 231 } 232 233 234 public void addTool(String str, boolean state) { 235 JToggleButton b = 236 (JToggleButton) toolbar.add(new JToggleButton(str)); 237 b.setFocusPainted(false); 238 b.setSelected(state); 239 b.addActionListener(this); 240 int width = b.getPreferredSize().width; 241 Dimension prefSize = new Dimension(width, 21); 242 b.setPreferredSize(prefSize); 243 b.setMaximumSize(prefSize); 244 b.setMinimumSize(prefSize); 245 } 246 247 248 public void actionPerformed(ActionEvent e) { 249 JToggleButton b = (JToggleButton) e.getSource(); 250 if (b.getText().equals("Objects")) { 251 demo.doObjects = b.isSelected(); 252 } else if (b.getText().equals("Clip1")) { 253 demo.animval[0].isSelected = b.isSelected(); 254 } else if (b.getText().equals("Clip2")) { 255 demo.animval[1].isSelected = b.isSelected(); 256 } else if (b.getText().equals("Clip3")) { 257 demo.animval[2].isSelected = b.isSelected(); 258 } 259 if (demo.animating.thread == null) { 260 demo.repaint(); 261 } 262 } 263 264 public Dimension getPreferredSize() { 265 return new Dimension(200,40); 266 } 267 268 269 public void run() { 270 try { 271 thread.sleep(5000); 272 } catch (InterruptedException e) { return; } 273 ((AbstractButton) toolbar.getComponentAtIndex(2)).doClick(); 274 try { 275 thread.sleep(5000); 276 } catch (InterruptedException e) { return; } 277 if (getSize().width > 400) { 278 ((AbstractButton) toolbar.getComponentAtIndex(3)).doClick(); 279 } 280 thread = null; 281 } 282 } } | Popular Tags |