1 /* Copyright (C) 2000, 2002, 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.Composite;
13 import java.awt.Image;
14 import java.awt.Shape;
15 import java.awt.Rectangle;
16 import java.awt.Graphics;
17 import java.awt.Graphics2D;
18 import java.awt.GraphicsConfiguration;
20 import java.awt.FontMetrics;
21 import java.awt.Paint;
22 import java.awt.RenderingHints;
23 import java.awt.Stroke;
24 import java.awt.font.FontRenderContext;
25 import java.awt.font.GlyphVector;
26 import java.awt.geom.AffineTransform;
27 import java.awt.image.ImageObserver;
28 import java.awt.image.BufferedImage;
29 import java.awt.image.BufferedImageOp;
30 import java.awt.image.RenderedImage;
31 import java.awt.image.renderable.RenderableImage;
32 import java.text.AttributedCharacterIterator;
36 * Delegates almost all work to a state object, that allows us to
37 * hot-swap rendering strategies based on state changes inflicted on
38 * this Graphics object. This class keeps track of properties that are
39 * not affected by the state, (such as clip shape,
40 * foreground/background color, font, etc.).
42 * <p>The far front-end of the rendering pipeline consists of the
43 * Graphics2D API. In the far back-end, lies the native graphics
44 * libraries. In most cases the native graphics libraries only have
45 * direct support for a subset of the properties of Graphics2D. To
46 * make up missing features in the native graphics libraries, the
47 * pipeline between the front-end and the back-end need to translate
48 * drawing request to primitive operations that are supported by the
49 * back-end. E.g. for X11, drawing a straight line will translate to
50 * an XDrawLine, drawing a bezier curve will trigger flattening of the
51 * curve and will result in a call to XDrawLines.
53 * <p>This is the basic strategy for the rendering pipeline: Whenever
54 * a graphics property change occurs, that causes the current pipeline
55 * to be insufficient, amend or replace parts of the pipeline so that
56 * the pipeline will once again be able to translate requests to the
57 * set of primitives supported by the native graphics library.
59 * <p>Most graphics libraries share common subsets of
60 * functionality. To be able to reuse pieces of the rendering pipeline
61 * for several backends, we define interfaces that describe subsets of
62 * characteristics supported by the backends. A wrapper for the native
63 * library can implement several interfaces to describe its range of
66 * <p>Typically, most painting is done with a graphics object with
67 * simple properties. Unless one is using a complex Look & Feel, the
68 * painting of Swing components will never require affine transforms,
69 * alpha blending, non-rectangular clipping, etc. When graphics
70 * objects are created, they start off in a state where all the
71 * properties are simple. Most graphics objects experience only
72 * trivial property changes, and never leave this simple state. It is
73 * therefore wise to ensure that the rendering pipeline for this
74 * initial state is lean and as much as possible plugs directly into
77 * <p>The initial state for graphics object of most raster displays
78 * would call for two levels of indirection:
81 * Graphics2D object ---> IntegerGraphicsState ---> DirectRasterGraphics
84 public class Graphics2DImpl extends Graphics2D implements Cloneable
86 GraphicsConfiguration config;
88 AbstractGraphicsState state;
95 public Graphics2DImpl(GraphicsConfiguration config)
100 public void setState(AbstractGraphicsState state)
103 this.state.setFrontend(this);
106 public Object clone()
110 Graphics2DImpl gfxCopy = (Graphics2DImpl) super.clone();
111 AbstractGraphicsState stateCopy =
112 (AbstractGraphicsState) state.clone();
113 gfxCopy.setState(stateCopy);
117 catch (CloneNotSupportedException ex)
119 // This should never happen.
120 throw new InternalError ();
125 // -------- Graphics methods:
127 public Graphics create()
129 Graphics2DImpl gfxCopy = (Graphics2DImpl) clone();
133 public Color getColor()
138 public void setColor(Color color)
141 state.setColor(color);
144 public void setPaintMode()
146 state.setPaintMode();
149 public void setXORMode(Color altColor)
151 state.setXORMode(altColor);
154 public Font getFont()
159 public void setFont(Font font)
165 public FontMetrics getFontMetrics(Font font)
167 return state.getFontMetrics(font);
170 public Rectangle getClipBounds()
172 return state.getClipBounds();
175 public void clipRect(int x, int y, int width, int height)
177 Shape clip = state.getClip();
180 clip = new Rectangle (x,y,width,height);
184 if (clip instanceof Rectangle)
186 Rectangle clipRect = (Rectangle) clip;
187 clip = clipRect.intersection(new Rectangle(x, y, width, height));
193 "intersecting current clip shape " + clip + " with new rectangle " +
194 "has not been implemented yet";
195 throw new UnsupportedOperationException(msg);
198 public void setClip(int x, int y, int width, int height)
200 Rectangle clip = new Rectangle(x, y, width, height);
204 public Shape getClip()
206 return state.getClip();
209 public void setClip(Shape clip)
214 public void copyArea(int x, int y, int width, int height,
217 state.copyArea(x, y, width, height, dx, dy);
220 public void drawLine(int x1, int y1, int x2, int y2)
222 state.drawLine(x1, y1, x2, y2);
225 public void fillRect(int x, int y, int width, int height)
227 state.fillRect(x, y, width, height);
230 public void clearRect(int x, int y, int width, int height)
232 state.clearRect(x, y, width, height);
235 public void drawRoundRect(int x, int y, int width, int height,
236 int arcWidth, int arcHeight)
238 state.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
241 public void fillRoundRect(int x, int y, int width, int height,
242 int arcWidth, int arcHeight)
244 state.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
247 public void drawOval(int x, int y, int width, int height)
249 state.drawOval(x, y, width, height);
252 public void fillOval(int x, int y, int width, int height)
254 state.fillOval(x, y, width, height);
257 public void drawArc(int x, int y, int width, int height,
258 int startAngle, int arcAngle)
260 state.drawArc(x, y, width, height, startAngle, arcAngle);
263 public void fillArc(int x, int y, int width, int height,
264 int startAngle, int arcAngle)
266 state.fillArc(x, y, width, height, startAngle, arcAngle);
269 public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
271 state.drawPolyline(xPoints, yPoints, nPoints);
274 public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
276 state.drawPolygon(xPoints, yPoints, nPoints);
279 public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
281 state.fillPolygon(xPoints, yPoints, nPoints);
284 public boolean drawImage(Image image, int x, int y,
285 ImageObserver observer)
287 return state.drawImage(image, x, y, observer);
290 public boolean drawImage(Image img, int x, int y,
291 int width, int height,
292 ImageObserver observer)
294 throw new UnsupportedOperationException("not implemented yet");
297 public boolean drawImage(Image img, int x, int y, Color bgcolor,
298 ImageObserver observer)
300 throw new UnsupportedOperationException("not implemented yet");
303 public boolean drawImage(Image img, int x, int y,
304 int width, int height, Color bgcolor,
305 ImageObserver observer)
307 throw new UnsupportedOperationException("not implemented yet");
310 public boolean drawImage(Image img,
311 int dx1, int dy1, int dx2, int dy2,
312 int sx1, int sy1, int sx2, int sy2,
313 ImageObserver observer)
315 throw new UnsupportedOperationException("not implemented yet");
318 public boolean drawImage(Image img,
319 int dx1, int dy1, int dx2, int dy2,
320 int sx1, int sy1, int sx2, int sy2,
321 Color bgcolor, ImageObserver observer)
323 throw new UnsupportedOperationException("not implemented yet");
326 public void dispose()
328 AbstractGraphicsState lState = state;
342 // -------- Graphics2D methods:
344 public void draw(Shape shape)
349 public boolean drawImage(Image image, AffineTransform xform,
352 throw new UnsupportedOperationException("not implemented yet");
356 public void drawString(String text, int x, int y)
358 state.drawString(text, x, y);
361 public void drawString(String text, float x, float y)
363 state.drawString(text, x, y);
366 public void fill(Shape shape)
371 public boolean hit(Rectangle rect, Shape text, boolean onStroke)
373 return state.hit(rect, text, onStroke);
376 public GraphicsConfiguration getDeviceConfiguration()
381 public void setPaint(Paint paint)
383 throw new UnsupportedOperationException("not implemented yet");
386 public void setRenderingHint(RenderingHints.Key hintKey,
389 throw new UnsupportedOperationException("not implemented yet");
392 public Object getRenderingHint(RenderingHints.Key hintKey)
394 throw new UnsupportedOperationException("not implemented yet");
397 public RenderingHints getRenderingHints()
399 throw new UnsupportedOperationException("not implemented yet");
402 public void translate(int x, int y)
404 state.translate(x, y);
407 public void translate(double tx, double ty)
409 state.translate(tx, ty);
412 public void rotate(double theta)
417 public void rotate(double theta, double x, double y)
419 state.rotate(theta, x, y);
422 public void scale(double scaleX, double scaleY)
424 state.scale(scaleX, scaleY);
427 public void shear(double shearX, double shearY)
429 state.shear(shearX, shearY);
432 public void transform(AffineTransform Tx)
434 throw new UnsupportedOperationException("not implemented yet");
437 public void setTransform(AffineTransform Tx)
439 throw new UnsupportedOperationException("not implemented yet");
442 public AffineTransform getTransform()
444 throw new UnsupportedOperationException("not implemented yet");
447 public Paint getPaint()
449 throw new UnsupportedOperationException("not implemented yet");
452 public void setBackground(Color color)
457 public Color getBackground()
462 public void clip(Shape shape)
464 Shape clip = state.getClip();
466 if ((shape instanceof Rectangle) && (clip instanceof Rectangle))
468 clip = ((Rectangle) clip).intersection((Rectangle) shape);
474 "intersecting current clip shape " + clip + " with new shape " + shape +
475 "has not been implemented yet";
476 throw new UnsupportedOperationException(msg);
479 public void drawImage(BufferedImage image, BufferedImageOp op, int x, int y)
481 throw new UnsupportedOperationException("not implemented yet");
484 public void drawRenderedImage(RenderedImage image, AffineTransform xform)
486 throw new UnsupportedOperationException("not implemented yet");
489 public void drawRenderableImage(RenderableImage image, AffineTransform xform)
491 throw new UnsupportedOperationException("not implemented yet");
494 public void drawString(AttributedCharacterIterator iterator,
497 throw new UnsupportedOperationException("not implemented yet");
500 public void drawString(AttributedCharacterIterator iterator, float x,
503 throw new UnsupportedOperationException("not implemented yet");
506 public void setComposite(Composite comp)
508 throw new UnsupportedOperationException("not implemented yet");
511 public void setStroke(Stroke stroke)
513 throw new UnsupportedOperationException("not implemented yet");
516 public void setRenderingHints(Map hints)
518 throw new UnsupportedOperationException("not implemented yet");
521 public void addRenderingHints(Map hints)
523 throw new UnsupportedOperationException("not implemented yet");
526 public Composite getComposite()
528 throw new UnsupportedOperationException("not implemented yet");
531 public Stroke getStroke()
533 throw new UnsupportedOperationException("not implemented yet");
536 public FontRenderContext getFontRenderContext ()
538 throw new UnsupportedOperationException("not implemented yet");
541 public void drawGlyphVector (GlyphVector g, float x, float y)
543 throw new UnsupportedOperationException("not implemented yet");