KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > TextureChooser


1 /*
2  * @(#)TextureChooser.java 1.34 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  * @(#)TextureChooser.java 1.34 06/08/29
39  */

40
41 package java2d;
42
43 import static java.awt.Color JavaDoc.*;
44 import java.awt.*;
45 import java.awt.geom.Ellipse2D JavaDoc;
46 import java.awt.image.BufferedImage JavaDoc;
47 import java.awt.event.MouseListener JavaDoc;
48 import java.awt.event.MouseEvent JavaDoc;
49 import java.awt.font.TextLayout JavaDoc;
50 import java.awt.font.FontRenderContext JavaDoc;
51 import javax.swing.JPanel JavaDoc;
52 import javax.swing.border.TitledBorder JavaDoc;
53 import javax.swing.border.EtchedBorder JavaDoc;
54 import java.awt.event.WindowEvent JavaDoc;
55 import java.awt.event.WindowAdapter JavaDoc;
56
57
58 /**
59  * Four types of Paint displayed: Geometry, Text & Image Textures and
60  * a Gradient Paint. Paints can be selected with the Mouse.
61  */

62 public class TextureChooser extends JPanel JavaDoc {
63
64     static public Object JavaDoc texture = getGeomTexture();
65     public int num;
66
67     public TextureChooser(int num) {
68         this.num = num;
69         setLayout(new GridLayout(0,2,5,5));
70         setBorder(new TitledBorder JavaDoc(new EtchedBorder JavaDoc(), "Texture Chooser"));
71
72         add(new Surface( getGeomTexture(), this, 0));
73         add(new Surface( getImageTexture(), this, 1));
74         add(new Surface( getTextTexture(), this, 2));
75         add(new Surface(getGradientPaint(), this, 3));
76     }
77
78
79     static public TexturePaint getGeomTexture() {
80         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(5, 5, BufferedImage.TYPE_INT_RGB);
81         Graphics2D tG2 = bi.createGraphics();
82         tG2.setBackground(WHITE);
83         tG2.clearRect(0,0,5,5);
84         tG2.setColor(new Color JavaDoc(211,211,211,200));
85         tG2.fill(new Ellipse2D.Float JavaDoc(0,0,5,5));
86         Rectangle r = new Rectangle(0,0,5,5);
87         return new TexturePaint(bi,r);
88     }
89
90     public TexturePaint getImageTexture() {
91         Image JavaDoc img = DemoImages.getImage("java-logo.gif", this);
92         int iw = img.getWidth( this);
93         int ih = img.getHeight(this);
94         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(iw, ih, BufferedImage.TYPE_INT_RGB);
95         Graphics2D tG2 = bi.createGraphics();
96         tG2.drawImage(img, 0, 0, this);
97         Rectangle r = new Rectangle(0,0,iw,ih);
98         return new TexturePaint(bi,r);
99     }
100
101
102     public TexturePaint getTextTexture() {
103         Font f = new Font("Times New Roman", Font.BOLD, 10);
104         TextLayout JavaDoc tl = new TextLayout JavaDoc("Java2D", f, new FontRenderContext JavaDoc(null, false, false));
105         int sw = (int) tl.getBounds().getWidth();
106         int sh = (int) (tl.getAscent()+tl.getDescent());
107         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(sw, sh, BufferedImage.TYPE_INT_RGB);
108         Graphics2D tG2 = bi.createGraphics();
109         tG2.setBackground(WHITE);
110         tG2.clearRect(0,0,sw,sh);
111         tG2.setColor(LIGHT_GRAY);
112         tl.draw(tG2, 0, (float) tl.getAscent());
113         Rectangle r = new Rectangle(0,0,sw,sh);
114         return new TexturePaint(bi,r);
115     }
116
117
118     public GradientPaint getGradientPaint() {
119         return new GradientPaint(0,0,WHITE,80,0,GREEN);
120     }
121
122     public class Surface extends JPanel JavaDoc implements MouseListener JavaDoc {
123
124         public boolean clickedFrame;
125         private int num;
126         private TextureChooser tc;
127         private boolean enterExitFrame = false;
128         private Object JavaDoc t;
129
130         public Surface(Object JavaDoc t, TextureChooser tc, int num) {
131             setBackground(WHITE);
132             this.t = t;
133             this.tc = tc;
134             this.clickedFrame = (num == tc.num);
135             this.num = num;
136             if (num == tc.num)
137                 tc.texture = t;
138             addMouseListener(this);
139         }
140
141         public void paintComponent(Graphics g) {
142             super.paintComponent(g);
143             Graphics2D g2 = (Graphics2D) g;
144             int w = getSize().width;
145             int h = getSize().height;
146             if (t instanceof TexturePaint)
147                 g2.setPaint((TexturePaint) t);
148             else {
149                 g2.setPaint((GradientPaint) t);
150             }
151             g2.fill(new Rectangle(0,0,w,h));
152             if (clickedFrame || enterExitFrame) {
153                 g2.setColor(GRAY);
154                 BasicStroke bs = new BasicStroke(3, BasicStroke.CAP_BUTT,
155                                 BasicStroke.JOIN_MITER);
156                 g2.setStroke(bs);
157                 g2.drawRect(0,0,w-1,h-1);
158                 tc.num = num;
159             }
160         }
161
162         public void mouseClicked(MouseEvent JavaDoc e) {
163             tc.texture = t;
164             clickedFrame = true;
165
166             for (Component comp : tc.getComponents()) {
167                 if (comp instanceof Surface) {
168                     Surface surf = (Surface) comp;
169                     if (!surf.equals(this) && surf.clickedFrame) {
170                         surf.clickedFrame = false;
171                         surf.repaint();
172                     }
173                 }
174             }
175             
176             // ABP
177
if (Java2Demo.controls.textureCB.isSelected()) {
178                 Java2Demo.controls.textureCB.doClick();
179                 Java2Demo.controls.textureCB.doClick();
180         }
181         }
182
183         public void mousePressed(MouseEvent JavaDoc e) {
184         }
185
186         public void mouseReleased(MouseEvent JavaDoc e) {
187         }
188
189         public void mouseEntered(MouseEvent JavaDoc e) {
190             enterExitFrame = true;
191             repaint();
192         }
193
194         public void mouseExited(MouseEvent JavaDoc e) {
195             enterExitFrame = false;
196             repaint();
197         }
198
199         public Dimension getMinimumSize() {
200             return getPreferredSize();
201         }
202
203         public Dimension getMaximumSize() {
204             return getPreferredSize();
205         }
206
207         public Dimension getPreferredSize() {
208             return new Dimension(30,30);
209         }
210
211     }
212
213     public static void main(String JavaDoc s[]) {
214         Frame f = new Frame("Java2D Demo - TextureChooser");
215         f.addWindowListener(new WindowAdapter JavaDoc() {
216             public void windowClosing(WindowEvent JavaDoc e) {System.exit(0);}
217         });
218         f.add("Center", new TextureChooser(0));
219         f.pack();
220         f.setSize(new Dimension(400,400));
221         f.setVisible(true);
222     }
223 }
224
Popular Tags