|
1 /* |
|
2 * |
|
3 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * |
|
9 * - Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * - Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * - Neither the name of Oracle nor the names of its |
|
17 * contributors may be used to endorse or promote products derived |
|
18 * from this software without specific prior written permission. |
|
19 * |
|
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 package java2d.demos.Lines; |
|
33 |
|
34 |
|
35 import static java.awt.Color.BLACK; |
|
36 import static java.awt.Color.WHITE; |
|
37 import java.awt.BasicStroke; |
|
38 import java.awt.BorderLayout; |
|
39 import java.awt.Component; |
|
40 import java.awt.Dimension; |
|
41 import java.awt.Font; |
|
42 import java.awt.Graphics; |
|
43 import java.awt.Graphics2D; |
|
44 import java.awt.RenderingHints; |
|
45 import java.awt.event.ActionEvent; |
|
46 import java.awt.event.ActionListener; |
|
47 import java.awt.geom.GeneralPath; |
|
48 import java2d.ControlsSurface; |
|
49 import java2d.CustomControls; |
|
50 import javax.swing.Icon; |
|
51 import javax.swing.JLabel; |
|
52 import javax.swing.JMenu; |
|
53 import javax.swing.JMenuBar; |
|
54 import javax.swing.JMenuItem; |
|
55 import javax.swing.JSlider; |
|
56 import javax.swing.JToolBar; |
|
57 import javax.swing.SwingConstants; |
|
58 import javax.swing.border.CompoundBorder; |
|
59 import javax.swing.border.EmptyBorder; |
|
60 import javax.swing.event.ChangeEvent; |
|
61 import javax.swing.event.ChangeListener; |
|
62 |
|
63 |
|
64 /** |
|
65 * BasicStroke join types and width sizes illustrated. Control for |
|
66 * rendering a shape returned from BasicStroke.createStrokedShape(Shape). |
|
67 */ |
|
68 @SuppressWarnings("serial") |
|
69 public class Joins extends ControlsSurface { |
|
70 |
|
71 protected int joinType = BasicStroke.JOIN_MITER; |
|
72 protected float bswidth = 20.0f; |
|
73 protected JSlider slider; |
|
74 protected JLabel label; |
|
75 |
|
76 public Joins() { |
|
77 setBackground(WHITE); |
|
78 slider = new JSlider(SwingConstants.VERTICAL, 0, 100, |
|
79 (int) (bswidth * 2)); |
|
80 slider.setPreferredSize(new Dimension(15, 100)); |
|
81 slider.addChangeListener(new ChangeListener() { |
|
82 |
|
83 public void stateChanged(ChangeEvent e) { |
|
84 // when using these sliders use double buffering, which means |
|
85 // ignoring when DemoSurface.imageType = 'On Screen' |
|
86 if (getImageType() <= 1) { |
|
87 setImageType(2); |
|
88 } |
|
89 bswidth = slider.getValue() / 2.0f; |
|
90 label.setText(" Width = " + String.valueOf(bswidth)); |
|
91 label.repaint(); |
|
92 repaint(); |
|
93 } |
|
94 }); |
|
95 setControls(new Component[] { new DemoControls(this), slider }); |
|
96 setConstraints(new String[] { BorderLayout.NORTH, BorderLayout.WEST }); |
|
97 } |
|
98 |
|
99 @Override |
|
100 public void render(int w, int h, Graphics2D g2) { |
|
101 BasicStroke bs = new BasicStroke(bswidth, |
|
102 BasicStroke.CAP_BUTT, joinType); |
|
103 GeneralPath p = new GeneralPath(); |
|
104 p.moveTo(-w / 4.0f, -h / 12.0f); |
|
105 p.lineTo(+w / 4.0f, -h / 12.0f); |
|
106 p.lineTo(-w / 6.0f, +h / 4.0f); |
|
107 p.lineTo(+0.0f, -h / 4.0f); |
|
108 p.lineTo(+w / 6.0f, +h / 4.0f); |
|
109 p.closePath(); |
|
110 p.closePath(); |
|
111 g2.translate(w / 2, h / 2); |
|
112 g2.setColor(BLACK); |
|
113 g2.draw(bs.createStrokedShape(p)); |
|
114 } |
|
115 |
|
116 public static void main(String s[]) { |
|
117 createDemoFrame(new Joins()); |
|
118 } |
|
119 |
|
120 |
|
121 class DemoControls extends CustomControls implements ActionListener { |
|
122 |
|
123 Joins demo; |
|
124 int joinType[] = { BasicStroke.JOIN_MITER, |
|
125 BasicStroke.JOIN_ROUND, BasicStroke.JOIN_BEVEL }; |
|
126 String joinName[] = { "Mitered Join", "Rounded Join", "Beveled Join" }; |
|
127 JMenu menu; |
|
128 JMenuItem menuitem[] = new JMenuItem[joinType.length]; |
|
129 JoinIcon icons[] = new JoinIcon[joinType.length]; |
|
130 JToolBar toolbar; |
|
131 |
|
132 @SuppressWarnings("LeakingThisInConstructor") |
|
133 public DemoControls(Joins demo) { |
|
134 super(demo.name); |
|
135 setBorder(new CompoundBorder(getBorder(), |
|
136 new EmptyBorder(2, 2, 2, 2))); |
|
137 this.demo = demo; |
|
138 setLayout(new BorderLayout()); |
|
139 label = new JLabel(" Width = " + String.valueOf(demo.bswidth)); |
|
140 Font font = new Font(Font.SERIF, Font.BOLD, 14); |
|
141 label.setFont(font); |
|
142 add("West", label); |
|
143 JMenuBar menubar = new JMenuBar(); |
|
144 add("East", menubar); |
|
145 menu = menubar.add(new JMenu(joinName[0])); |
|
146 menu.setFont(font = new Font(Font.SERIF, Font.PLAIN, 10)); |
|
147 ActionListener actionListener = new ActionListener() { |
|
148 |
|
149 public void actionPerformed(ActionEvent e) { |
|
150 throw new UnsupportedOperationException("Not supported yet."); |
|
151 } |
|
152 }; |
|
153 for (int i = 0; i < joinType.length; i++) { |
|
154 icons[i] = new JoinIcon(joinType[i]); |
|
155 menuitem[i] = menu.add(new JMenuItem(joinName[i])); |
|
156 menuitem[i].setFont(font); |
|
157 menuitem[i].setIcon(icons[i]); |
|
158 menuitem[i].addActionListener(this); |
|
159 } |
|
160 menu.setIcon(icons[0]); |
|
161 } |
|
162 |
|
163 @Override |
|
164 public void actionPerformed(ActionEvent e) { |
|
165 for (int i = 0; i < joinType.length; i++) { |
|
166 if (e.getSource().equals(menuitem[i])) { |
|
167 demo.joinType = joinType[i]; |
|
168 menu.setIcon(icons[i]); |
|
169 menu.setText(joinName[i]); |
|
170 break; |
|
171 } |
|
172 } |
|
173 demo.repaint(); |
|
174 } |
|
175 |
|
176 @Override |
|
177 public Dimension getPreferredSize() { |
|
178 return new Dimension(200, 37); |
|
179 } |
|
180 |
|
181 @Override |
|
182 @SuppressWarnings("SleepWhileHoldingLock") |
|
183 public void run() { |
|
184 try { |
|
185 Thread.sleep(999); |
|
186 } catch (Exception e) { |
|
187 return; |
|
188 } |
|
189 Thread me = Thread.currentThread(); |
|
190 while (thread == me) { |
|
191 for (int i = 0; i < menuitem.length; i++) { |
|
192 menuitem[i].doClick(); |
|
193 for (int k = 10; k < 60; k += 2) { |
|
194 demo.slider.setValue(k); |
|
195 try { |
|
196 Thread.sleep(100); |
|
197 } catch (InterruptedException e) { |
|
198 return; |
|
199 } |
|
200 } |
|
201 try { |
|
202 Thread.sleep(999); |
|
203 } catch (InterruptedException e) { |
|
204 return; |
|
205 } |
|
206 } |
|
207 } |
|
208 thread = null; |
|
209 } |
|
210 |
|
211 |
|
212 class JoinIcon implements Icon { |
|
213 |
|
214 int joinType; |
|
215 |
|
216 public JoinIcon(int joinType) { |
|
217 this.joinType = joinType; |
|
218 } |
|
219 |
|
220 @Override |
|
221 public void paintIcon(Component c, Graphics g, int x, int y) { |
|
222 ((Graphics2D) g).setRenderingHint( |
|
223 RenderingHints.KEY_ANTIALIASING, |
|
224 RenderingHints.VALUE_ANTIALIAS_ON); |
|
225 BasicStroke bs = new BasicStroke(8.0f, |
|
226 BasicStroke.CAP_BUTT, joinType); |
|
227 ((Graphics2D) g).setStroke(bs); |
|
228 GeneralPath p = new GeneralPath(); |
|
229 p.moveTo(0, 3); |
|
230 p.lineTo(getIconWidth() - 2, getIconHeight() / 2); |
|
231 p.lineTo(0, getIconHeight()); |
|
232 ((Graphics2D) g).draw(p); |
|
233 } |
|
234 |
|
235 @Override |
|
236 public int getIconWidth() { |
|
237 return 20; |
|
238 } |
|
239 |
|
240 @Override |
|
241 public int getIconHeight() { |
|
242 return 20; |
|
243 } |
|
244 } // End JoinIcon class |
|
245 } // End DemoControls class |
|
246 } // End Joins class |
|
247 |