KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > java2d > Java2DGraphicsState


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: Java2DGraphicsState.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.java2d;
21
22 import java.awt.BasicStroke JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Graphics2D JavaDoc;
25 import java.awt.Paint JavaDoc;
26 import java.awt.Shape JavaDoc;
27 import java.awt.geom.AffineTransform JavaDoc;
28 import java.awt.geom.Area JavaDoc;
29 import java.awt.geom.GeneralPath JavaDoc;
30
31 import org.apache.fop.fo.Constants;
32 import org.apache.fop.fonts.FontInfo;
33
34 /**
35  * Keeps information about the current state of the Graphics2D currentGraphics.
36  * It is also used as a stack to hold a graphics context.
37  * <p>
38  * The graphics context is updated with the updateXXX() methods.
39  */

40 public class Java2DGraphicsState {
41
42     /** Holds the datas of the current state */
43     private Graphics2D JavaDoc currentGraphics;
44
45     private BasicStroke JavaDoc currentStroke;
46
47     private float currentStrokeWidth;
48
49     private int currentStrokeStyle;
50
51     /** Font configuration, passed from AWTRenderer */
52     private FontInfo fontInfo;
53
54     /** Initial AffinTransform passed by the renderer, includes scaling-info */
55     private AffineTransform JavaDoc initialTransform;
56
57     /**
58      * State for storing graphics state.
59      * @param graphics the graphics associated with the BufferedImage
60      * @param fontInfo the FontInfo from the renderer
61      * @param at the initial AffineTransform containing the scale transformation
62      */

63     public Java2DGraphicsState(Graphics2D JavaDoc graphics, FontInfo fontInfo,
64             AffineTransform JavaDoc at) {
65         this.fontInfo = fontInfo;
66         this.currentGraphics = graphics;
67         this.initialTransform = at;
68         currentGraphics.setTransform(at);
69     }
70
71     /**
72      * Copy constructor.
73      * @param org the instance to copy
74      */

75     public Java2DGraphicsState(Java2DGraphicsState org) {
76         this.currentGraphics = (Graphics2D JavaDoc)org.currentGraphics.create();
77         this.fontInfo = org.fontInfo;
78         this.initialTransform = org.initialTransform;
79         this.currentStroke = org.currentStroke;
80         this.currentStrokeStyle = org.currentStrokeStyle;
81         this.currentStrokeWidth = org.currentStrokeWidth;
82     }
83     
84     /**
85      * @return the currently valid state
86      */

87     public Graphics2D JavaDoc getGraph() {
88         return currentGraphics;
89     }
90
91     /** Frees resources allocated by the current Graphics2D instance. */
92     public void dispose() {
93         this.currentGraphics.dispose();
94         this.currentGraphics = null;
95         
96     }
97
98     /**
99      * Set the current background color. Check if the background color will
100      * change and then set the new color.
101      *
102      * @param col the new color as a java.awt.Color
103      * @return true if the background color has changed
104      */

105     public boolean updateColor(Color JavaDoc col) {
106         if (!col.equals(getGraph().getColor())) {
107             getGraph().setColor(col);
108             return true;
109         } else {
110             return false;
111         }
112     }
113
114     /**
115      * @return the current java.awt.Color
116      */

117     public java.awt.Color JavaDoc getColor() {
118         return currentGraphics.getColor();
119     }
120
121     /**
122      * Set the current font name. Check if the font name will change and then
123      * set the new name.
124      *
125      * @param name the new font name
126      * @param size the font size
127      * @return true if the new Font changes the current Font
128      */

129     public boolean updateFont(String JavaDoc name, int size) {
130
131         FontMetricsMapper mapper = (FontMetricsMapper)fontInfo.getMetricsFor(name);
132         boolean updateName = (!mapper.getFontName().equals(
133                                     getGraph().getFont().getFontName()));
134         boolean updateSize = (size != (getGraph().getFont().getSize() * 1000));
135
136         if (updateName || updateSize) {
137             // the font name and/or the font size have changed
138
java.awt.Font JavaDoc font = mapper.getFont(size);
139
140             currentGraphics.setFont(font);
141             return true;
142         } else {
143             return false;
144         }
145     }
146
147     /** @return the current java.awt.Font */
148     public java.awt.Font JavaDoc getFont() {
149         return currentGraphics.getFont();
150     }
151
152     /**
153      * Sets the current Stroke. The line width should be set with
154      * updateLineWidth() before calling this method
155      *
156      * @param width the line width
157      * @param style the constant for the style of the line as an int
158      * @return true if the new Stroke changes the current Stroke
159      */

160     public boolean updateStroke(float width, int style) {
161
162         boolean update = false;
163
164         // only update if necessary
165
if ((width != currentStrokeWidth) || (style != currentStrokeStyle)) {
166
167             update = true;
168
169             switch (style) {
170             case Constants.EN_DOTTED:
171
172                 currentStroke = new BasicStroke JavaDoc(width, BasicStroke.CAP_ROUND,
173                         BasicStroke.JOIN_BEVEL, 0f, new float[] {0, 2 * width}, width);
174                 currentGraphics.setStroke(currentStroke);
175
176                 currentStrokeWidth = width;
177                 currentStrokeStyle = style;
178
179                 break;
180
181             case Constants.EN_DASHED:
182
183                 currentStroke = new BasicStroke JavaDoc(width, BasicStroke.CAP_BUTT,
184                         BasicStroke.JOIN_BEVEL, 0f, new float[] {8f, 2f}, 0f);
185                 currentGraphics.setStroke(currentStroke);
186
187                 currentStrokeWidth = width;
188                 currentStrokeStyle = style;
189
190                 break;
191
192             default: // EN_SOLID:
193

194                 currentStroke = new BasicStroke JavaDoc(width);
195                 currentGraphics.setStroke(currentStroke);
196
197                 currentStrokeWidth = width;
198                 currentStrokeStyle = style;
199
200                 break;
201             }
202         }
203
204         return update;
205     }
206
207     /** @return the currently active Stroke */
208     public BasicStroke JavaDoc getStroke() {
209         return (BasicStroke JavaDoc) currentGraphics.getStroke();
210     }
211
212     /**
213      * Set the current paint. This checks if the paint will change and then sets
214      * the current paint.
215      *
216      * @param p the new paint
217      * @return true if the new paint changes the current paint
218      */

219     public boolean updatePaint(Paint JavaDoc p) {
220         if (getGraph().getPaint() == null) {
221             if (p != null) {
222                 getGraph().setPaint(p);
223                 return true;
224             }
225         } else if (p.equals(getGraph().getPaint())) {
226             getGraph().setPaint(p);
227             return true;
228         }
229         return false;
230     }
231
232     /**
233      * Set the current clip. This either sets a new clip or sets the clip to the
234      * intersect of the old clip and the new clip.
235      *
236      * @param cl the new clip in the current state
237      * @return true if the clip shape needed to be updated
238      */

239     public boolean updateClip(Shape JavaDoc cl) {
240         if (getGraph().getClip() != null) {
241             Area JavaDoc newClip = new Area JavaDoc(getGraph().getClip());
242             newClip.intersect(new Area JavaDoc(cl));
243             getGraph().setClip(new GeneralPath JavaDoc(newClip));
244         } else {
245             getGraph().setClip(cl);
246         }
247         return true; // TODO only update if necessary
248
}
249
250     /**
251      * Composes an AffineTransform object with the Transform in this Graphics2D
252      * according to the rule last-specified-first-applied.
253      * @see java.awt.Graphics2D#transform(AffineTransform tf).
254      *
255      * @param tf the transform to concatonate to the current level transform
256      */

257     public void transform(AffineTransform JavaDoc tf) {
258         getGraph().transform(tf);
259     }
260
261     /**
262      * Get the current transform. This gets the combination of all transforms in
263      * the current state.
264      *
265      * @return the calculate combined transform for the current state
266      */

267     public AffineTransform JavaDoc getTransform() {
268         return getGraph().getTransform();
269     }
270
271     /** @see java.lang.Object#toString() */
272     public String JavaDoc toString() {
273         String JavaDoc s = "AWTGraphicsState " + currentGraphics.toString()
274                 + ", Stroke (width: " + currentStrokeWidth + " style: "
275                 + currentStrokeStyle + "), " + getTransform();
276         return s;
277     }
278
279 }
280
Popular Tags