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.font.FontRenderContext ; 47 import java.awt.font.TextLayout ; 48 import javax.swing.*; 49 import java2d.AnimatingControlsSurface; 50 import java2d.CustomControls; 51 52 import static java.awt.Color .*; 53 54 55 56 59 public class Intersection extends AnimatingControlsSurface { 60 61 private static final int HEIGHT_DECREASE = 0; 62 private static final int HEIGHT_INCREASE = 1; 63 private static final int WIDTH_DECREASE = 2; 64 private static final int WIDTH_INCREASE = 3; 65 66 private int xx, yy, ww, hh; 67 private int direction = HEIGHT_DECREASE; 68 private int angdeg; 69 private Shape textshape; 70 private double sw, sh; 71 private GeneralPath ovals; 72 private Rectangle2D rectshape; 73 private DemoControls controls; 74 protected boolean doIntersection = true; 75 protected boolean doOvals = true; 76 protected boolean doText; 77 protected boolean threeSixty; 78 79 80 public Intersection() { 81 setBackground(WHITE); 82 setControls(new Component[] { new DemoControls(this) }); 83 } 84 85 86 public void reset(int w, int h) { 87 xx = yy = 0; 88 ww = w-1; hh = h; 89 direction = HEIGHT_DECREASE; 90 angdeg = 0; 91 FontRenderContext frc = new FontRenderContext (null, true, false); 92 Font f = new Font("serif",Font.BOLD,32); 93 TextLayout tl = new TextLayout ("J2D", f, frc); 94 sw = tl.getBounds().getWidth(); 95 sh = tl.getBounds().getHeight(); 96 int size = Math.min(w, h); 97 double sx = (size-40)/sw; 98 double sy = (size-100)/sh; 99 AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy); 100 textshape = tl.getOutline(Tx); 101 rectshape = textshape.getBounds(); 102 sw = rectshape.getWidth(); 103 sh = rectshape.getHeight(); 104 ovals = new GeneralPath(); 105 ovals.append(new Ellipse2D.Double( 10, 10, 20, 20), false); 106 ovals.append(new Ellipse2D.Double(w-30, 10, 20, 20), false); 107 ovals.append(new Ellipse2D.Double( 10, h-30, 20, 20), false); 108 ovals.append(new Ellipse2D.Double(w-30, h-30, 20, 20), false); 109 } 110 111 112 public void step(int w, int h) { 113 if (direction == HEIGHT_DECREASE) { 114 yy+=2; hh-=4; 115 if (yy >= h/2) { 116 direction = HEIGHT_INCREASE; 117 } 118 } else if (direction == HEIGHT_INCREASE) { 119 yy-=2; hh+=4; 120 if (yy <= 0) { 121 direction = WIDTH_DECREASE; 122 hh = h-1; yy = 0; 123 } 124 } 125 if (direction == WIDTH_DECREASE) { 126 xx+=2; ww-=4; 127 if (xx >= w/2) { 128 direction = WIDTH_INCREASE; 129 } 130 } else if (direction == WIDTH_INCREASE) { 131 xx-=2; ww+=4; 132 if (xx <= 0) { 133 direction = HEIGHT_DECREASE; 134 ww = w-1; xx = 0; 135 } 136 } 137 if ((angdeg += 5) == 360) { 138 angdeg = 0; 139 threeSixty = true; 140 } 141 } 142 143 144 public void render(int w, int h, Graphics2D g2) { 145 146 Rectangle rect = new Rectangle(xx, yy, ww, hh); 147 148 AffineTransform Tx = new AffineTransform(); 149 Tx.rotate(Math.toRadians(angdeg),w/2,h/2); 150 Tx.translate(w/2-sw/2, sh+(h-sh)/2); 151 152 GeneralPath path = new GeneralPath(); 153 if (doOvals) { 154 path.append(ovals, false); 155 } 156 if (doText) { 157 path.append(Tx.createTransformedShape(textshape), false); 158 } else { 159 path.append(Tx.createTransformedShape(rectshape), false); 160 } 161 162 if (doIntersection) { 163 g2.clip(rect); 164 g2.clip(path); 165 } 166 167 g2.setColor(GREEN); 168 g2.fill(rect); 169 170 g2.setClip(new Rectangle(0, 0, w, h)); 171 172 g2.setColor(LIGHT_GRAY); 173 g2.draw(rect); 174 g2.setColor(BLACK); 175 g2.draw(path); 176 } 177 178 179 public static void main(String argv[]) { 180 createDemoFrame(new Intersection()); 181 } 182 183 184 static class DemoControls extends CustomControls implements ActionListener { 185 186 Intersection demo; 187 JToolBar toolbar; 188 189 public DemoControls(Intersection demo) { 190 super(demo.name); 191 this.demo = demo; 192 add(toolbar = new JToolBar()); 193 toolbar.setFloatable(false); 194 addTool("Intersect", true ); 195 addTool("Text", false); 196 addTool("Ovals", true ); 197 } 198 199 200 public void addTool(String str, boolean state) { 201 JToggleButton b = (JToggleButton) toolbar.add(new JToggleButton(str)); 202 b.setFocusPainted(false); 203 b.setSelected(state); 204 b.addActionListener(this); 205 int width = b.getPreferredSize().width; 206 Dimension prefSize = new Dimension(width, 21); 207 b.setPreferredSize(prefSize); 208 b.setMaximumSize( prefSize); 209 b.setMinimumSize( prefSize); 210 } 211 212 213 public void actionPerformed(ActionEvent e) { 214 JToggleButton b = (JToggleButton) e.getSource(); 215 if (b.getText().equals("Intersect")) { 216 demo.doIntersection = b.isSelected(); 217 } else if (b.getText().equals("Ovals")) { 218 demo.doOvals = b.isSelected(); 219 } else if (b.getText().equals("Text")) { 220 demo.doText = b.isSelected(); 221 } 222 if (demo.animating.thread == null) { 223 demo.repaint(); 224 } 225 } 226 227 public Dimension getPreferredSize() { 228 return new Dimension(200,40); 229 } 230 231 232 public void run() { 233 Thread me = Thread.currentThread(); 234 while (thread == me) { 235 if (demo.threeSixty) { 236 ((AbstractButton) toolbar.getComponentAtIndex(1)).doClick(); 237 demo.threeSixty = false; 238 } 239 try { 240 thread.sleep(500); 241 } catch (InterruptedException e) { return; } 242 } 243 thread = null; 244 } 245 } } | Popular Tags |