1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: BufferedGraphics2D.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.awt
.geom
.*;
34 import java
.awt
.image
.*;
35 import java
.awt
.image
.renderable
.*;
36 import java
.awt
.font
.*;
41 public class BufferedGraphics2D
42 extends java
.awt
.Graphics2D
44 // TODO: Somehow, get rid of this duplicate graphics (the graphics member,
45 // and this object itself, extending a Graphics2D)
46 private Graphics2D graphics
;
47 private BufferedImage backBuffer
;
48 private Graphics2D backBufferGraphics
;
50 //----------------------------------------------------------------------------------
52 public BufferedGraphics2D( java
.awt
.Graphics2D _graphics
, int width
, int height
)
54 setGraphics( _graphics
, Math
.max(1,width
), Math
.max(1,height
) );
57 public void redraw( Graphics2D graph
)
62 CanvasUtils
.printLog("BufferedGraphics2D.redraw: using buffer of size (" +
63 backBuffer
.getWidth() + "," + backBuffer
.getHeight() + ")" );
65 // set transform to identity
66 graph
.setTransform( new AffineTransform() );
67 graph
.drawImage(backBuffer
, 0, 0, null);
68 CanvasUtils
.postRenderImageTreatment( backBuffer
);
72 public BufferedImage
getBackBuffer()
77 public void setSize( int width
, int height
)
79 if( backBuffer
!= null &&
80 width
== backBuffer
.getWidth() &&
81 height
== backBuffer
.getHeight() )
86 if( backBufferGraphics
!= null )
87 backBufferGraphics
.dispose();
89 if( backBuffer
!= null )
92 // TODO: Maybe VolatileImage with another BufferedImage as a backup is
94 backBuffer
= graphics
.getDeviceConfiguration().createCompatibleImage(width
,
97 backBufferGraphics
= backBuffer
.createGraphics();
98 CanvasUtils
.initGraphics( backBufferGraphics
);
100 // clear the buffer to white (to have a defined state here)
101 backBufferGraphics
.setColor( java
.awt
.Color
.white
);
102 backBufferGraphics
.fillRect( 0,0,width
,height
);
105 public void setGraphics( Graphics2D _graphics
, int width
, int height
)
107 if( graphics
!= null )
110 graphics
= _graphics
;
112 setSize(width
,height
);
115 //----------------------------------------------------------------------------------
121 public void clearRect(int x
, int y
, int width
, int height
)
123 graphics
.clearRect(x
,y
,width
,height
);
124 backBufferGraphics
.clearRect(x
,y
,width
,height
);
127 public void clipRect(int x
, int y
, int width
, int height
)
129 graphics
.clipRect(x
,y
,width
,height
);
130 backBufferGraphics
.clipRect(x
,y
,width
,height
);
133 public void copyArea(int x
, int y
, int width
, int height
, int dx
, int dy
)
135 graphics
.copyArea(x
,y
,width
,height
,dx
,dy
);
136 backBufferGraphics
.copyArea(x
,y
,width
,height
,dx
,dy
);
139 public Graphics
create()
144 public Graphics
create(int x
, int y
, int width
, int height
)
149 public void dispose()
152 backBufferGraphics
.dispose();
156 public void draw3DRect(int x
, int y
, int width
, int height
, boolean raised
)
158 graphics
.draw3DRect(x
,y
,width
,height
,raised
);
159 backBufferGraphics
.draw3DRect(x
,y
,width
,height
,raised
);
162 public void drawArc(int x
, int y
, int width
, int height
, int startAngle
, int arcAngle
)
164 graphics
.drawArc(x
,y
,width
,height
,startAngle
,arcAngle
);
165 backBufferGraphics
.drawArc(x
,y
,width
,height
,startAngle
,arcAngle
);
168 public void drawBytes(byte[] data
, int offset
, int length
, int x
, int y
)
170 graphics
.drawBytes(data
,offset
,length
,x
,y
);
171 backBufferGraphics
.drawBytes(data
,offset
,length
,x
,y
);
174 public void drawChars(char[] data
, int offset
, int length
, int x
, int y
)
176 graphics
.drawChars(data
,offset
,length
,x
,y
);
177 backBufferGraphics
.drawChars(data
,offset
,length
,x
,y
);
180 public boolean drawImage(Image img
, int x
, int y
, Color bgcolor
, ImageObserver observer
)
182 backBufferGraphics
.drawImage(img
,x
,y
,bgcolor
,observer
);
183 return graphics
.drawImage(img
,x
,y
,bgcolor
,observer
);
186 public boolean drawImage(Image img
, int x
, int y
, ImageObserver observer
)
188 backBufferGraphics
.drawImage(img
,x
,y
,observer
);
189 return graphics
.drawImage(img
,x
,y
,observer
);
192 public boolean drawImage(Image img
, int x
, int y
, int width
, int height
, Color bgcolor
, ImageObserver observer
)
194 backBufferGraphics
.drawImage(img
,x
,y
,width
,height
,bgcolor
,observer
);
195 return graphics
.drawImage(img
,x
,y
,width
,height
,bgcolor
,observer
);
198 public boolean drawImage(Image img
, int x
, int y
, int width
, int height
, ImageObserver observer
)
200 backBufferGraphics
.drawImage(img
,x
,y
,width
,height
,observer
);
201 return graphics
.drawImage(img
,x
,y
,width
,height
,observer
);
204 public boolean drawImage(Image img
, int dx1
, int dy1
, int dx2
, int dy2
, int sx1
, int sy1
, int sx2
, int sy2
, Color bgcolor
, ImageObserver observer
)
206 backBufferGraphics
.drawImage(img
,dx1
,dy1
,dx2
,dy2
,sx1
,sy1
,sx2
,sy2
,bgcolor
,observer
);
207 return graphics
.drawImage(img
,dx1
,dy1
,dx2
,dy2
,sx1
,sy1
,sx2
,sy2
,bgcolor
,observer
);
210 public boolean drawImage(Image img
, int dx1
, int dy1
, int dx2
, int dy2
, int sx1
, int sy1
, int sx2
, int sy2
, ImageObserver observer
)
212 backBufferGraphics
.drawImage(img
,dx1
,dy1
,dx2
,dy2
,sx1
,sy1
,sx2
,sy2
,observer
);
213 return graphics
.drawImage(img
,dx1
,dy1
,dx2
,dy2
,sx1
,sy1
,sx2
,sy2
,observer
);
216 public void drawLine(int x1
, int y1
, int x2
, int y2
)
218 graphics
.drawLine(x1
,y1
,x2
,y2
);
219 backBufferGraphics
.drawLine(x1
,y1
,x2
,y2
);
222 public void drawOval(int x
, int y
, int width
, int height
)
224 graphics
.drawOval(x
,y
,width
,height
);
225 backBufferGraphics
.drawOval(x
,y
,width
,height
);
228 public void drawPolygon(int[] xPoints
, int[] yPoints
, int nPoints
)
230 graphics
.drawPolygon(xPoints
,yPoints
,nPoints
);
231 backBufferGraphics
.drawPolygon(xPoints
,yPoints
,nPoints
);
234 public void drawPolygon(Polygon p
)
236 graphics
.drawPolygon(p
);
237 backBufferGraphics
.drawPolygon(p
);
240 public void drawPolyline(int[] xPoints
, int[] yPoints
, int nPoints
)
242 graphics
.drawPolyline(xPoints
,yPoints
,nPoints
);
243 backBufferGraphics
.drawPolyline(xPoints
,yPoints
,nPoints
);
246 public void drawRect(int x
, int y
, int width
, int height
)
248 graphics
.drawRect(x
,y
,width
,height
);
249 backBufferGraphics
.drawRect(x
,y
,width
,height
);
252 public void drawRoundRect(int x
, int y
, int width
, int height
, int arcWidth
, int arcHeight
)
254 graphics
.drawRoundRect(x
,y
,width
,height
,arcWidth
,arcHeight
);
255 backBufferGraphics
.drawRoundRect(x
,y
,width
,height
,arcWidth
,arcHeight
);
258 public void drawString(AttributedCharacterIterator iterator
, int x
, int y
)
260 graphics
.drawString(iterator
,x
,y
);
261 backBufferGraphics
.drawString(iterator
,x
,y
);
264 public void drawString(String str
, int x
, int y
)
266 graphics
.drawString(str
,x
,y
);
267 backBufferGraphics
.drawString(str
,x
,y
);
270 public void fill3DRect(int x
, int y
, int width
, int height
, boolean raised
)
272 graphics
.fill3DRect(x
,y
,width
,height
,raised
);
273 backBufferGraphics
.fill3DRect(x
,y
,width
,height
,raised
);
276 public void fillArc(int x
, int y
, int width
, int height
, int startAngle
, int arcAngle
)
278 graphics
.fillArc(x
,y
,width
,height
,startAngle
,arcAngle
);
279 backBufferGraphics
.fillArc(x
,y
,width
,height
,startAngle
,arcAngle
);
282 public void fillOval(int x
, int y
, int width
, int height
)
284 graphics
.fillOval(x
,y
,width
,height
);
285 backBufferGraphics
.fillOval(x
,y
,width
,height
);
288 public void fillPolygon(int[] xPoints
, int[] yPoints
, int nPoints
)
290 graphics
.fillPolygon(xPoints
,yPoints
,nPoints
);
291 backBufferGraphics
.fillPolygon(xPoints
,yPoints
,nPoints
);
294 public void fillPolygon(Polygon p
)
296 graphics
.fillPolygon(p
);
297 backBufferGraphics
.fillPolygon(p
);
300 public void fillRect(int x
, int y
, int width
, int height
)
302 graphics
.fillRect(x
,y
,width
,height
);
303 backBufferGraphics
.fillRect(x
,y
,width
,height
);
306 public void fillRoundRect(int x
, int y
, int width
, int height
, int arcWidth
, int arcHeight
)
308 graphics
.fillRoundRect(x
,y
,width
,height
,arcWidth
,arcHeight
);
309 backBufferGraphics
.fillRoundRect(x
,y
,width
,height
,arcWidth
,arcHeight
);
312 public Shape
getClip()
314 return graphics
.getClip();
317 public Rectangle
getClipBounds()
319 return graphics
.getClipBounds();
322 public Rectangle
getClipBounds(Rectangle r
)
324 return graphics
.getClipBounds(r
);
327 public Rectangle
getClipRect()
329 return graphics
.getClipRect();
332 public Color
getColor()
337 public Font
getFont()
342 public FontMetrics
getFontMetrics()
344 return getFontMetrics();
347 public FontMetrics
getFontMetrics(Font f
)
349 return getFontMetrics(f
);
352 public boolean hitClip(int x
, int y
, int width
, int height
)
354 return graphics
.hitClip(x
,y
,width
,height
);
357 public void setClip(int x
, int y
, int width
, int height
)
359 graphics
.setClip(x
,y
,width
,height
);
360 backBufferGraphics
.setClip(x
,y
,width
,height
);
363 public void setClip(Shape clip
)
365 graphics
.setClip(clip
);
366 backBufferGraphics
.setClip(clip
);
369 public void setColor(Color c
)
371 graphics
.setColor(c
);
372 backBufferGraphics
.setColor(c
);
375 public void setFont(Font font
)
377 graphics
.setFont(font
);
378 backBufferGraphics
.setFont(font
);
381 public void setPaintMode()
383 graphics
.setPaintMode();
384 backBufferGraphics
.setPaintMode();
387 public void setXORMode(Color c1
)
389 graphics
.setXORMode(c1
);
390 backBufferGraphics
.setXORMode(c1
);
393 public String
toString()
395 return graphics
.toString();
398 public void translate(int x
, int y
)
400 graphics
.translate(x
,y
);
401 backBufferGraphics
.translate(x
,y
);
404 //----------------------------------------------------------------------------------
410 public void addRenderingHints(Map hints
)
412 graphics
.addRenderingHints(hints
);
413 backBufferGraphics
.addRenderingHints(hints
);
416 public void clip(Shape s
)
419 backBufferGraphics
.clip(s
);
422 public void draw(Shape s
)
425 backBufferGraphics
.draw(s
);
428 public void drawGlyphVector(GlyphVector g
, float x
, float y
)
430 graphics
.drawGlyphVector(g
,x
,y
);
431 backBufferGraphics
.drawGlyphVector(g
,x
,y
);
434 public void drawImage(BufferedImage img
, BufferedImageOp op
, int x
, int y
)
436 graphics
.drawImage(img
,op
,x
,y
);
437 backBufferGraphics
.drawImage(img
,op
,x
,y
);
440 public boolean drawImage(Image img
, AffineTransform xform
, ImageObserver obs
)
442 backBufferGraphics
.drawImage(img
,xform
,obs
);
443 return graphics
.drawImage(img
,xform
,obs
);
446 public void drawRenderableImage(RenderableImage img
, AffineTransform xform
)
448 graphics
.drawRenderableImage(img
,xform
);
449 backBufferGraphics
.drawRenderableImage(img
,xform
);
452 public void drawRenderedImage(RenderedImage img
, AffineTransform xform
)
454 graphics
.drawRenderedImage(img
,xform
);
455 backBufferGraphics
.drawRenderedImage(img
,xform
);
458 public void drawString(AttributedCharacterIterator iterator
, float x
, float y
)
460 graphics
.drawString(iterator
,x
,y
);
461 backBufferGraphics
.drawString(iterator
,x
,y
);
464 public void drawString(String s
, float x
, float y
)
466 graphics
.drawString(s
,x
,y
);
467 backBufferGraphics
.drawString(s
,x
,y
);
470 public void fill(Shape s
)
473 backBufferGraphics
.fill(s
);
476 public Color
getBackground()
478 return graphics
.getBackground();
481 public Composite
getComposite()
483 return graphics
.getComposite();
486 public GraphicsConfiguration
getDeviceConfiguration()
488 return graphics
.getDeviceConfiguration();
491 public FontRenderContext
getFontRenderContext()
493 return graphics
.getFontRenderContext();
496 public Paint
getPaint()
498 return graphics
.getPaint();
501 public Object
getRenderingHint(RenderingHints
.Key hintKey
)
503 return graphics
.getRenderingHint(hintKey
);
506 public RenderingHints
getRenderingHints()
508 return graphics
.getRenderingHints();
511 public Stroke
getStroke()
513 return graphics
.getStroke();
516 public AffineTransform
getTransform()
518 return graphics
.getTransform();
521 public boolean hit(Rectangle rect
, Shape s
, boolean onStroke
)
523 return graphics
.hit(rect
,s
,onStroke
);
526 public void rotate(double theta
)
528 graphics
.rotate(theta
);
529 backBufferGraphics
.rotate(theta
);
532 public void rotate(double theta
, double x
, double y
)
534 graphics
.rotate(theta
,x
,y
);
535 backBufferGraphics
.rotate(theta
,x
,y
);
538 public void scale(double sx
, double sy
)
540 graphics
.scale(sx
,sy
);
541 backBufferGraphics
.scale(sx
,sy
);
544 public void setBackground(Color color
)
546 graphics
.setBackground(color
);
547 backBufferGraphics
.setBackground(color
);
550 public void setComposite(Composite comp
)
552 graphics
.setComposite(comp
);
553 backBufferGraphics
.setComposite(comp
);
556 public void setPaint(Paint paint
)
558 graphics
.setPaint(paint
);
559 backBufferGraphics
.setPaint(paint
);
562 public void setRenderingHint(RenderingHints
.Key hintKey
, Object hintValue
)
564 graphics
.setRenderingHint(hintKey
,hintValue
);
565 backBufferGraphics
.setRenderingHint(hintKey
,hintValue
);
568 public void setRenderingHints(Map hints
)
570 graphics
.setRenderingHints(hints
);
571 backBufferGraphics
.setRenderingHints(hints
);
574 public void setStroke(Stroke s
)
576 graphics
.setStroke(s
);
577 backBufferGraphics
.setStroke(s
);
580 public void setTransform(AffineTransform Tx
)
582 graphics
.setTransform(Tx
);
583 backBufferGraphics
.setTransform(Tx
);
586 public void shear(double shx
, double shy
)
588 graphics
.shear(shx
,shy
);
589 backBufferGraphics
.shear(shx
,shy
);
592 public void transform(AffineTransform Tx
)
594 graphics
.transform(Tx
);
595 backBufferGraphics
.transform(Tx
);
598 public void translate(double tx
, double ty
)
600 graphics
.translate(tx
,ty
);
601 backBufferGraphics
.translate(tx
,ty
);