KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > EscherGraphics2d


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

16
17 package org.apache.poi.hssf.usermodel;
18
19 import org.apache.poi.util.POILogFactory;
20 import org.apache.poi.util.POILogger;
21
22 import java.awt.*;
23 import java.awt.font.FontRenderContext JavaDoc;
24 import java.awt.font.GlyphVector JavaDoc;
25 import java.awt.font.TextLayout JavaDoc;
26 import java.awt.geom.AffineTransform JavaDoc;
27 import java.awt.geom.Area JavaDoc;
28 import java.awt.geom.GeneralPath JavaDoc;
29 import java.awt.image.BufferedImage JavaDoc;
30 import java.awt.image.BufferedImageOp JavaDoc;
31 import java.awt.image.ImageObserver JavaDoc;
32 import java.awt.image.RenderedImage JavaDoc;
33 import java.awt.image.renderable.RenderableImage JavaDoc;
34 import java.text.AttributedCharacterIterator JavaDoc;
35 import java.util.Map JavaDoc;
36
37 /**
38  * Translates Graphics2d calls into escher calls. The translation is lossy so
39  * many features are not supported and some just aren't implemented yet. If
40  * in doubt test the specific calls you wish to make. Graphics calls are
41  * always drawn into an EscherGroup so one will need to be created.
42  * <p>
43  * <b>Important:</b>
44  * <blockquote>
45  * One important concept worth considering is that of font size. One of the
46  * difficulties in converting Graphics calls into escher drawing calls is that
47  * Excel does not have the concept of absolute pixel positions. It measures
48  * it's cell widths in 'characters' and the cell heights in points.
49  * Unfortunately it's not defined exactly what a type of character it's
50  * measuring. Presumably this is due to the fact that the Excel will be
51  * using different fonts on different platforms or even within the same
52  * platform.
53  * <p>
54  * Because of this constraint you have to calculate the verticalPointsPerPixel.
55  * This the amount the font should be scaled by when
56  * you issue commands such as drawString(). A good way to calculate this
57  * is to use the follow formula:
58  * <p>
59  * <pre>
60  * multipler = groupHeightInPoints / heightOfGroup
61  * </pre>
62  * <p>
63  * The height of the group is calculated fairly simply by calculating the
64  * difference between the y coordinates of the bounding box of the shape. The
65  * height of the group can be calculated by using a convenience called
66  * <code>HSSFClientAnchor.getAnchorHeightInPoints()</code>.
67  * </blockquote>
68  *
69  * @author Glen Stampoultzis (glens at apache.org)
70  */

