1 36 37 40 41 package java2d.demos.Clipping; 42 43 import java.awt.*; 44 import java.awt.event.*; 45 import java.awt.geom.Area ; 46 import java.awt.geom.Ellipse2D ; 47 import java.awt.geom.GeneralPath ; 48 import javax.swing.*; 49 import java2d.ControlsSurface; 50 import java2d.CustomControls; 51 52 import static java.awt.Color .*; 53 54 55 59 public class Areas extends ControlsSurface { 60 61 protected String areaType = "nop"; 62 63 64 public Areas() { 65 setBackground(WHITE); 66 setControls(new Component[] { new DemoControls(this) }); 67 } 68 69 70 public void render(int w, int h, Graphics2D g2) { 71 GeneralPath p1 = new GeneralPath (); 72 p1.moveTo( w * .25f, 0.0f); 73 p1.lineTo( w * .75f, h * .5f); 74 p1.lineTo( w * .25f, (float) h); 75 p1.lineTo( 0.0f, h * .5f); 76 p1.closePath(); 77 78 GeneralPath p2 = new GeneralPath (); 79 p2.moveTo( w * .75f, 0.0f); 80 p2.lineTo( (float) w, h * .5f); 81 p2.lineTo( w * .75f, (float) h); 82 p2.lineTo( w * .25f, h * .5f); 83 p2.closePath(); 84 85 86 Area area = new Area (p1); 87 g2.setColor(YELLOW); 88 if (areaType.equals("nop")) { 89 g2.fill(p1); 90 g2.fill(p2); 91 g2.setColor(RED); 92 g2.draw(p1); 93 g2.draw(p2); 94 return; 95 } else if (areaType.equals("add" )) { area.add (new Area (p2)); 96 } else if (areaType.equals("sub" )) { area.subtract (new Area (p2)); 97 } else if (areaType.equals("xor" )) { area.exclusiveOr(new Area (p2)); 98 } else if (areaType.equals("int" )) { area.intersect (new Area (p2)); 99 } else if (areaType.equals("pear")) { 100 101 double sx = w/100; 102 double sy = h/140; 103 g2.scale(sx, sy); 104 double x = w/sx/2; 105 double y = h/sy/2; 106 107 Ellipse2D leaf = new Ellipse2D.Double (x-16, y-29, 15.0, 15.0); 110 Area leaf1 = new Area (leaf); 111 leaf.setFrame(x-14, y-47, 30.0, 30.0); 112 Area leaf2 = new Area (leaf); 113 leaf1.intersect(leaf2); 114 g2.setColor(GREEN); 115 g2.fill(leaf1); 116 117 leaf.setFrame(x+1, y-29, 15.0, 15.0); 119 leaf1 = new Area (leaf); 120 leaf2.intersect(leaf1); 121 g2.fill(leaf2); 122 123 Ellipse2D stem = new Ellipse2D.Double (x, y-42, 40.0, 40.0); 126 Area st1 = new Area (stem); 127 stem.setFrame(x+3, y-47, 50.0, 50.0); 128 st1.subtract(new Area (stem)); 129 g2.setColor(BLACK); 130 g2.fill(st1); 131 132 Ellipse2D circle = new Ellipse2D.Double (x-25, y, 50.0, 50.0); 135 Ellipse2D oval = new Ellipse2D.Double (x-19, y-20, 40.0, 70.0); 136 Area circ = new Area (circle); 137 circ.add(new Area (oval)); 138 139 g2.setColor(YELLOW); 140 g2.fill(circ); 141 return; 142 } 143 144 g2.fill(area); 145 g2.setColor(RED); 146 g2.draw(area); 147 } 148 149 150 public static void main(String argv[]) { 151 createDemoFrame(new Areas()); 152 } 153 154 155 156 static class DemoControls extends CustomControls implements ActionListener { 157 158 Areas demo; 159 JToolBar toolbar; 160 JComboBox combo; 161 162 public DemoControls(Areas demo) { 163 super(demo.name); 164 this.demo = demo; 165 add(toolbar = new JToolBar()); 166 toolbar.setFloatable(false); 167 addTool("nop", "no area operation", true ); 168 addTool("add", "add", false); 169 addTool("sub", "subtract", false); 170 addTool("xor", "exclusiveOr", false); 171 addTool("int", "intersection", false); 172 addTool("pear", "pear", false); 173 } 174 175 176 public void addTool(String str, String tooltip, boolean state) { 177 JToggleButton b = (JToggleButton) toolbar.add(new JToggleButton(str)); 178 b.setFocusPainted(false); 179 b.setToolTipText(tooltip); 180 b.setSelected(state); 181 b.addActionListener(this); 182 int width = (int) b.getPreferredSize().width; 183 Dimension prefSize = new Dimension(width, 21); 184 b.setPreferredSize(prefSize); 185 b.setMaximumSize(prefSize); 186 b.setMinimumSize(prefSize); 187 } 188 189 190 public void actionPerformed(ActionEvent e) { 191 for (Component comp : toolbar.getComponents()) { 192 ((JToggleButton) comp).setSelected(false); 193 } 194 JToggleButton b = (JToggleButton) e.getSource(); 195 b.setSelected(true); 196 demo.areaType = b.getText(); 197 demo.repaint(); 198 } 199 200 public Dimension getPreferredSize() { 201 return new Dimension(200,40); 202 } 203 204 public void run() { 205 try { thread.sleep(1111); } catch (Exception e) { return; } 206 Thread me = Thread.currentThread(); 207 while (thread == me) { 208 for (Component comp : toolbar.getComponents()) { 209 ((AbstractButton) comp).doClick(); 210 try { 211 thread.sleep(4444); 212 } catch (InterruptedException e) { return; } 213 } 214 } 215 thread = null; 216 } 217 } } | Popular Tags |