1 /* Copyright (C) 2000, 2003 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 import java
.awt
.Color
;
12 import java
.awt
.Image
;
13 import java
.awt
.Shape
;
14 import java
.awt
.Rectangle
;
15 import java
.awt
.Graphics
;
16 import java
.awt
.Graphics2D
;
17 import java
.awt
.GraphicsConfiguration
;
19 import java
.awt
.FontMetrics
;
20 import java
.awt
.image
.BufferedImage
;
21 import java
.awt
.image
.ImageObserver
;
22 import java
.awt
.image
.Raster
;
23 import java
.awt
.image
.WritableRaster
;
24 import java
.awt
.image
.ColorModel
;
27 * IntegerGraphicsState is one of several graphics state
28 * implementations. This graphics state is used when the graphics
29 * object has simple properties, (coordinate translation only, no
30 * transform) and the backend supports integer coordinates (pixel
31 * based). For primitive paint operations, this object translates the
32 * coordinates and forwards the request to the backend. For requests
33 * to draw arbitrary shapes and paths, this object translates the
34 * requests to primitive drawing operations supported by the
35 * backend. IntegerGraphicsState is meant to support the most common
36 * state of an graphics object. The degree of functionality is roughly
37 * equivalent with the old java.awt.Graphics API.
39 public class IntegerGraphicsState
extends AbstractGraphicsState
44 DirectRasterGraphics directGfx
;
47 /** Interface for images which are coupled to a GraphicsConfiguration,
48 * as is typically the case for an off-screen buffer used in
49 * double-buffering. Any image which implements this interface is
50 * rendered directly by DirectRasterGraphics (i.e. by directGfx.drawImage)
52 public interface ScreenCoupledImage
54 /** Get the GraphicsConfiguration to which this image is coupled
55 * @return the GraphicsConfiguration
57 GraphicsConfiguration
getGraphicsConfiguration ();
60 public IntegerGraphicsState(DirectRasterGraphics directGfx
)
62 this.directGfx
= directGfx
;
67 IntegerGraphicsState clone
= (IntegerGraphicsState
) super.clone();
68 clone
.directGfx
= (DirectRasterGraphics
) directGfx
.clone();
75 DirectRasterGraphics lDeviceGfx
= directGfx
;
79 if (lDeviceGfx
!= null)
85 // -------- Graphics methods:
87 public void setColor(Color color
)
89 directGfx
.setColor(color
);
92 public void setPaintMode()
94 directGfx
.setPaintMode();
97 public void setXORMode(Color altColor
)
99 directGfx
.setXORMode(altColor
);
102 public void setFont(Font font
)
104 directGfx
.setFont(font
);
107 public FontMetrics
getFontMetrics(Font font
)
109 return directGfx
.getFontMetrics(font
);
112 public void setClip(Shape clip
)
114 if (clip
instanceof Rectangle
)
116 Rectangle clipRect
= (Rectangle
) ((Rectangle
) clip
).clone();
120 this.clip
= clipRect
;
122 directGfx
.setClip(clipRect
);
127 "translating clip shape " + clip
+ " into device " +
128 "coordinate space has not been implemented yet";
130 throw new UnsupportedOperationException(msg
);
133 public Shape
getClip()
137 if (clip
instanceof Rectangle
)
139 Rectangle clipRect
= (Rectangle
) ((Rectangle
) clip
).clone();
146 "translating clip shape " + clip
+ " into user " +
147 "coordinate space has not been implemented yet";
149 throw new UnsupportedOperationException(msg
);
152 public Rectangle
getClipBounds()
156 Rectangle clipRect
= clip
.getBounds();
163 public void copyArea(int x
, int y
,
164 int width
, int height
,
167 directGfx
.copyArea(x
+tx
, y
+ty
, width
, height
, dx
, dy
);
170 public void drawLine(int x1
, int y1
,
173 directGfx
.drawLine(x1
+tx
, y1
+ty
, x2
+tx
, y2
+ty
);
176 public void fillRect(int x
, int y
,
177 int width
, int height
)
179 directGfx
.fillRect(x
+tx
, y
+ty
, width
, height
);
182 public void clearRect(int x
, int y
,
183 int width
, int height
)
185 directGfx
.setColor(frontend
.getBackground());
186 directGfx
.fillRect(x
+tx
, y
+ty
, width
, height
);
187 directGfx
.setColor(frontend
.getColor());
190 public void drawRoundRect(int x
, int y
,
191 int width
, int height
,
192 int arcWidth
, int arcHeight
)
194 throw new UnsupportedOperationException("not implemented yet");
197 public void fillRoundRect(int x
, int y
,
198 int width
, int height
,
199 int arcWidth
, int arcHeight
)
201 throw new UnsupportedOperationException("not implemented yet");
204 public void drawOval(int x
, int y
,
205 int width
, int height
)
207 drawArc (x
, y
, width
, height
, 0, 360);
210 public void fillOval(int x
, int y
,
211 int width
, int height
)
213 fillArc (x
, y
, width
, height
, 0, 360);
216 public void drawArc(int x
, int y
,
217 int width
, int height
,
218 int startAngle
, int arcAngle
)
220 directGfx
.drawArc(x
+tx
, y
+ty
, width
, height
, startAngle
, arcAngle
);
223 public void fillArc(int x
, int y
,
224 int width
, int height
,
225 int startAngle
, int arcAngle
)
227 directGfx
.fillArc(x
+tx
, y
+ty
, width
, height
, startAngle
, arcAngle
);
230 public void drawPolyline(int[] xPoints
, int[] yPoints
, int nPoints
)
232 if ((tx
== 0) && (ty
== 0))
234 directGfx
.drawPolyline(xPoints
, yPoints
, nPoints
);
238 throw new UnsupportedOperationException("translate not implemented");
241 public void drawPolygon(int[] xPoints
, int[] yPoints
, int nPoints
)
243 if ((tx
== 0) && (ty
== 0))
245 directGfx
.drawPolygon(xPoints
, yPoints
, nPoints
);
249 throw new UnsupportedOperationException("translate not implemented");
252 public void fillPolygon (int[] xPoints
, int[] yPoints
, int nPoints
)
254 // FIXME: remove tx & ty args once translation via AffineTransform
256 directGfx
.fillPolygon (xPoints
, yPoints
, nPoints
, tx
, ty
);
259 public boolean drawImage(Image image
, int x
, int y
,
260 ImageObserver observer
)
265 if (image
instanceof ScreenCoupledImage
)
267 GraphicsConfiguration config
268 = ((ScreenCoupledImage
)image
).getGraphicsConfiguration ();
269 if (config
== frontend
.config
)
270 return directGfx
.drawImage (image
, x
, y
, observer
);
272 if (image
instanceof BufferedImage
)
274 BufferedImage bImage
= (BufferedImage
) image
;
275 // FIXME: eliminate? ScreenCoupledImage is probably more efficient
276 Object config
= bImage
.getProperty ("java.awt.GraphicsConfiguration");
277 if (config
== frontend
.config
)
278 return directGfx
.drawImage (image
, x
, y
, observer
);
280 int width
= image
.getWidth (null);
281 int height
= image
.getHeight (null);
283 Rectangle bounds
= new Rectangle (x
, y
, width
, height
);
285 MappedRaster mr
= directGfx
.mapRaster (bounds
);
287 // manipulate raster here...
288 ColorModel colorModel
= mr
.getColorModel ();
289 WritableRaster raster
= mr
.getRaster ();
291 int xEnd
= x
+ width
;
292 int yEnd
= y
+ height
;
294 // FIXME: Use the following code only as a fallback. It's SLOW!
296 Object rgbElem
= null;
297 for (int yy
=0; yy
<height
; yy
++)
299 for (int xx
=0; xx
<width
; xx
++)
301 int srgb
= bImage
.getRGB (xx
, yy
);
302 int sa
= ((srgb
>>> 24) & 0xff) + 1;
303 int sr
= ((srgb
>>> 16) & 0xff) + 1;
304 int sg
= ((srgb
>>> 8) & 0xff) + 1;
305 int sb
= (srgb
& 0xff) + 1;
307 rgbElem
= raster
.getDataElements (xx
+x
, yy
+y
, rgbElem
);
308 int drgb
= colorModel
.getRGB (rgbElem
);
309 int dr
= ((drgb
>>> 16) & 0xff) + 1;
310 int dg
= ((drgb
>>> 8) & 0xff) + 1;
311 int db
= (drgb
& 0xff) + 1;
314 dr
= ((sr
*sa
+ dr
*da
) >>> 8) - 1;
315 dg
= ((sg
*sa
+ dg
*da
) >>> 8) - 1;
316 db
= ((sb
*sa
+ db
*da
) >>> 8) - 1;
318 drgb
= (dr
<<16) | (dg
<<8) | db
;
320 rgbElem
= colorModel
.getDataElements (drgb
, rgbElem
);
322 raster
.setDataElements (xx
+x
, yy
+y
, rgbElem
);
325 directGfx
.unmapRaster (mr
);
329 throw new UnsupportedOperationException ("drawing image " + image
+
334 // -------- Graphics2D methods:
336 public void draw(Shape shape
)
338 if (shape
instanceof Rectangle
)
340 Rectangle rect
= (Rectangle
) shape
;
341 directGfx
.drawRect(rect
.x
+tx
, rect
.y
+ty
, rect
.width
, rect
.height
);
345 throw new UnsupportedOperationException("shape not implemented");
348 public void fill(Shape shape
)
350 if (shape
instanceof Rectangle
)
352 Rectangle rect
= (Rectangle
) shape
;
353 directGfx
.fillRect(rect
.x
+tx
, rect
.y
+ty
, rect
.width
, rect
.height
);
357 throw new UnsupportedOperationException("not implemented");
360 public boolean hit(Rectangle rect
, Shape text
,
363 throw new UnsupportedOperationException("not implemented");
366 public void drawString(String text
, int x
, int y
)
368 directGfx
.drawString(text
, x
+tx
, y
+ty
);
371 public void drawString(String text
, float x
, float y
)
373 drawString(text
, (int) x
, (int) y
);
376 public void translate(int x
, int y
)
382 public void translate(double tx
, double ty
)
384 if ((tx
== 0) && (ty
== 0))
387 needAffineTransform();
390 public void rotate(double theta
)
395 needAffineTransform();
398 public void rotate(double theta
, double x
, double y
)
403 needAffineTransform();
406 public void scale(double scaleX
, double scaleY
)
408 if ((scaleX
== 1) && (scaleY
== 1))
411 needAffineTransform();
414 public void shear(double shearX
, double shearY
)
416 if ((shearX
== 0) && (shearY
== 0))
419 needAffineTransform();
422 private void needAffineTransform()
424 throw new UnsupportedOperationException("state with affine " +
425 "transform not implemented");