KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Lines > Joins


1 /*
2  * @(#)Joins.java 1.30 06/08/29
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)Joins.java 1.30 06/08/29
39  */

40
41 package java2d.demos.Lines;
42
43 import static java.awt.Color JavaDoc.*;
44 import java.awt.*;
45 import java.awt.event.*;
46 import java.awt.geom.AffineTransform JavaDoc;
47 import java.awt.geom.GeneralPath JavaDoc;
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 /**
56  * BasicStroke join types and width sizes illustrated. Control for
57  * rendering a shape returned from BasicStroke.createStrokedShape(Shape).
58  */

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 JavaDoc[] { BorderLayout.NORTH, BorderLayout.WEST});
74     }
75
76
77     public void stateChanged(ChangeEvent e) {
78         // when using these sliders use double buffering, which means
79
// ignoring when DemoSurface.imageType = 'On Screen'
80
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 JavaDoc p = new GeneralPath JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc e) { return; }
168             Thread JavaDoc 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 JavaDoc e) { return; }
177                     }
178                     try {
179                         thread.sleep(999);
180                     } catch (InterruptedException JavaDoc 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 JavaDoc p = new GeneralPath JavaDoc();
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         } // End JoinIcon class
209
} // End DemoControls class
210
} // End Joins class
211
Popular Tags