2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / awt / j2d / IntegerGraphicsState.java
blob4eb4c6182b12c94b6a38471c6181636b5d5e30e3
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
7 details. */
9 package gnu.awt.j2d;
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;
18 import java.awt.Font;
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;
26 /**
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
41 int tx;
42 int ty;
44 DirectRasterGraphics directGfx;
45 Shape clip;
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;
65 public Object clone()
67 IntegerGraphicsState clone = (IntegerGraphicsState) super.clone();
68 clone.directGfx = (DirectRasterGraphics) directGfx.clone();
70 return clone;
73 public void dispose()
75 DirectRasterGraphics lDeviceGfx = directGfx;
77 directGfx = null;
79 if (lDeviceGfx != null)
80 lDeviceGfx.dispose();
82 super.dispose();
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();
117 clipRect.x += tx;
118 clipRect.y += ty;
120 this.clip = clipRect;
122 directGfx.setClip(clipRect);
123 return;
126 String msg =
127 "translating clip shape " + clip + " into device " +
128 "coordinate space has not been implemented yet";
130 throw new UnsupportedOperationException(msg);
133 public Shape getClip()
135 if (clip instanceof Rectangle)
137 Rectangle clipRect = (Rectangle) clip;
138 clipRect.x -= tx;
139 clipRect.y -= ty;
140 return clipRect;
143 String msg =
144 "translating clip shape " + clip + " into user " +
145 "coordinate space has not been implemented yet";
147 throw new UnsupportedOperationException(msg);
150 public Rectangle getClipBounds()
152 Rectangle clipRect = clip.getBounds();
154 clipRect.x -= tx;
155 clipRect.y -= ty;
156 return clipRect;
159 public void copyArea(int x, int y,
160 int width, int height,
161 int dx, int dy)
163 directGfx.copyArea(x+tx, y+ty, width, height, dx, dy);
166 public void drawLine(int x1, int y1,
167 int x2, int y2)
169 directGfx.drawLine(x1+tx, y1+ty, x2+tx, y2+ty);
172 public void fillRect(int x, int y,
173 int width, int height)
175 directGfx.fillRect(x+tx, y+ty, width, height);
178 public void clearRect(int x, int y,
179 int width, int height)
181 directGfx.setColor(frontend.getBackground());
182 directGfx.fillRect(x+tx, y+ty, width, height);
183 directGfx.setColor(frontend.getColor());
186 public void drawRoundRect(int x, int y,
187 int width, int height,
188 int arcWidth, int arcHeight)
190 throw new UnsupportedOperationException("not implemented yet");
193 public void fillRoundRect(int x, int y,
194 int width, int height,
195 int arcWidth, int arcHeight)
197 throw new UnsupportedOperationException("not implemented yet");
200 public void drawOval(int x, int y,
201 int width, int height)
203 drawArc (x, y, width, height, 0, 360);
206 public void fillOval(int x, int y,
207 int width, int height)
209 fillArc (x, y, width, height, 0, 360);
212 public void drawArc(int x, int y,
213 int width, int height,
214 int startAngle, int arcAngle)
216 directGfx.drawArc(x+tx, y+ty, width, height, startAngle, arcAngle);
219 public void fillArc(int x, int y,
220 int width, int height,
221 int startAngle, int arcAngle)
223 directGfx.fillArc(x+tx, y+ty, width, height, startAngle, arcAngle);
226 public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
228 if ((tx == 0) && (ty == 0))
230 directGfx.drawPolyline(xPoints, yPoints, nPoints);
231 return;
234 throw new UnsupportedOperationException("translate not implemented");
237 public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
239 if ((tx == 0) && (ty == 0))
241 directGfx.drawPolygon(xPoints, yPoints, nPoints);
242 return;
245 throw new UnsupportedOperationException("translate not implemented");
248 public void fillPolygon (int[] xPoints, int[] yPoints, int nPoints)
250 // FIXME: remove tx & ty args once translation via AffineTransform
251 // is implemented.
252 directGfx.fillPolygon (xPoints, yPoints, nPoints, tx, ty);
255 public boolean drawImage(Image image, int x, int y,
256 ImageObserver observer)
258 x += tx;
259 y += ty;
261 if (image instanceof ScreenCoupledImage)
263 GraphicsConfiguration config
264 = ((ScreenCoupledImage)image).getGraphicsConfiguration ();
265 if (config == frontend.config)
266 return directGfx.drawImage (image, x, y, observer);
268 if (image instanceof BufferedImage)
270 BufferedImage bImage = (BufferedImage) image;
271 // FIXME: eliminate? ScreenCoupledImage is probably more efficient
272 Object config = bImage.getProperty ("java.awt.GraphicsConfiguration");
273 if (config == frontend.config)
274 return directGfx.drawImage (image, x, y, observer);
276 int width = image.getWidth (null);
277 int height = image.getHeight (null);
279 Rectangle bounds = new Rectangle (x, y, width, height);
281 MappedRaster mr = directGfx.mapRaster (bounds);
283 // manipulate raster here...
284 ColorModel colorModel = mr.getColorModel ();
285 WritableRaster raster = mr.getRaster ();
287 int xEnd = x + width;
288 int yEnd = y + height;
290 // FIXME: Use the following code only as a fallback. It's SLOW!
292 Object rgbElem = null;
293 for (int yy=0; yy<height; yy++)
295 for (int xx=0; xx<width; xx++)
297 int srgb = bImage.getRGB (xx, yy);
298 int sa = ((srgb >>> 24) & 0xff) + 1;
299 int sr = ((srgb >>> 16) & 0xff) + 1;
300 int sg = ((srgb >>> 8) & 0xff) + 1;
301 int sb = (srgb & 0xff) + 1;
303 rgbElem = raster.getDataElements (xx+x, yy+y, rgbElem);
304 int drgb = colorModel.getRGB (rgbElem);
305 int dr = ((drgb >>> 16) & 0xff) + 1;
306 int dg = ((drgb >>> 8) & 0xff) + 1;
307 int db = (drgb & 0xff) + 1;
308 int da = 256 - sa;
310 dr = ((sr*sa + dr*da) >>> 8) - 1;
311 dg = ((sg*sa + dg*da) >>> 8) - 1;
312 db = ((sb*sa + db*da) >>> 8) - 1;
314 drgb = (dr<<16) | (dg<<8) | db;
316 rgbElem = colorModel.getDataElements (drgb, rgbElem);
318 raster.setDataElements (xx+x, yy+y, rgbElem);
321 directGfx.unmapRaster (mr);
322 return true;
325 throw new UnsupportedOperationException ("drawing image " + image +
326 "not implemented");
330 // -------- Graphics2D methods:
332 public void draw(Shape shape)
334 if (shape instanceof Rectangle)
336 Rectangle rect = (Rectangle) shape;
337 directGfx.drawRect(rect.x+tx, rect.y+ty, rect.width, rect.height);
338 return;
341 throw new UnsupportedOperationException("shape not implemented");
344 public void fill(Shape shape)
346 if (shape instanceof Rectangle)
348 Rectangle rect = (Rectangle) shape;
349 directGfx.fillRect(rect.x+tx, rect.y+ty, rect.width, rect.height);
350 return;
353 throw new UnsupportedOperationException("not implemented");
356 public boolean hit(Rectangle rect, Shape text,
357 boolean onStroke)
359 throw new UnsupportedOperationException("not implemented");
362 public void drawString(String text, int x, int y)
364 directGfx.drawString(text, x+tx, y+ty);
367 public void drawString(String text, float x, float y)
369 drawString(text, (int) x, (int) y);
372 public void translate(int x, int y)
374 tx += x;
375 ty += y;
378 public void translate(double tx, double ty)
380 if ((tx == 0) && (ty == 0))
381 return;
383 needAffineTransform();
386 public void rotate(double theta)
388 if (theta == 0)
389 return;
391 needAffineTransform();
394 public void rotate(double theta, double x, double y)
396 if (theta == 0)
397 return;
399 needAffineTransform();
402 public void scale(double scaleX, double scaleY)
404 if ((scaleX == 1) && (scaleY == 1))
405 return;
407 needAffineTransform();
410 public void shear(double shearX, double shearY)
412 if ((shearX == 0) && (shearY == 0))
413 return;
415 needAffineTransform();
418 private void needAffineTransform()
420 throw new UnsupportedOperationException("state with affine " +
421 "transform not implemented");