KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Colors > ColorConvert


1 /*
2  * @(#)ColorConvert.java 1.38 06/08/09
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  * @(#)ColorConvert.java 1.38 06/08/09
39  */

40
41 package java2d.demos.Colors;
42
43 import java.awt.*;
44 import java.awt.geom.Rectangle2D JavaDoc;
45 import java.awt.color.ColorSpace JavaDoc;
46 import java.awt.image.BufferedImage JavaDoc;
47 import java.awt.image.ColorConvertOp JavaDoc;
48 import java.awt.font.TextLayout JavaDoc;
49 import java.awt.font.FontRenderContext JavaDoc;
50 import java2d.Surface;
51
52 import static java.awt.Color JavaDoc.*;
53
54
55 /**
56  * ColorConvertOp a ColorSpace.TYPE_RGB BufferedImage to a ColorSpace.CS_GRAY
57  * BufferedImage.
58  */

59 public class ColorConvert extends Surface {
60
61     private static Image JavaDoc img;
62     private static Color JavaDoc colors[] = { red, pink, orange,
63             yellow, green, magenta, cyan, blue};
64
65
66     public ColorConvert() {
67         setBackground(white);
68         img = getImage("clouds.jpg");
69     }
70
71
72     public void render(int w, int h, Graphics2D g2) {
73
74         int iw = img.getWidth(this);
75         int ih = img.getHeight(this);
76         FontRenderContext JavaDoc frc = g2.getFontRenderContext();
77         Font font = g2.getFont();
78         g2.setColor(black);
79         TextLayout JavaDoc tl = new TextLayout JavaDoc("ColorConvertOp RGB->GRAY", font, frc);
80         tl.draw(g2, (float) (w/2-tl.getBounds().getWidth()/2),
81                             tl.getAscent()+tl.getLeading());
82
83         BufferedImage JavaDoc srcImg =
84             new BufferedImage JavaDoc(iw, ih, BufferedImage.TYPE_INT_RGB);
85         Graphics2D srcG = srcImg.createGraphics();
86         RenderingHints rhs = g2.getRenderingHints();
87         srcG.setRenderingHints(rhs);
88         srcG.drawImage(img, 0, 0, null);
89
90         String JavaDoc s = "JavaColor";
91         Font f = new Font("serif", Font.BOLD, iw/6);
92         tl = new TextLayout JavaDoc(s, f, frc);
93         Rectangle2D JavaDoc tlb = tl.getBounds();
94         char[] chars = s.toCharArray();
95         float charWidth = 0.0f;
96         int rw = iw/chars.length;
97         int rh = ih/chars.length;
98         for (int i = 0; i < chars.length; i++) {
99             tl = new TextLayout JavaDoc(String.valueOf(chars[i]), f, frc);
100             Shape shape = tl.getOutline(null);
101             srcG.setColor(colors[i%colors.length]);
102             tl.draw(srcG, (float) (iw/2-tlb.getWidth()/2+charWidth),
103                           (float) (ih/2+tlb.getHeight()/2));
104             charWidth += (float) shape.getBounds().getWidth();
105             srcG.fillRect(i*rw, ih-rh, rw, rh);
106             srcG.setColor(colors[colors.length-1-i%colors.length]);
107             srcG.fillRect(i*rw, 0, rw, rh);
108         }
109
110         ColorSpace JavaDoc cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
111         ColorConvertOp JavaDoc theOp = new ColorConvertOp JavaDoc(cs, rhs);
112
113         BufferedImage JavaDoc dstImg =
114                 new BufferedImage JavaDoc(iw, ih, BufferedImage.TYPE_INT_RGB);
115         theOp.filter(srcImg, dstImg);
116
117         g2.drawImage(srcImg, 10, 20, w/2-20, h-30, null);
118         g2.drawImage(dstImg, w/2+10, 20, w/2-20, h-30, null);
119     }
120
121
122     public static void main(String JavaDoc s[]) {
123         createDemoFrame(new ColorConvert());
124     }
125 }
126
Popular Tags