KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Clipping > Text


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

40
41 package java2d.demos.Clipping;
42
43 import java.awt.*;
44 import java.awt.event.*;
45 import java.awt.image.BufferedImage JavaDoc;
46 import java.awt.geom.Line2D JavaDoc;
47 import java.awt.geom.AffineTransform JavaDoc;
48 import java.awt.image.BufferedImage JavaDoc;
49 import java.awt.font.TextLayout JavaDoc;
50 import java.awt.font.FontRenderContext JavaDoc;
51 import javax.swing.*;
52 import java2d.ControlsSurface;
53 import java2d.CustomControls;
54
55 import static java.awt.Color JavaDoc.*;
56
57
58 /**
59  * Clipping an image, lines, text, texture and gradient with text.
60  */

61 public class Text extends ControlsSurface {
62
63     static Image JavaDoc img;
64     static TexturePaint texture;
65     static {
66         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(5, 5, BufferedImage.TYPE_INT_RGB);
67         Graphics2D big = bi.createGraphics();
68         big.setBackground(YELLOW);
69         big.clearRect(0,0,5,5);
70         big.setColor(RED);
71         big.fillRect(0,0,3,3);
72         texture = new TexturePaint(bi,new Rectangle(0,0,5,5));
73     }
74     private String JavaDoc clipType = "Lines";
75     private DemoControls controls;
76     protected boolean doClip = true;
77
78
79     public Text() {
80         setBackground(WHITE);
81         img = getImage("clouds.jpg");
82         setControls(new Component[] { new DemoControls(this) });
83     }
84
85
86     public void render(int w, int h, Graphics2D g2) {
87
88         FontRenderContext JavaDoc frc = g2.getFontRenderContext();
89         Font f = new Font("sansserif",Font.BOLD,32);
90         String JavaDoc s = new String JavaDoc("JAVA");
91         TextLayout JavaDoc tl = new TextLayout JavaDoc(s, f, frc);
92         double sw = tl.getBounds().getWidth();
93         double sh = tl.getBounds().getHeight();
94         double sx = (w-40)/sw;
95         double sy = (h-40)/sh;
96         AffineTransform JavaDoc Tx = AffineTransform.getScaleInstance(sx, sy);
97         Shape shape = tl.getOutline(Tx);
98         sw = shape.getBounds().getWidth();
99         sh = shape.getBounds().getHeight();
100         Tx = AffineTransform.getTranslateInstance(w/2-sw/2, h/2+sh/2);
101         shape = Tx.createTransformedShape(shape);
102         Rectangle r = shape.getBounds();
103
104         if (doClip) {
105             g2.clip(shape);
106         }
107
108         if (clipType.equals("Lines")) {
109             g2.setColor(BLACK);
110             g2.fill(r);
111             g2.setColor(YELLOW);
112             g2.setStroke(new BasicStroke(1.5f));
113             for (int j = r.y; j < r.y + r.height; j=j+3) {
114                 Line2D JavaDoc line = new Line2D.Float JavaDoc( (float) r.x, (float) j,
115                                             (float) (r.x+r.width), (float) j);
116                 g2.draw(line);
117             }
118         } else if (clipType.equals("Image")) {
119             g2.drawImage(img, r.x, r.y, r.width, r.height, null);
120         } else if (clipType.equals("TP")) {
121             g2.setPaint(texture);
122             g2.fill(r);
123         } else if (clipType.equals("GP")) {
124             g2.setPaint(new GradientPaint(0,0,BLUE,w,h,YELLOW));
125             g2.fill(r);
126         } else if (clipType.equals("Text")) {
127             g2.setColor(BLACK);
128             g2.fill(shape.getBounds());
129             g2.setColor(CYAN);
130             f = new Font("serif",Font.BOLD,10);
131             tl = new TextLayout JavaDoc("java", f, frc);
132             sw = tl.getBounds().getWidth();
133     
134             int x = r.x;
135             int y = (int) (r.y + tl.getAscent());
136             sh = r.y + r.height;
137             while ( y < sh ) {
138                 tl.draw(g2, x, y);
139                 if ((x += (int) sw) > (r.x+r.width)) {
140                     x = r.x;
141                     y += (int) tl.getAscent();
142                 }
143             }
144         }
145         g2.setClip(new Rectangle(0, 0, w, h));
146
147         g2.setColor(GRAY);
148         g2.draw(shape);
149     }
150
151
152     public static void main(String JavaDoc s[]) {
153         createDemoFrame(new Text());
154     }
155
156
157     static class DemoControls extends CustomControls implements ActionListener {
158
159         Text demo;
160         JToolBar toolbar;
161
162         public DemoControls(Text demo) {
163             super(demo.name);
164             this.demo = demo;
165             add(toolbar = new JToolBar());
166             toolbar.setFloatable(false);
167             addTool("Clip", true );
168             addTool("Lines", true );
169             addTool("Image", false);
170             addTool("TP", false);
171             addTool("GP", false);
172             addTool("Text" , false);
173         }
174
175         public void addTool(String JavaDoc str, boolean state) {
176             JToggleButton b = (JToggleButton) toolbar.add(new JToggleButton(str));
177             b.setFocusPainted(false);
178             b.setSelected(state);
179             b.addActionListener(this);
180             int width = b.getPreferredSize().width;
181             Dimension prefSize = new Dimension(width, 21);
182             b.setPreferredSize(prefSize);
183             b.setMaximumSize( prefSize);
184             b.setMinimumSize( prefSize);
185         }
186
187         public void actionPerformed(ActionEvent e) {
188             if (e.getSource().equals(toolbar.getComponentAtIndex(0))) {
189                 JToggleButton b = (JToggleButton) e.getSource();
190                 demo.doClip = b.isSelected();
191             } else {
192                 for(Component comp : toolbar.getComponents()) {
193                     ((JToggleButton) comp).setSelected(false);
194                 }
195                 JToggleButton b = (JToggleButton) e.getSource();
196                 b.setSelected(true);
197                 demo.clipType = b.getText();
198             }
199             demo.repaint();
200         }
201
202         public Dimension getPreferredSize() {
203             return new Dimension(200,40);
204         }
205
206
207         public void run() {
208             try { thread.sleep(1111); } catch (Exception JavaDoc e) { return; }
209             Thread JavaDoc me = Thread.currentThread();
210             while (thread == me) {
211                 for (int i = 1; i < toolbar.getComponentCount()-1; i++) {
212                     ((AbstractButton) toolbar.getComponentAtIndex(i)).doClick();
213                     try {
214                         thread.sleep(4444);
215                     } catch (InterruptedException JavaDoc e) { return; }
216                 }
217             }
218             thread = null;
219         }
220     } // End DemoControls
221
} // End Text
222
Popular Tags