KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Colors


1 // $Id: Colors.java,v 1.2 2003/11/03 10:56:14 mike Exp $
2

3 import java.io.*;
4 import java.awt.*;
5 import java.awt.color.*;
6 import org.faceless.pdf2.*;
7
8 // An example demonstrating the different types of color available for use
9
// in the PDF library.
10
//
11
// Note that due to bugs in several implementations of the java.awt.color.*
12
// package (notably many of the IBM implementations), aspects of this example
13
// may not work on all platforms.
14
//
15
public class Colors
16 {
17     public static void main(String JavaDoc[] args) throws Exception JavaDoc
18     {
19     PDF pdf = new PDF();
20     PDFPage page = pdf.newPage(PDF.PAGESIZE_A4);
21
22     PDFStyle text = new PDFStyle();
23     text.setFont(new StandardFont(StandardFont.HELVETICA), 9);
24     text.setFillColor(Color.black);
25
26     // First example is the most common case - draw a red rectangle
27
// using the sRGB colorspace, which is the Java default.
28
//
29
PDFStyle color = new PDFStyle();
30     color.setLineColor(Color.black);
31     color.setFillColor(Color.red);
32     page.setStyle(color);
33     page.drawRectangle(50,750,100,700);
34     page.setStyle(text);
35     page.drawText("Red in the default sRGB colorspace", 120, 720);
36
37     // Next, use the java.awt.GradientPaint class to define a
38
// red-to-blue gradient, in the sRGB colorspace again.
39
//
40
PDFStyle gradient = new PDFStyle();
41     gradient.setLineColor(Color.black);
42     gradient.setFillColor(new GradientPaint(50,700,Color.red, 150,700,Color.blue));
43     page.setStyle(gradient);
44     page.drawRectangle(50,650,100,600);
45     page.setStyle(text);
46     page.drawText("A red-blue gradient in the sRGB colorspace", 120, 620);
47
48     // The org.faceless.pdf.ColorPattern class defines a repeating
49
// two color pattern which can be used as a paint for shapes or text.
50
//
51
PDFStyle pattern = new PDFStyle();
52     pattern.setLineColor(Color.black);
53     pattern.setFillColor(new PDFPattern("Star",0,0,50,50,Color.red, Color.blue));
54     page.setStyle(pattern);
55     page.drawRectangle(50,550,100,500);
56     page.setStyle(text);
57     page.drawText("A pattern using the ColorPattern class", 120, 520);
58
59     // The CMYKColorSpace class defines a color in the four-color CMYK
60
// range. This is not a calibrated colorspace, so to a certain
61
// extent the exact output color is device-dependent, but as
62
// this colorspace has a larger range, and as most printers are CMYK
63
// based, this is still a useful space to use.
64
//
65
PDFStyle cmyk = new PDFStyle();
66     cmyk.setLineColor(Color.black);
67     Color cmykblue = CMYKColorSpace.getColor(1.0f, 0.72f, 0f, 0.06f);
68     cmyk.setFillColor(cmykblue);
69     page.setStyle(cmyk);
70     page.drawRectangle(50,450,100,400);
71     page.setStyle(text);
72     page.drawText("A color in the uncalibrated CMYK ColorSpace", 120, 420);
73
74     // The SpotColorSpace allows a specific "ink" to be specified. If
75
// the output device has this ink it's used, but otherwise the
76
// backup color (which must be specified) will be used instead.
77
//
78
PDFStyle spot = new PDFStyle();
79     spot.setLineColor(Color.black);
80     spot.setFillColor(new SpotColorSpace("PANTONE Reflex Blue CVC", cmykblue).getColor(1));
81     page.setStyle(spot);
82     page.drawRectangle(50,350,100,300);
83     page.setStyle(text);
84     page.drawText("The PANTONE Reflex Blue spot color", 120, 320);
85
86
87     // From here it gets more difficult, as several implementations
88
// of Java don't handle non-sRGB colorspaces at all, and are liable
89
// to throw anything, including Exceptions and Errors.
90
// Generally we find encasing the block in a "try/catch (Throwable)"
91
// will handle these exceptions.
92

93     // First up, the CS_GRAY colorspace allows grayscale colors to be
94
// specified.
95
//
96
try {
97         ColorSpace grayspace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
98         PDFStyle gray = new PDFStyle();
99         gray.setLineColor(Color.black);
100         float[] comp = { 0.5f };
101         gray.setFillColor(new Color(grayspace, comp, 1));
102         page.setStyle(gray);
103         page.drawRectangle(50,250,100,200);
104     } catch (Throwable JavaDoc e) {
105         System.err.println("WARNING: Gray ColorSpace: Caught "+e);
106     }
107     page.setStyle(text);
108     page.drawText("A 50% gray color in the grayscale ColorSpace", 120, 220);
109
110     // Finally, try loading a specific ICC profile from a file.
111
//
112
try {
113         ICC_Profile prof = ICC_Profile.getInstance("resources/NTSC.icm");
114         ICC_ColorSpace ntsc = new ICC_ColorSpace(prof);
115         float[] comp = { 1.0f, 0.0f, 0.0f };
116
117         PDFStyle icc = new PDFStyle();
118         icc.setLineColor(Color.black);
119         icc.setFillColor(new Color(ntsc, comp, 1));
120         page.setStyle(icc);
121         page.drawRectangle(50,150,100,100);
122     } catch (Throwable JavaDoc e) {
123         System.err.println("WARNING: ICC ColorSpace: Caught " + e);
124     }
125     page.setStyle(text);
126     page.drawText("A color in the NTSC ICC ColorSpace", 120, 120);
127     OutputStream fo = new FileOutputStream("Colors.pdf");
128     pdf.render(fo);
129     fo.close();
130     }
131 }
132
Popular Tags