| 1 36 37 40 41 package java2d.demos.Lines; 42 43 import static java.awt.Color .*; 44 import java.awt.*; 45 import java.awt.event.*; 46 import java.awt.geom.AffineTransform ; 47 import java.awt.geom.GeneralPath ; 48 import javax.swing.*; 49 import javax.swing.border.*; 50 import javax.swing.event.*; 51 import java2d.ControlsSurface; 52 import java2d.CustomControls; 53 54 55 59 public class Joins extends ControlsSurface implements ChangeListener { 60 61 protected int joinType = BasicStroke.JOIN_MITER; 62 protected float bswidth = 20.0f; 63 protected JSlider slider; 64 protected JLabel label; 65 66 67 public Joins() { 68 setBackground(WHITE); 69 slider = new JSlider(JSlider.VERTICAL, 0, 100, (int)(bswidth*2)); 70 slider.setPreferredSize(new Dimension(15, 100)); 71 slider.addChangeListener(this); 72 setControls(new Component[] { new DemoControls(this), slider }); 73 setConstraints(new String [] { BorderLayout.NORTH, BorderLayout.WEST}); 74 } 75 76 77 public void stateChanged(ChangeEvent e) { 78 if (getImageType() <= 1) { 81 setImageType(2); 82 } 83 bswidth = (float) slider.getValue() / 2.0f; 84 label.setText(" Width = " + String.valueOf(bswidth)); 85 label.repaint(); 86 repaint(); 87 } 88 89 90 public void render(int w, int h, Graphics2D g2) { 91 BasicStroke bs = new BasicStroke(bswidth, 92 BasicStroke.CAP_BUTT, joinType); 93 GeneralPath p = new GeneralPath (); 94 p.moveTo(- w / 4.0f, - h / 12.0f); 95 p.lineTo(+ w / 4.0f, - h / 12.0f); 96 p.lineTo(- w / 6.0f, + h / 4.0f); 97 p.lineTo(+ 0.0f, - h / 4.0f); 98 p.lineTo(+ w / 6.0f, + h / 4.0f); 99 p.closePath(); 100 p.closePath(); 101 g2.translate(w/2, h/2); 102 g2.setColor(BLACK); 103 g2.draw(bs.createStrokedShape(p)); 104 } 105 106 107 public static void main(String s[]) { 108 createDemoFrame(new Joins()); 109 } 110 111 112 class DemoControls extends CustomControls implements ActionListener { 113 114 Joins demo; 115 int joinType[] = { BasicStroke.JOIN_MITER, 116 BasicStroke.JOIN_ROUND, BasicStroke.JOIN_BEVEL }; 117 String joinName[] = { "Mitered Join", "Rounded Join", "Beveled Join" }; 118 JMenu menu; 119 JMenuItem menuitem[] = new JMenuItem[joinType.length]; 120 JoinIcon icons[] = new JoinIcon[joinType.length]; 121 JToolBar toolbar; 122 123 124 public DemoControls(Joins demo) { 125 super(demo.name); 126 setBorder(new CompoundBorder(getBorder(), new EmptyBorder(2, 2, 2, 2))); 127 this.demo = demo; 128 setLayout(new BorderLayout()); 129 label = new JLabel(" Width = " + String.valueOf(demo.bswidth)); 130 Font font = new Font("serif", Font.BOLD, 14); 131 label.setFont(font); 132 add("West", label); 133 JMenuBar menubar = new JMenuBar(); 134 add("East", menubar); 135 menu = (JMenu) menubar.add(new JMenu(joinName[0])); 136 menu.setFont(font = new Font("serif", Font.PLAIN, 10)); 137 for (int i = 0; i < joinType.length; i++) { 138 icons[i]= new JoinIcon(joinType[i]); 139 menuitem[i] = menu.add(new JMenuItem(joinName[i])); 140 menuitem[i].setFont(font); 141 menuitem[i].setIcon(icons[i]); 142 menuitem[i].addActionListener(this); 143 } 144 menu.setIcon(icons[0]); 145 } 146 147 148 public void actionPerformed(ActionEvent e) { 149 for (int i = 0; i < joinType.length; i++) { 150 if (e.getSource().equals(menuitem[i])) { 151 demo.joinType = joinType[i]; 152 menu.setIcon(icons[i]); 153 menu.setText(joinName[i]); 154 break; 155 } 156 } 157 demo.repaint(); 158 } 159 160 161 public Dimension getPreferredSize() { 162 return new Dimension(200,37); 163 } 164 165 166 public void run() { 167 try { thread.sleep(999); } catch (Exception e) { return; } 168 Thread me = Thread.currentThread(); 169 while (thread == me) { 170 for (int i = 0; i < menuitem.length; i++) { 171 menuitem[i].doClick(); 172 for (int k = 10; k < 60; k+=2) { 173 demo.slider.setValue(k); 174 try { 175 thread.sleep(100); 176 } catch (InterruptedException e) { return; } 177 } 178 try { 179 thread.sleep(999); 180 } catch (InterruptedException e) { return; } 181 } 182 } 183 thread = null; 184 } 185 186 187 class JoinIcon implements Icon { 188 int joinType; 189 public JoinIcon(int joinType) { 190 this.joinType = joinType; 191 } 192 193 public void paintIcon(Component c, Graphics g, int x, int y) { 194 ((Graphics2D) g).setRenderingHint( 195 RenderingHints.KEY_ANTIALIASING, 196 RenderingHints.VALUE_ANTIALIAS_ON); 197 BasicStroke bs = new BasicStroke(8.0f, 198 BasicStroke.CAP_BUTT, joinType); 199 ((Graphics2D) g).setStroke(bs); 200 GeneralPath p = new GeneralPath (); 201 p.moveTo(0, 3); 202 p.lineTo(getIconWidth()-2, getIconHeight()/2); 203 p.lineTo(0,getIconHeight()); 204 ((Graphics2D) g).draw(p); 205 } 206 public int getIconWidth() { return 20; } 207 public int getIconHeight() { return 20; } 208 } } } | Popular Tags |