71 public class EscherGraphics2d extends Graphics2D
72 {
73     private EscherGraphics escherGraphics;
74     private BufferedImage JavaDoc img;
75     private AffineTransform JavaDoc trans;
76     private Stroke stroke;
77     private Paint paint;
78     private Shape deviceclip;
79     private POILogger logger = POILogFactory.getLogger(getClass());
80
81     /**
82      * Constructs one escher graphics object from an escher graphics object.
83      *
84      * @param escherGraphics the original EscherGraphics2d object to copy
85      */

86     public EscherGraphics2d(EscherGraphics escherGraphics)
87     {
88         this.escherGraphics = escherGraphics;
89         setImg( new BufferedImage JavaDoc(1, 1, 2) );
90         setColor(Color.black);
91     }
92
93     public void addRenderingHints(Map JavaDoc map)
94     {
95         getG2D().addRenderingHints(map);
96     }
97
98     public void clearRect(int i, int j, int k, int l)
99     {
100         Paint paint1 = getPaint();
101         setColor(getBackground());
102         fillRect(i, j, k, l);
103         setPaint(paint1);
104     }
105
106     public void clip(Shape shape)
107     {
108         if(getDeviceclip() != null)
109         {
110             Area JavaDoc area = new Area JavaDoc(getClip());
111             if(shape != null)
112                 area.intersect(new Area JavaDoc(shape));
113             shape = area;
114         }
115         setClip(shape);
116     }
117
118     public void clipRect(int x, int y, int width, int height)
119     {
120         clip(new Rectangle(x,y,width,height));
121     }
122
123     public void copyArea(int x, int y, int width, int height,
124                   int dx, int dy)
125     {
126         getG2D().copyArea(x,y,width,height,dx,dy);
127     }
128
129     public Graphics create()
130     {
131         EscherGraphics2d g2d = new EscherGraphics2d(escherGraphics);
132         return g2d;
133     }
134
135     public void dispose()
136     {
137         getEscherGraphics().dispose();
138         getG2D().dispose();
139         getImg().flush();
140     }
141
142     public void draw(Shape shape)
143     {
144         if (logger.check( POILogger.WARN ))
145             logger.log(POILogger.WARN,"copyArea not supported");
146     }
147
148     public void drawArc(int x, int y, int width, int height,
149                  int startAngle, int arcAngle)
150     {
151         draw(new java.awt.geom.Arc2D.Float(x, y, width, height, startAngle, arcAngle, 0));
152     }
153
154     public void drawGlyphVector(GlyphVector JavaDoc g, float x, float y)
155     {
156         fill(g.getOutline(x, y));
157     }
158
159     public boolean drawImage(Image JavaDoc image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
160             int sx2, int sy2, Color bgColor, ImageObserver JavaDoc imageobserver)
161     {
162         if (logger.check( POILogger.WARN ))
163             logger.log(POILogger.WARN,"drawImage() not supported");
164         return true;
165     }
166
167     public boolean drawImage(Image JavaDoc image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
168             int sx2, int sy2, ImageObserver JavaDoc imageobserver)
169     {
170         if (logger.check( POILogger.WARN ))
171             logger.log(POILogger.WARN,"drawImage() not supported");
172         return drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null, imageobserver);
173     }
174     public boolean drawImage(Image JavaDoc image, int dx1, int dy1, int dx2, int dy2, Color bgColor, ImageObserver JavaDoc imageobserver)
175     {
176         if (logger.check( POILogger.WARN ))
177             logger.log(POILogger.WARN,"drawImage() not supported");
178         return true;
179     }
180
181     public boolean drawImage(Image JavaDoc img, int x, int y,
182                       int width, int height,
183                       ImageObserver JavaDoc observer)
184     {
185         return drawImage(img, x,y,width,height, null, observer);
186     }
187
188     public boolean drawImage(Image JavaDoc image, int x, int y, Color bgColor, ImageObserver JavaDoc imageobserver)
189     {
190         return drawImage(image, x, y, image.getWidth(imageobserver), image.getHeight(imageobserver), bgColor, imageobserver);
191     }
192
193     public boolean drawImage(Image JavaDoc image, int x, int y, ImageObserver JavaDoc imageobserver)
194     {
195         return drawImage(image, x, y, image.getWidth(imageobserver), image.getHeight(imageobserver), imageobserver);
196     }
197
198     public boolean drawImage(Image JavaDoc image, AffineTransform JavaDoc affinetransform, ImageObserver JavaDoc imageobserver)
199     {
200         AffineTransform JavaDoc affinetransform1 = (AffineTransform JavaDoc)getTrans().clone();
201         getTrans().concatenate(affinetransform);
202         drawImage(image, 0, 0, imageobserver);
203         setTrans( affinetransform1 );
204         return true;
205     }
206
207     public void drawImage(BufferedImage JavaDoc bufferedimage, BufferedImageOp JavaDoc op, int x, int y)
208     {
209         BufferedImage JavaDoc img = op.filter(bufferedimage, null);
210         drawImage(((Image JavaDoc) (img)), new AffineTransform JavaDoc(1.0F, 0.0F, 0.0F, 1.0F, x, y), null);
211     }
212
213     public void drawLine(int x1, int y1, int x2, int y2)
214     {
215         getEscherGraphics().drawLine(x1,y1,x2,y2);
216 // draw(new GeneralPath(new java.awt.geom.Line2D.Float(x1, y1, x2, y2)));
217
}
218
219     public void drawOval(int x, int y, int width, int height)
220     {
221         getEscherGraphics().drawOval(x,y,width,height);
222 // draw(new java.awt.geom.Ellipse2D.Float(x, y, width, height));
223
}
224
225     public void drawPolygon(int xPoints[], int yPoints[],
226                      int nPoints)
227     {
228         getEscherGraphics().drawPolygon(xPoints, yPoints, nPoints);
229     }
230
231     public void drawPolyline(int xPoints[], int yPoints[], int nPoints)
232     {
233         if(nPoints > 0)
234         {
235             GeneralPath JavaDoc generalpath = new GeneralPath JavaDoc();
236             generalpath.moveTo(xPoints[0], yPoints[0]);
237             for(int j = 1; j < nPoints; j++)
238                 generalpath.lineTo(xPoints[j], yPoints[j]);
239
240             draw(generalpath);
241         }
242     }
243
244     public void drawRect(int x, int y, int width, int height)
245     {
246         escherGraphics.drawRect(x,y,width,height);
247     }
248
249     public void drawRenderableImage(RenderableImage JavaDoc renderableimage, AffineTransform JavaDoc affinetransform)
250     {
251         drawRenderedImage(renderableimage.createDefaultRendering(), affinetransform);
252     }
253
254     public void drawRenderedImage(RenderedImage JavaDoc renderedimage, AffineTransform JavaDoc affinetransform)
255     {
256         BufferedImage JavaDoc bufferedimage = new BufferedImage JavaDoc(renderedimage.getColorModel(), renderedimage.getData().createCompatibleWritableRaster(), false, null);
257         bufferedimage.setData(renderedimage.getData());
258         drawImage(bufferedimage, affinetransform, null);
259     }
260
261     public void drawRoundRect(int i, int j, int k, int l, int i1, int j1)
262     {
263         draw(new java.awt.geom.RoundRectangle2D.Float(i, j, k, l, i1, j1));
264     }
265
266     public void drawString(String JavaDoc string, float x, float y)
267     {
268         getEscherGraphics().drawString(string, (int)x, (int)y);
269     }
270
271     public void drawString(String JavaDoc string, int x, int y)
272     {
273         getEscherGraphics().drawString(string, x, y);
274     }
275
276     public void drawString(AttributedCharacterIterator JavaDoc attributedcharacteriterator, float x, float y)
277     {
278         TextLayout JavaDoc textlayout = new TextLayout JavaDoc(attributedcharacteriterator, getFontRenderContext());
279         Paint paint1 = getPaint();
280         setColor(getColor());
281         fill(textlayout.getOutline(AffineTransform.getTranslateInstance(x, y)));
282         setPaint(paint1);
283     }
284
285     public void drawString(AttributedCharacterIterator JavaDoc attributedcharacteriterator, int x, int y)
286     {
287         drawString(attributedcharacteriterator, x, y);
288     }
289
290     public void fill(Shape shape)
291     {
292         if (logger.check( POILogger.WARN ))
293             logger.log(POILogger.WARN,"fill(Shape) not supported");
294     }
295
296     public void fillArc(int i, int j, int k, int l, int i1, int j1)
297     {
298         fill(new java.awt.geom.Arc2D.Float(i, j, k, l, i1, j1, 2));
299     }
300
301     public void fillOval(int x, int y, int width, int height)
302     {
303         escherGraphics.fillOval(x,y,width,height);
304     }
305
306     /**
307      * Fills a closed polygon defined by
308      * arrays of <i>x</i> and <i>y</i> coordinates.
309      * <p>
310      * This method draws the polygon defined by <code>nPoint</code> line
311      * segments, where the first <code>nPoint&nbsp;-&nbsp;1</code>
312      * line segments are line segments from
313      * <code>(xPoints[i&nbsp;-&nbsp;1],&nbsp;yPoints[i&nbsp;-&nbsp;1])</code>
314      * to <code>(xPoints[i],&nbsp;yPoints[i])</code>, for
315      * 1&nbsp;&le;&nbsp;<i>i</i>&nbsp;&le;&nbsp;<code>nPoints</code>.
316      * The figure is automatically closed by drawing a line connecting
317      * the final point to the first point, if those points are different.
318      * <p>
319      * The area inside the polygon is defined using an
320      * even-odd fill rule, also known as the alternating rule.
321      * @param xPoints a an array of <code>x</code> coordinates.
322      * @param yPoints a an array of <code>y</code> coordinates.
323      * @param nPoints a the total number of points.
324      * @see java.awt.Graphics#drawPolygon(int[], int[], int)
325      */

