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 public IntegerGraphicsState(DirectRasterGraphics directGfx
)
49 this.directGfx
= directGfx
;
54 IntegerGraphicsState clone
= (IntegerGraphicsState
) super.clone();
55 clone
.directGfx
= (DirectRasterGraphics
) directGfx
.clone();
62 DirectRasterGraphics lDeviceGfx
= directGfx
;
66 if (lDeviceGfx
!= null)
72 // -------- Graphics methods:
74 public void setColor(Color color
)
76 directGfx
.setColor(color
);
79 public void setPaintMode()
81 directGfx
.setPaintMode();
84 public void setXORMode(Color altColor
)
86 directGfx
.setXORMode(altColor
);
89 public void setFont(Font font
)
91 directGfx
.setFont(font
);
94 public FontMetrics
getFontMetrics(Font font
)
96 return directGfx
.getFontMetrics(font
);
99 public void setClip(Shape clip
)
101 if (clip
instanceof Rectangle
)
103 Rectangle clipRect
= (Rectangle
) ((Rectangle
) clip
).clone();
107 this.clip
= clipRect
;
109 directGfx
.setClip(clipRect
);
114 "translating clip shape " + clip
+ " into device " +
115 "coordinate space has not been implemented yet";
117 throw new UnsupportedOperationException(msg
);
120 public Shape
getClip()
122 if (clip
instanceof Rectangle
)
124 Rectangle clipRect
= (Rectangle
) clip
;
131 "translating clip shape " + clip
+ " into user " +
132 "coordinate space has not been implemented yet";
134 throw new UnsupportedOperationException(msg
);
137 public Rectangle
getClipBounds()
139 Rectangle clipRect
= clip
.getBounds();
146 public void copyArea(int x
, int y
,
147 int width
, int height
,
150 directGfx
.copyArea(x
+tx
, y
+ty
, width
, height
, dx
, dy
);
153 public void drawLine(int x1
, int y1
,
156 directGfx
.drawLine(x1
+tx
, y1
+ty
, x2
+tx
, y2
+ty
);
159 public void fillRect(int x
, int y
,
160 int width
, int height
)
162 directGfx
.fillRect(x
+tx
, y
+ty
, width
, height
);
165 public void clearRect(int x
, int y
,
166 int width
, int height
)
168 directGfx
.setColor(frontend
.getBackground());
169 directGfx
.fillRect(x
+tx
, y
+ty
, width
, height
);
170 directGfx
.setColor(frontend
.getColor());
173 public void drawRoundRect(int x
, int y
,
174 int width
, int height
,
175 int arcWidth
, int arcHeight
)
177 throw new UnsupportedOperationException("not implemented yet");
180 public void fillRoundRect(int x
, int y
,
181 int width
, int height
,
182 int arcWidth
, int arcHeight
)
184 throw new UnsupportedOperationException("not implemented yet");
187 public void drawOval(int x
, int y
,
188 int width
, int height
)
190 throw new UnsupportedOperationException("not implemented yet");
193 public void fillOval(int x
, int y
,
194 int width
, int height
)
196 throw new UnsupportedOperationException("not implemented yet");
199 public void drawArc(int x
, int y
,
200 int width
, int height
,
201 int startAngle
, int arcAngle
)
203 directGfx
.drawArc(x
+tx
, y
+ty
, width
, height
, startAngle
, arcAngle
);
206 public void fillArc(int x
, int y
,
207 int width
, int height
,
208 int startAngle
, int arcAngle
)
210 directGfx
.fillArc(x
+tx
, y
+ty
, width
, height
, startAngle
, arcAngle
);
213 public void drawPolyline(int[] xPoints
, int[] yPoints
, int nPoints
)
215 if ((tx
== 0) && (ty
== 0))
217 directGfx
.drawPolyline(xPoints
, yPoints
, nPoints
);
221 throw new UnsupportedOperationException("translate not implemented");
224 public void drawPolygon(int[] xPoints
, int[] yPoints
, int nPoints
)
226 if ((tx
== 0) && (ty
== 0))
228 directGfx
.drawPolygon(xPoints
, yPoints
, nPoints
);
232 throw new UnsupportedOperationException("translate not implemented");
235 public void fillPolygon (int[] xPoints
, int[] yPoints
, int nPoints
)
237 // FIXME: remove tx & ty args once translation via AffineTransform
239 directGfx
.fillPolygon (xPoints
, yPoints
, nPoints
, tx
, ty
);
242 public boolean drawImage(Image image
, int x
, int y
,
243 ImageObserver observer
)
248 if (image
instanceof BufferedImage
)
250 BufferedImage bImage
= (BufferedImage
) image
;
252 bImage
.getProperty("java.awt.GraphicsConfiguration");
254 if (config
== frontend
.config
)
255 return directGfx
.drawImage(image
, x
, y
, observer
);
257 int width
= image
.getWidth(null);
258 int height
= image
.getHeight(null);
260 Rectangle bounds
= new Rectangle(x
, y
, width
, height
);
262 MappedRaster mr
= directGfx
.mapRaster(bounds
);
264 // manipulate raster here...
265 ColorModel colorModel
= mr
.getColorModel();
266 WritableRaster raster
= mr
.getRaster();
268 int xEnd
= x
+ width
;
269 int yEnd
= y
+ height
;
271 // FIXME: Use the following code only as a fallback. It's SLOW!
273 Object rgbElem
= null;
274 for (int yy
=0; yy
<height
; yy
++)
276 for (int xx
=0; xx
<width
; xx
++)
278 int srgb
= bImage
.getRGB(xx
, yy
);
279 int sa
= ((srgb
>>> 24) & 0xff) + 1;
280 int sr
= ((srgb
>>> 16) & 0xff) + 1;
281 int sg
= ((srgb
>>> 8) & 0xff) + 1;
282 int sb
= (srgb
& 0xff) + 1;
284 rgbElem
= raster
.getDataElements(xx
+x
, yy
+y
, rgbElem
);
285 int drgb
= colorModel
.getRGB(rgbElem
);
286 int dr
= ((drgb
>>> 16) & 0xff) + 1;
287 int dg
= ((drgb
>>> 8) & 0xff) + 1;
288 int db
= (drgb
& 0xff) + 1;
291 dr
= ((sr
*sa
+ dr
*da
) >>> 8) - 1;
292 dg
= ((sg
*sa
+ dg
*da
) >>> 8) - 1;
293 db
= ((sb
*sa
+ db
*da
) >>> 8) - 1;
295 drgb
= (dr
<<16) | (dg
<<8) | db
;
297 rgbElem
= colorModel
.getDataElements(drgb
, rgbElem
);
299 raster
.setDataElements(xx
+x
, yy
+y
, rgbElem
);
302 directGfx
.unmapRaster(mr
);
306 throw new UnsupportedOperationException("drawing image " + image
+
311 // -------- Graphics2D methods:
313 public void draw(Shape shape
)
315 if (shape
instanceof Rectangle
)
317 Rectangle rect
= (Rectangle
) shape
;
318 directGfx
.drawRect(rect
.x
+tx
, rect
.y
+ty
, rect
.width
, rect
.height
);
322 throw new UnsupportedOperationException("shape not implemented");
325 public void fill(Shape shape
)
327 if (shape
instanceof Rectangle
)
329 Rectangle rect
= (Rectangle
) shape
;
330 directGfx
.fillRect(rect
.x
+tx
, rect
.y
+ty
, rect
.width
, rect
.height
);
334 throw new UnsupportedOperationException("not implemented");
337 public boolean hit(Rectangle rect
, Shape text
,
340 throw new UnsupportedOperationException("not implemented");
343 public void drawString(String text
, int x
, int y
)
345 directGfx
.drawString(text
, x
+tx
, y
+ty
);
348 public void drawString(String text
, float x
, float y
)
350 drawString(text
, (int) x
, (int) y
);
353 public void translate(int x
, int y
)
359 public void translate(double tx
, double ty
)
361 if ((tx
== 0) && (ty
== 0))
364 needAffineTransform();
367 public void rotate(double theta
)
372 needAffineTransform();
375 public void rotate(double theta
, double x
, double y
)
380 needAffineTransform();
383 public void scale(double scaleX
, double scaleY
)
385 if ((scaleX
== 1) && (scaleY
== 1))
388 needAffineTransform();
391 public void shear(double shearX
, double shearY
)
393 if ((shearX
== 0) && (shearY
== 0))
396 needAffineTransform();
399 private void needAffineTransform()
401 throw new UnsupportedOperationException("state with affine " +
402 "transform not implemented");