merge the formfield patch from ooo-build
[ooovba.git] / canvas / source / java / SpriteRep.java
blob74529e6f26802351dc6334a9737928067fa168e5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SpriteRep.java,v $
10 * $Revision: 1.7 $
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 ************************************************************************/
31 // Canvas
32 import com.sun.star.rendering.*;
34 // Java AWT
35 import java.awt.*;
36 import java.awt.geom.*;
38 public class SpriteRep
40 private java.awt.image.BufferedImage buffer;
41 private CanvasBitmap canvasBitmap;
42 private double alpha;
43 private java.awt.geom.Point2D.Double outputPosition;
44 private boolean bufferOwned;
46 //----------------------------------------------------------------------------------
48 // TODO: Everything in this class
49 // TODO: Implement lifetime control for buffer object, which is shared between SpriteReps
50 public SpriteRep()
52 CanvasUtils.printLog( "SpriteRep constructor called!" );
54 alpha = 0.0;
55 outputPosition = new java.awt.geom.Point2D.Double(0.0,0.0);
56 bufferOwned = true; // the buffer member is our own, and has to be disposed
59 public SpriteRep( SpriteRep original )
61 CanvasUtils.printLog( "SpriteRep clone constructor called!" );
63 alpha = 0.0;
64 outputPosition = new java.awt.geom.Point2D.Double(0.0,0.0);
65 cloneBuffer( original );
66 bufferOwned = false; // the buffer member is not our own, and must not be disposed
69 //----------------------------------------------------------------------------------
71 public synchronized void renderAnimation( XAnimation animation, ViewState viewState, double t )
73 if( canvasBitmap != null )
75 // clear buffer with all transparent
76 Graphics2D bitmapGraphics = canvasBitmap.getGraphics();
78 // before that, setup _everything_ we might have changed in CanvasUtils.setupGraphicsState
79 bitmapGraphics.setColor( new Color( 0.0f, 0.0f, 0.0f, 1.0f ) );
80 bitmapGraphics.setComposite(
81 java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.CLEAR));
82 bitmapGraphics.setTransform( new AffineTransform() );
83 bitmapGraphics.setClip( new java.awt.Rectangle(0,0,buffer.getWidth(),buffer.getHeight()) );
84 bitmapGraphics.fillRect(0,0,buffer.getWidth(),buffer.getHeight());
86 try
88 // now push the animation at time instance t into the
89 // virginal graphics
90 animation.render(canvasBitmap, viewState, t);
92 catch( com.sun.star.lang.IllegalArgumentException e )
94 CanvasUtils.printLog( "Cannot create EmbeddedFrame within VCL window hierarchy!" );
99 public synchronized void setSpriteAlpha( double _alpha )
101 CanvasUtils.printLog("SpriteRep.setSpriteAlpha called with alpha=" + alpha);
102 alpha = _alpha;
105 public synchronized void moveSprite( java.awt.geom.Point2D.Double aNewPos )
107 outputPosition = aNewPos;
108 CanvasUtils.printLog( "SpriteRep.moveSprite: moving to (" + outputPosition.x + ", " + outputPosition.y + ")" );
111 public synchronized void redraw( Graphics2D output )
113 if( buffer != null )
115 CanvasUtils.printLog( "SpriteRep.redraw: compositing with alpha=" + alpha );
116 output.setComposite( java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, (float)alpha) );
118 output.drawImage( buffer,
119 (int)(outputPosition.getX() + .5),
120 (int)(outputPosition.getY() + .5),
121 null );
123 //CanvasUtils.postRenderImageTreatment( buffer );
125 CanvasUtils.printLog( "SpriteRep.redraw called, output rect is (" +
126 outputPosition.getX() + ", " +
127 outputPosition.getY() + ", " +
128 buffer.getWidth() + ", " +
129 buffer.getHeight() + ")" );
133 public synchronized void setupBuffer( java.awt.Graphics2D graphics, int width, int height )
135 if( canvasBitmap != null )
136 canvasBitmap.dispose();
138 if( buffer != null )
139 buffer.flush();
141 buffer = graphics.getDeviceConfiguration().createCompatibleImage(Math.max(1,width),
142 Math.max(1,height),
143 Transparency.TRANSLUCENT);
144 canvasBitmap = new CanvasBitmap( buffer );
145 CanvasUtils.initGraphics( canvasBitmap.getGraphics() );
147 CanvasUtils.printLog( "SpriteRep.setupBuffer called, with dimensions (" + width + ", " + height + ")" );
150 public synchronized void cloneBuffer( SpriteRep original )
152 buffer = original.buffer;
155 public synchronized com.sun.star.rendering.XCanvas getContentCanvas()
157 CanvasUtils.printLog( "SpriteRep.getContentCanvas() called" );
159 Graphics2D graphics = canvasBitmap.getGraphics();
160 graphics.setTransform( new AffineTransform() );
161 graphics.setComposite( AlphaComposite.getInstance(AlphaComposite.CLEAR));
162 graphics.fillRect( 0,0,buffer.getWidth(),buffer.getHeight() );
164 return canvasBitmap;
167 public void dispose()
169 if( canvasBitmap != null )
170 canvasBitmap.dispose();
172 if( buffer != null && bufferOwned )
173 buffer.flush();
175 canvasBitmap = null;
176 buffer = null;