KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Paint > Texture


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

40
41 package java2d.demos.Paint;
42
43
44 import static java.awt.Color JavaDoc.*;
45 import java.awt.*;
46 import java.awt.geom.Ellipse2D JavaDoc;
47 import java.awt.geom.GeneralPath JavaDoc;
48 import java.awt.geom.AffineTransform JavaDoc;
49 import java.awt.image.BufferedImage JavaDoc;
50 import java.awt.font.TextLayout JavaDoc;
51 import java.awt.font.FontRenderContext JavaDoc;
52 import java2d.Surface;
53
54
55 /**
56  * TexturePaint of gradient, buffered image and shapes.
57  */

58 public class Texture extends Surface {
59
60     private static TexturePaint bluedots, greendots, triangles;
61     private static TexturePaint blacklines, gradient;
62     static {
63         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(10, 10, BufferedImage.TYPE_INT_RGB);
64         Graphics2D gi = bi.createGraphics();
65         gi.setBackground(WHITE);
66         gi.clearRect(0,0,10,10);
67         GeneralPath JavaDoc p1 = new GeneralPath JavaDoc();
68         p1.moveTo(0,0);
69         p1.lineTo(5,10);
70         p1.lineTo(10,0);
71         p1.closePath();
72         gi.setColor(LIGHT_GRAY);
73         gi.fill(p1);
74         triangles = new TexturePaint(bi,new Rectangle(0,0,10,10));
75
76         bi = new BufferedImage JavaDoc(5, 5, BufferedImage.TYPE_INT_RGB);
77         gi = bi.createGraphics();
78         gi.setColor(BLACK);
79         gi.fillRect(0,0,5,5);
80         gi.setColor(GRAY);
81         gi.fillRect(1,1,4,4);
82         blacklines = new TexturePaint(bi,new Rectangle(0,0,5,5));
83
84         int w = 30; int h = 30;
85         bi = new BufferedImage JavaDoc(w, h, BufferedImage.TYPE_INT_RGB);
86         gi = bi.createGraphics();
87         Color JavaDoc oc = WHITE; Color JavaDoc ic = LIGHT_GRAY;
88         gi.setPaint(new GradientPaint(0,0,oc,w*.35f,h*.35f,ic));
89         gi.fillRect(0, 0, w/2, h/2);
90         gi.setPaint(new GradientPaint(w,0,oc,w*.65f,h*.35f,ic));
91         gi.fillRect(w/2, 0, w/2, h/2);
92         gi.setPaint(new GradientPaint(0,h,oc,w*.35f,h*.65f,ic));
93         gi.fillRect(0, h/2, w/2, h/2);
94         gi.setPaint(new GradientPaint(w,h,oc,w*.65f,h*.65f,ic));
95         gi.fillRect(w/2, h/2, w/2, h/2);
96         gradient = new TexturePaint(bi,new Rectangle(0,0,w,h));
97
98         bi = new BufferedImage JavaDoc(2,2,BufferedImage.TYPE_INT_RGB);
99         bi.setRGB(0, 0, 0xffffffff); bi.setRGB(1, 0, 0xffffffff);
100         bi.setRGB(0, 1, 0xffffffff); bi.setRGB(1, 1, 0xff0000ff);
101         bluedots = new TexturePaint(bi,new Rectangle(0,0,2,2));
102
103         bi = new BufferedImage JavaDoc(2,2,BufferedImage.TYPE_INT_RGB);
104         bi.setRGB(0, 0, 0xffffffff); bi.setRGB(1, 0, 0xffffffff);
105         bi.setRGB(0, 1, 0xffffffff); bi.setRGB(1, 1, 0xff00ff00);
106         greendots = new TexturePaint(bi,new Rectangle(0,0,2,2));
107     }
108
109
110     public Texture() {
111         setBackground(WHITE);
112     }
113
114
115     public void render(int w, int h, Graphics2D g2) {
116
117         Rectangle r = new Rectangle(10,10,w-20,h/2-20);
118         g2.setPaint(gradient);
119         g2.fill(r);
120         g2.setPaint(GREEN);
121         g2.setStroke(new BasicStroke(20));
122         g2.draw(r);
123         g2.setPaint(blacklines);
124         g2.setStroke(new BasicStroke(15));
125         g2.draw(r);
126
127         Font f = new Font("Times New Roman", Font.BOLD, w/5);
128         TextLayout JavaDoc tl = new TextLayout JavaDoc("Texture", f, g2.getFontRenderContext());
129         int sw = (int) tl.getBounds().getWidth();
130         int sh = (int) tl.getBounds().getHeight();
131         Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2, h*.25+sh/2));
132         g2.setColor(BLACK);
133         g2.setStroke(new BasicStroke(3));
134         g2.draw(sha);
135         g2.setPaint(greendots);
136         g2.fill(sha);
137
138         r.setLocation(10,h/2+10);
139         g2.setPaint(triangles);
140         g2.fill(r);
141         g2.setPaint(blacklines);
142         g2.setStroke(new BasicStroke(20));
143         g2.draw(r);
144         g2.setPaint(GREEN);
145         g2.setStroke(new BasicStroke(4));
146         g2.draw(r);
147
148         f = new Font("serif", Font.BOLD, w/4);
149         tl = new TextLayout JavaDoc("Paint", f, g2.getFontRenderContext());
150         sw = (int) tl.getBounds().getWidth();
151         sh = (int) tl.getBounds().getHeight();
152         sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2, h*.75+sh/2));
153         g2.setColor(BLACK);
154         g2.setStroke(new BasicStroke(5));
155         g2.draw(sha);
156         g2.setPaint(bluedots);
157         g2.fill(sha);
158     }
159
160
161     public static void main(String JavaDoc s[]) {
162         createDemoFrame(new Texture());
163     }
164 }
165
Popular Tags