OSDN Git Service

2003-02-19 Michael Koch <konqueror@gmx.de>
[pf3gnuchains/gcc-fork.git] / libjava / gnu / awt / j2d / Graphics2DImpl.java
1 /* Copyright (C) 2000, 2002, 2003  Free Software Foundation
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package gnu.awt.j2d;
10
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;
19 import java.awt.Font;
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;
33 import java.util.Map;
34
35 /**
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.).
41  *
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.
52  *
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.
58  *
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
64  * functionality.
65  *
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
75  * the backend.
76  *
77  * <p>The initial state for graphics object of most raster displays
78  * would call for two levels of indirection:
79  *
80  * <pre>
81  * Graphics2D object ---> IntegerGraphicsState ---> DirectRasterGraphics
82  * </pre>
83  */
84 public class Graphics2DImpl extends Graphics2D implements Cloneable
85 {
86   GraphicsConfiguration config;
87
88   AbstractGraphicsState state;
89     
90   Color fg;
91   Color bg;
92
93   Font font;
94   
95   public Graphics2DImpl(GraphicsConfiguration config)
96   {
97     this.config = config;
98   }
99   
100   public void setState(AbstractGraphicsState state)
101   {
102     this.state = state;
103     this.state.setFrontend(this);
104   }
105     
106   public Object clone()
107   {
108     Graphics2DImpl gfxCopy = (Graphics2DImpl) super.clone();
109     AbstractGraphicsState stateCopy =
110       (AbstractGraphicsState) state.clone();
111     gfxCopy.setState(stateCopy);
112     
113     return gfxCopy;
114   }
115
116
117   // -------- Graphics methods:
118
119   public Graphics create()
120   {
121     Graphics2DImpl gfxCopy = (Graphics2DImpl) clone();
122     return gfxCopy;
123   }
124
125   public Color getColor()
126   {
127     return fg;
128   }
129   
130   public void setColor(Color color)
131   {
132     fg = color;
133     state.setColor(color);
134   }
135
136   public void setPaintMode()
137   {
138     state.setPaintMode();
139   }
140
141   public void setXORMode(Color altColor)
142   {
143     state.setXORMode(altColor);
144   }
145
146   public Font getFont()
147   {
148     return font;
149   }
150
151   public void setFont(Font font)
152   {
153     this.font = font;
154     state.setFont(font);
155   }
156     
157   public FontMetrics getFontMetrics(Font font)
158   {
159     return state.getFontMetrics(font);
160   }
161
162   public Rectangle getClipBounds()
163   {
164     return state.getClipBounds();
165   }
166     
167   public void clipRect(int x, int y, int width, int height)
168   {
169     Shape clip = state.getClip();
170     if (clip instanceof Rectangle)
171       {
172         Rectangle clipRect = (Rectangle) clip;
173         clip = clipRect.intersection(new Rectangle(x, y, width, height));
174         setClip(clip);
175         return;
176       }
177         
178     String msg =
179       "intersecting current clip shape " + clip + " with new rectangle " +
180       "has not been implemented yet";
181     throw new UnsupportedOperationException(msg);
182   }
183
184   public void setClip(int x, int y, int width, int height)
185   {
186     Rectangle clip = new Rectangle(x, y, width, height);
187     setClip(clip);
188   }
189
190   public Shape getClip()
191   {
192     return state.getClip();
193   }
194
195   public void setClip(Shape clip)
196   {
197     state.setClip(clip);
198   }
199
200   public void copyArea(int x, int y, int width, int height,
201                        int dx, int dy)
202   {
203     state.copyArea(x, y, width, height, dx, dy);
204   }
205
206   public void drawLine(int x1, int y1, int x2, int y2)
207   {
208     state.drawLine(x1, y1, x2, y2);
209   }
210     
211   public void fillRect(int x, int y, int width, int height)
212   {
213     state.fillRect(x, y, width, height);
214   }
215     
216   public void clearRect(int x, int y, int width, int height)
217   {
218     state.clearRect(x, y, width, height);
219   }
220   
221   public void drawRoundRect(int x, int y, int width, int height,
222                             int arcWidth, int arcHeight)
223   {
224     state.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
225   }
226     
227   public void fillRoundRect(int x, int y, int width, int height,
228                             int arcWidth, int arcHeight)
229   {
230     state.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
231   }
232
233   public void drawOval(int x, int y, int width, int height)
234   {
235     state.drawOval(x, y, width, height);
236   }
237
238   public void fillOval(int x, int y, int width, int height)
239   {
240     state.fillOval(x, y, width, height);
241   }
242
243   public void drawArc(int x, int y, int width, int height,
244                       int startAngle, int arcAngle)
245   {
246     state.drawArc(x, y, width, height, startAngle, arcAngle);
247   }
248
249   public void fillArc(int x, int y, int width, int height,
250                       int startAngle, int arcAngle)
251   {
252     state.fillArc(x, y, width, height, startAngle, arcAngle);
253   }
254
255   public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
256   {
257     state.drawPolyline(xPoints, yPoints, nPoints);
258   }
259   
260   public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
261   {
262     state.drawPolygon(xPoints, yPoints, nPoints);
263   }
264     
265   public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
266   {
267     state.fillPolygon(xPoints, yPoints, nPoints);
268   }
269     
270   public boolean drawImage(Image image, int x, int y,
271                            ImageObserver observer)
272   {
273     return state.drawImage(image, x, y, observer);
274   }
275
276   public boolean drawImage(Image img, int x, int y,
277                            int width, int height,
278                            ImageObserver observer)
279   {
280     throw new UnsupportedOperationException("not implemented yet");
281   }
282
283   public boolean drawImage(Image img, int x, int y, Color bgcolor,
284                            ImageObserver observer)
285   {
286     throw new UnsupportedOperationException("not implemented yet");
287   }
288   
289   public boolean drawImage(Image img, int x, int y,
290                            int width, int height, Color bgcolor,
291                            ImageObserver observer)
292   {
293     throw new UnsupportedOperationException("not implemented yet");
294   }
295
296   public boolean drawImage(Image img,
297                            int dx1, int dy1, int dx2, int dy2,
298                            int sx1, int sy1, int sx2, int sy2,
299                            ImageObserver observer)
300   {
301     throw new UnsupportedOperationException("not implemented yet");
302   }
303   
304   public boolean drawImage(Image img,
305                            int dx1, int dy1, int dx2, int dy2,
306                            int sx1, int sy1, int sx2, int sy2,
307                            Color bgcolor, ImageObserver observer)
308   {
309     throw new UnsupportedOperationException("not implemented yet");
310   }
311   
312   public void dispose()
313   {
314     AbstractGraphicsState lState = state;
315     
316     state = null;
317     config = null;
318     font = null;
319     fg = null;
320     bg = null;
321     
322     if (lState != null)
323       lState.dispose();
324   }
325     
326
327
328   // -------- Graphics2D methods:
329     
330   public void draw(Shape shape)
331   {
332     state.draw(shape);
333   }
334   
335   public boolean drawImage(Image image, AffineTransform xform,
336                            ImageObserver obs)
337   {
338     throw new UnsupportedOperationException("not implemented yet");
339   }
340
341
342   public void drawString(String text, int x, int y)
343   {
344     state.drawString(text, x, y);
345   }
346
347   public void drawString(String text, float x, float y)
348   {
349     state.drawString(text, x, y);
350   }
351
352   public void fill(Shape shape)
353   {
354     state.fill(shape);
355   }
356
357   public boolean hit(Rectangle rect, Shape text, boolean onStroke)
358   {
359     return state.hit(rect, text, onStroke);
360   }
361     
362   public GraphicsConfiguration getDeviceConfiguration()
363   {
364     return config;
365   }
366
367   public void setPaint(Paint paint)
368   {
369     throw new UnsupportedOperationException("not implemented yet");
370   }
371
372   public void setRenderingHint(RenderingHints.Key hintKey,
373                                Object hintValue)
374   {
375     throw new UnsupportedOperationException("not implemented yet");
376   }
377   
378   public Object getRenderingHint(RenderingHints.Key hintKey)
379   {
380     throw new UnsupportedOperationException("not implemented yet");
381   }
382
383   public RenderingHints getRenderingHints()
384   {
385     throw new UnsupportedOperationException("not implemented yet");
386   }
387     
388   public void translate(int x, int y)
389   {
390     state.translate(x, y);
391   }
392   
393   public void translate(double tx, double ty)
394   {
395     state.translate(tx, ty);
396   }
397     
398   public void rotate(double theta)
399   {
400     state.rotate(theta);
401   }
402
403   public void rotate(double theta, double x, double y)
404   {
405     state.rotate(theta, x, y);
406   }
407   
408   public void scale(double scaleX, double scaleY)
409   {
410     state.scale(scaleX, scaleY);
411   }
412   
413   public void shear(double shearX, double shearY)
414   {
415     state.shear(shearX, shearY);
416   }
417
418   public void transform(AffineTransform Tx)
419   {
420     throw new UnsupportedOperationException("not implemented yet");
421   }
422
423   public void setTransform(AffineTransform Tx)
424   {
425     throw new UnsupportedOperationException("not implemented yet");
426   }
427     
428   public AffineTransform getTransform()
429   {
430     throw new UnsupportedOperationException("not implemented yet");
431   }
432
433   public Paint getPaint()
434   {
435     throw new UnsupportedOperationException("not implemented yet");
436   }
437     
438   public void setBackground(Color color)
439   {
440     bg = color;
441   }
442
443   public Color getBackground()
444   {
445     return bg;
446   }
447
448   public void clip(Shape shape)
449   {
450     Shape clip = state.getClip();
451     
452     if ((shape instanceof Rectangle) && (clip instanceof Rectangle))
453       {
454         clip = ((Rectangle) clip).intersection((Rectangle) shape);
455         state.setClip(clip);
456         return;
457       }
458         
459     String msg =
460       "intersecting current clip shape " + clip + " with new shape " + shape +
461       "has not been implemented yet";
462     throw new UnsupportedOperationException(msg);
463   }
464   
465   public void drawImage(BufferedImage image, BufferedImageOp op, int x, int y)
466   {
467     throw new UnsupportedOperationException("not implemented yet");  
468   }
469
470   public void drawRenderedImage(RenderedImage image, AffineTransform xform)
471   {
472     throw new UnsupportedOperationException("not implemented yet");  
473   }
474
475   public void drawRenderableImage(RenderableImage image, AffineTransform xform)
476   {
477     throw new UnsupportedOperationException("not implemented yet");
478   }
479
480   public void drawString(AttributedCharacterIterator iterator,
481                                   int x, int y)
482   {
483     throw new UnsupportedOperationException("not implemented yet");  
484   }
485
486   public void drawString(AttributedCharacterIterator iterator, float x, 
487                          float y)
488   {
489     throw new UnsupportedOperationException("not implemented yet");
490   }
491
492   public void setComposite(Composite comp)
493   {
494     throw new UnsupportedOperationException("not implemented yet");
495   }
496
497   public void setStroke(Stroke stroke)
498   {
499     throw new UnsupportedOperationException("not implemented yet");
500   }
501
502   public void setRenderingHints(Map hints)
503   {
504     throw new UnsupportedOperationException("not implemented yet");
505   }
506
507   public void addRenderingHints(Map hints)
508   {
509     throw new UnsupportedOperationException("not implemented yet");
510   }
511
512   public Composite getComposite()
513   {
514     throw new UnsupportedOperationException("not implemented yet");
515   }
516
517   public Stroke getStroke()
518   {
519     throw new UnsupportedOperationException("not implemented yet");
520   }
521
522   public FontRenderContext getFontRenderContext ()
523   {
524     throw new UnsupportedOperationException("not implemented yet");
525   }
526
527   public void drawGlyphVector (GlyphVector g, float x, float y)
528   {
529     throw new UnsupportedOperationException("not implemented yet");
530   }
531 }