326     public void fillPolygon(int xPoints[], int yPoints[], int nPoints)
327     {
328         escherGraphics.fillPolygon(xPoints, yPoints, nPoints);
329     }
330
331     public void fillRect(int x, int y, int width, int height)
332     {
333         getEscherGraphics().fillRect(x,y,width,height);
334     }
335
336     public void fillRoundRect(int x, int y, int width, int height,
337                        int arcWidth, int arcHeight)
338     {
339         fill(new java.awt.geom.RoundRectangle2D.Float(x, y, width, height, arcWidth, arcHeight));
340     }
341
342     public Color getBackground()
343     {
344         return getEscherGraphics().getBackground();
345     }
346
347     public Shape getClip()
348     {
349         try
350         {
351             return getTrans().createInverse().createTransformedShape(getDeviceclip());
352         }
353         catch(Exception JavaDoc _ex)
354         {
355             return null;
356         }
357     }
358
359     public Rectangle getClipBounds()
360     {
361         if(getDeviceclip() != null)
362             return getClip().getBounds();
363         else
364             return null;
365     }
366
367     public Color getColor()
368     {
369         return escherGraphics.getColor();
370     }
371
372     public Composite getComposite()
373     {
374         return getG2D().getComposite();
375     }
376
377     public GraphicsConfiguration getDeviceConfiguration()
378     {
379         return getG2D().getDeviceConfiguration();
380     }
381
382     public Font getFont()
383     {
384         return getEscherGraphics().getFont();
385     }
386
387     public FontMetrics getFontMetrics(Font font)
388     {
389         return getEscherGraphics().getFontMetrics(font);
390     }
391
392     public FontRenderContext JavaDoc getFontRenderContext()
393     {
394         getG2D().setTransform(getTrans());
395         return getG2D().getFontRenderContext();
396     }
397
398     public Paint getPaint()
399     {
400         return paint;
401     }
402
403     public Object JavaDoc getRenderingHint(java.awt.RenderingHints.Key key)
404     {
405         return getG2D().getRenderingHint(key);
406     }
407
408     public RenderingHints getRenderingHints()
409     {
410         return getG2D().getRenderingHints();
411     }
412
413     public Stroke getStroke()
414     {
415         return stroke;
416     }
417
418     public AffineTransform JavaDoc getTransform()
419     {
420         return (AffineTransform JavaDoc)getTrans().clone();
421     }
422
423     public boolean hit(Rectangle rectangle, Shape shape, boolean flag)
424     {
425         getG2D().setTransform(getTrans());
426         getG2D().setStroke(getStroke());
427         getG2D().setClip(getClip());
428         return getG2D().hit(rectangle, shape, flag);
429     }
430
431     public void rotate(double d)
432     {
433         getTrans().rotate(d);
434     }
435
436     public void rotate(double d, double d1, double d2)
437     {
438         getTrans().rotate(d, d1, d2);
439     }
440
441     public void scale(double d, double d1)
442     {
443         getTrans().scale(d, d1);
444     }
445
446     public void setBackground(Color c)
447     {
448         getEscherGraphics().setBackground(c);
449     }
450
451     public void setClip(int i, int j, int k, int l)
452     {
453         setClip(((Shape) (new Rectangle(i, j, k, l))));
454     }
455
456     public void setClip(Shape shape)
457     {
458         setDeviceclip( getTrans().createTransformedShape(shape) );
459     }
460
461     public void setColor(Color c)
462     {
463         escherGraphics.setColor(c);
464     }
465
466     public void setComposite(Composite composite)
467     {
468         getG2D().setComposite(composite);
469     }
470
471     public void setFont(Font font)
472     {
473         getEscherGraphics().setFont(font);
474     }
475
476     public void setPaint(Paint paint1)
477     {
478         if(paint1 != null)
479         {
480             paint = paint1;
481             if(paint1 instanceof Color)
482                 setColor( (Color)paint1 );
483         }
484     }
485
486     public void setPaintMode()
487     {
488         getEscherGraphics().setPaintMode();
489     }
490
491     public void setRenderingHint(java.awt.RenderingHints.Key key, Object JavaDoc obj)
492     {
493         getG2D().setRenderingHint(key, obj);
494     }
495
496     public void setRenderingHints(Map JavaDoc map)
497     {
498         getG2D().setRenderingHints(map);
499     }
500
501     public void setStroke(Stroke s)
502     {
503         stroke = s;
504     }
505
506     public void setTransform(AffineTransform JavaDoc affinetransform)
507     {
508         setTrans( (AffineTransform JavaDoc)affinetransform.clone() );
509     }
510
511     public void setXORMode(Color color1)
512     {
513         getEscherGraphics().setXORMode(color1);
514     }
515
516     public void shear(double d, double d1)
517     {
518         getTrans().shear(d, d1);
519     }
520
521     public void transform(AffineTransform JavaDoc affinetransform)
522     {
523         getTrans().concatenate(affinetransform);
524     }
525
526 // Image transformImage(Image image, Rectangle rectangle, Rectangle rectangle1, ImageObserver imageobserver, Color color1)
527
// {
528
// logger.log(POILogger.WARN,"transformImage() not supported");
529
// return null;
530
// }
531
//
532
// Image transformImage(Image image, int ai[], Rectangle rectangle, ImageObserver imageobserver, Color color1)
533
// {
534
// logger.log(POILogger.WARN,"transformImage() not supported");
535
// return null;
536
// }
537

538     public void translate(double d, double d1)
539     {
540         getTrans().translate(d, d1);
541     }
542
543     public void translate(int i, int j)
544     {
545         getTrans().translate(i, j);
546     }
547
548     private EscherGraphics getEscherGraphics()
549     {
550         return escherGraphics;
551     }
552
553     private BufferedImage JavaDoc getImg()
554     {
555         return img;
556     }
557
558     private void setImg( BufferedImage JavaDoc img )
559     {
560         this.img = img;
561     }
562
563     private Graphics2D getG2D()
564     {
565         return (Graphics2D) img.getGraphics();
566     }
567
568     private AffineTransform JavaDoc getTrans()
569     {
570         return trans;
571     }
572
573     private void setTrans( AffineTransform JavaDoc trans )
574     {
575         this.trans = trans;
576     }
577
578     private Shape getDeviceclip()
579     {
580         return deviceclip;
581     }
582
583     private void setDeviceclip( Shape deviceclip )
584     {
585         this.deviceclip = deviceclip;
586     }
587
588 }
589
Popular Tags