FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / awt / Graphics.java
blob1b4581b64f0692c270d919e7874936cc6a2c7210
1 /* Graphics.java -- Abstract Java drawing class
2 Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt;
41 import java.awt.image.ImageObserver;
42 import java.text.AttributedCharacterIterator;
44 /**
45 * This is the abstract superclass of classes for drawing to graphics
46 * devices such as the screen or printers.
48 * @author Aaron M. Renn (arenn@urbanophile.com)
49 * @author Warren Levy <warrenl@cygnus.com>
51 public abstract class Graphics
55 * Instance Variables
58 /*************************************************************************/
61 * Constructors
64 /**
65 * Default constructor for subclasses.
67 protected
68 Graphics()
72 /*************************************************************************/
75 * Instance Methods
78 /**
79 * Returns a copy of this <code>Graphics</code> object.
81 * @return A copy of this object.
83 public abstract Graphics
84 create();
86 /*************************************************************************/
88 /**
89 * Returns a copy of this <code>Graphics</code> object. The origin point
90 * will be translated to the point (x, y) and the cliping rectangle set
91 * to the intersection of the clipping rectangle in this object and the
92 * rectangle specified by the parameters to this method.
94 * @param x The new X coordinate of the clipping region rect.
95 * @param y The new Y coordinate of the clipping region rect.
96 * @param width The width of the clipping region intersect rectangle.
97 * @param height The height of the clipping region intersect rectangle.
99 * @return A copy of this object, modified as specified.
101 public Graphics
102 create(int x, int y, int width, int height)
104 Graphics g = create();
106 g.translate(x, y);
107 // FIXME: I'm not sure if this will work. Are the old clip rect bounds
108 // translated above?
109 g.clipRect(0, 0, width, height);
111 return(g);
114 /*************************************************************************/
117 * Translates this context so that its new origin point is the point
118 * (x, y).
120 * @param x The new X coordinate of the origin.
121 * @param y The new Y coordinate of the origin.
123 public abstract void
124 translate(int x, int y);
126 /*************************************************************************/
129 * Returns the current color for this object.
131 * @return The color for this object.
133 public abstract Color
134 getColor();
136 /*************************************************************************/
139 * Sets the current color for this object.
141 * @param color The new color.
143 public abstract void
144 setColor(Color color);
146 /*************************************************************************/
149 * Sets this context into "paint" mode, where the target pixels are
150 * completely overwritten when drawn on.
152 public abstract void
153 setPaintMode();
155 /*************************************************************************/
158 * Sets this context info "XOR" mode, where the targe pixles are
159 * XOR-ed when drawn on.
161 * @param color The color to XOR against.
163 public abstract void
164 setXORMode(Color color);
166 /*************************************************************************/
169 * Returns the current font for this graphics context.
171 * @return The current font.
173 public abstract Font
174 getFont();
176 /*************************************************************************/
179 * Sets the font for this graphics context to the specified value.
181 * @param font The new font.
183 public abstract void
184 setFont(Font font);
186 /*************************************************************************/
189 * Returns the font metrics for the current font.
191 * @return The font metrics for the current font.
193 public FontMetrics
194 getFontMetrics()
196 return(getFontMetrics(getFont()));
199 /*************************************************************************/
202 * Returns the font metrics for the specified font.
204 * @param font The font to return metrics for.
206 * @return The requested font metrics.
208 public abstract FontMetrics
209 getFontMetrics(Font font);
211 /*************************************************************************/
214 * Returns the bounding rectangle of the clipping region for this
215 * graphics context.
217 * @return The bounding rectangle for the clipping region.
219 public abstract Rectangle
220 getClipBounds();
222 /*************************************************************************/
225 * Returns the bounding rectangle of the clipping region for this
226 * graphics context.
228 * @return The bounding rectangle for the clipping region.
230 * @deprecated This method is deprecated in favor of
231 * <code>getClipBounds()</code>.
233 public Rectangle
234 getClipRect()
236 return(getClipBounds());
239 /*************************************************************************/
242 * Sets the clipping region to the intersection of the current clipping
243 * region and the rectangle determined by the specified parameters.
245 * @param x The X coordinate of the upper left corner of the intersect rect.
246 * @param Y The Y coordinate of the upper left corner of the intersect rect.
247 * @param width The width of the intersect rect.
248 * @param height The height of the intersect rect.
250 public abstract void
251 clipRect(int x, int y, int width, int height);
253 /*************************************************************************/
256 * Sets the clipping region to the rectangle determined by the specified
257 * parameters.
259 * @param x The X coordinate of the upper left corner of the rect.
260 * @param y The Y coordinate of the upper left corner of the rect.
261 * @param width The width of the rect.
262 * @param height The height of the rect.
264 public abstract void
265 setClip(int x, int y, int width, int height);
267 /*************************************************************************/
270 * Returns the current clipping region as a <code>Shape</code> object.
272 * @return The clipping region as a <code>Shape</code>.
274 public abstract Shape
275 getClip();
277 /*************************************************************************/
280 * Sets the clipping region to the specified <code>Shape</code>.
282 * @param shape The new clipping region.
284 public abstract void
285 setClip(Shape clip);
287 /*************************************************************************/
290 * Copies the specified rectangle to the specified offset location.
292 * @param x The X coordinate of the upper left corner of the copy rect.
293 * @param y The Y coordinate of the upper left corner of the copy rect.
294 * @param width The width of the copy rect.
295 * @param height The height of the copy rect.
296 * @param dx The offset from the X value to start drawing.
297 * @param dy The offset from the Y value to start drawing.
299 public abstract void
300 copyArea(int x, int y, int width, int height, int dx, int dy);
302 /*************************************************************************/
305 * Draws a line between the two specified points.
307 * @param x1 The X coordinate of the first point.
308 * @param y1 The Y coordinate of the first point.
309 * @param x2 The X coordinate of the second point.
310 * @param y2 The Y coordinate of the second point.
312 public abstract void
313 drawLine(int x1, int y1, int x2, int y2);
315 /*************************************************************************/
318 * Fills the area bounded by the specified rectangle.
320 * @param x The X coordinate of the upper left corner of the fill rect.
321 * @param y The Y coordinate of the upper left corner of the fill rect.
322 * @param width The width of the fill rect.
323 * @param height The height of the fill rect.
325 public abstract void
326 fillRect(int x, int y, int width, int height);
328 /*************************************************************************/
331 * Draws the outline of the specified rectangle.
333 * @param x The X coordinate of the upper left corner of the draw rect.
334 * @param y The Y coordinate of the upper left corner of the draw rect.
335 * @param width The width of the draw rect.
336 * @param height The height of the draw rect.
338 public void
339 drawRect(int x, int y, int width, int height)
341 int x1 = x;
342 int y1 = y;
343 int x2 = x + width;
344 int y2 = y + height;
345 drawLine(x1, y1, x2, y1);
346 drawLine(x2, y1, x2, y2);
347 drawLine(x2, y2, x1, y2);
348 drawLine(x1, y2, x1, y1);
351 /*************************************************************************/
354 * Clears the specified rectangle.
356 * @param x The X coordinate of the upper left corner of the clear rect.
357 * @param y The Y coordinate of the upper left corner of the clear rect.
358 * @param width The width of the clear rect.
359 * @param height The height of the clear rect.
361 public abstract void
362 clearRect(int x, int y, int width, int height);
364 /*************************************************************************/
367 * Draws the outline of the specified rectangle with rounded cornders.
369 * @param x The X coordinate of the upper left corner of the draw rect.
370 * @param y The Y coordinate of the upper left corner of the draw rect.
371 * @param width The width of the draw rect.
372 * @param height The height of the draw rect.
373 * @param arcWidth The width of the corner arcs.
374 * @param arcHeigth The height of the corner arcs.
376 public abstract void
377 drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight);
379 /*************************************************************************/
382 * Fills the specified rectangle with rounded cornders.
384 * @param x The X coordinate of the upper left corner of the fill rect.
385 * @param y The Y coordinate of the upper left corner of the fill rect.
386 * @param width The width of the fill rect.
387 * @param height The height of the fill rect.
388 * @param arcWidth The width of the corner arcs.
389 * @param arcHeigth The height of the corner arcs.
391 public abstract void
392 fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight);
394 /*************************************************************************/
396 public void
397 draw3DRect(int x, int y, int width, int height, boolean raised)
399 Color color = getColor();
400 Color tl = color.brighter();
401 Color br = color.darker();
403 if (!raised)
405 Color tmp = tl;
406 tl = br;
407 br = tmp;
410 int x1 = x;
411 int y1 = y;
412 int x2 = x + width;
413 int y2 = y + height;
415 setColor(tl);
416 drawLine(x1, y1, x2, y1);
417 drawLine(x1, y2, x1, y1);
418 setColor(br);
419 drawLine(x2, y1, x2, y2);
420 drawLine(x2, y1, x1, y2);
421 setColor(color);
425 * Fills the specified rectangle with a 3D effect
427 * @param x The X coordinate of the upper left corner of the fill rect.
428 * @param y The Y coordinate of the upper left corner of the fill rect.
429 * @param width The width of the fill rect.
430 * @param height The height of the fill rect.
431 * @param raised <code>true</code> if the rectangle appears raised,
432 * <code>false</code> if it should appear etched.
434 public void
435 fill3DRect(int x, int y, int width, int height, boolean raised)
437 fillRect(x, y, width, height);
438 draw3DRect(x, y, width-1, height-1, raised);
441 /*************************************************************************/
444 * Draws the outline of the specified rectangle with a 3D effect
446 * @param x The X coordinate of the upper left corner of the draw rect.
447 * @param y The Y coordinate of the upper left corner of the draw rect.
448 * @param width The width of the draw rect.
449 * @param height The height of the draw rect.
450 * @param raised <code>true</code> if the rectangle appears raised,
451 * <code>false</code> if it should appear etched.
453 public void
454 drawRoundRect(int x, int y, int width, int height, boolean raised)
456 // FIXME: ???
459 /*************************************************************************/
462 * Draws an oval that just fits within the specified rectangle.
464 * @param x The X coordinate of the upper left corner of the rect.
465 * @param y The Y coordinate of the upper left corner of the rect.
466 * @param width The width of the rect.
467 * @param height The height of the rect.
469 public abstract void
470 drawOval(int x, int y, int width, int height);
472 /*************************************************************************/
475 * Fills an oval that just fits within the specified rectangle.
477 * @param x The X coordinate of the upper left corner of the rect.
478 * @param y The Y coordinate of the upper left corner of the rect.
479 * @param width The width of the rect.
480 * @param height The height of the rect.
482 public abstract void
483 fillOval(int x, int y, int width, int height);
485 /*************************************************************************/
488 * Draws an arc using the specified bounding rectangle and the specified
489 * angle parameter. The arc is centered at the center of the rectangle.
490 * The arc starts at the arcAngle position and extend for arcAngle
491 * degrees. The degree origin is at the 3 o'clock position.
493 * @param x The X coordinate of the upper left corner of the rect.
494 * @param y The Y coordinate of the upper left corner of the rect.
495 * @param width The width of the rect.
496 * @param height The height of the rect.
497 * @param arcStart The beginning angle of the arc.
498 * @param arcAngle The extent of the arc.
500 public abstract void
501 drawArc(int x, int y, int width, int height, int startAngle, int arcAngle);
503 /*************************************************************************/
506 * Fills the arc define by the specified bounding rectangle and the specified
507 * angle parameter. The arc is centered at the center of the rectangle.
508 * The arc starts at the arcAngle position and extend for arcAngle
509 * degrees. The degree origin is at the 3 o'clock position.
511 * @param x The X coordinate of the upper left corner of the rect.
512 * @param y The Y coordinate of the upper left corner of the rect.
513 * @param width The width of the rect.
514 * @param height The height of the rect.
515 * @param arcStart The beginning angle of the arc.
516 * @param arcAngle The extent of the arc.
518 public abstract void
519 fillArc(int x, int y, int width, int height, int startAngle, int arcAngle);
521 /*************************************************************************/
524 * Draws a series of interconnected lines determined by the arrays
525 * of corresponding x and y coordinates.
527 * @param xPoints The X coordinate array.
528 * @param yPoints The Y coordinate array.
529 * @param npoints The number of points to draw.
531 public abstract void
532 drawPolyline(int xPoints[], int yPoints[], int npoints);
534 /*************************************************************************/
537 * Draws a series of interconnected lines determined by the arrays
538 * of corresponding x and y coordinates. The figure is closed if necessary
539 * by connecting the first and last points.
541 * @param xPoints The X coordinate array.
542 * @param yPoints The Y coordinate array.
543 * @param npoints The number of points to draw.
545 public abstract void
546 drawPolygon(int xPoints[], int yPoints[], int npoints);
548 /*************************************************************************/
551 * Draws the specified polygon.
553 * @param polygon The polygon to draw.
555 public void
556 drawPolygon(Polygon polygon)
558 drawPolygon(polygon.xpoints, polygon.ypoints, polygon.npoints);
561 /*************************************************************************/
564 * Fills the polygon determined by the arrays
565 * of corresponding x and y coordinates.
567 * @param xPoints The X coordinate array.
568 * @param yPoints The Y coordinate array.
569 * @param npoints The number of points to draw.
571 public abstract void
572 fillPolygon(int xPoints[], int yPoints[], int npoints);
574 /*************************************************************************/
577 * Fills the specified polygon
579 * @param polygon The polygon to fill.
581 public void
582 fillPolygon(Polygon polygon)
584 fillPolygon(polygon.xpoints, polygon.ypoints, polygon.npoints);
587 /*************************************************************************/
590 * Draws the specified string starting at the specified point.
592 * @param string The string to draw.
593 * @param x The X coordinate of the point to draw at.
594 * @param y The Y coordinate of the point to draw at.
596 public abstract void
597 drawString(String string, int x, int y);
599 public abstract void drawString (AttributedCharacterIterator ci, int x, int y);
601 /*************************************************************************/
604 * Draws the specified characters starting at the specified point.
606 * @param data The array of characters to draw.
607 * @param offset The offset into the array to start drawing characters from.
608 * @param length The number of characters to draw.
609 * @param x The X coordinate of the point to draw at.
610 * @param y The Y coordinate of the point to draw at.
612 public void
613 drawChars(char data[], int offset, int length, int x, int y)
615 drawString(new String(data, offset, length), x, y);
618 /*************************************************************************/
621 * Draws the specified bytes as text starting at the specified point.
623 * @param data The array of bytes to draw.
624 * @param offset The offset into the array to start drawing bytes from.
625 * @param length The number of bytes to draw.
626 * @param x The X coordinate of the point to draw at.
627 * @param y The Y coordinate of the point to draw at.
629 public void
630 drawChars(byte data[], int offset, int length, int x, int y)
632 drawString(new String(data, offset, length), x, y);
636 public abstract void drawString(AttributedCharacterIterator iterator,
637 int x, int y)
640 public void
641 drawBytes(byte[] data, int offset, int length, int x, int y)
643 String str = new String(data, offset, length);
644 drawString(str, x, y);
647 /*************************************************************************/
650 * Draws all of the image that is available and returns. If the image
651 * is not completely loaded, <code>false</code> is returned and
652 * the specified iamge observer is notified as more data becomes
653 * available.
655 * @param image The image to draw.
656 * @param x The X coordinate of the point to draw at.
657 * @param y The Y coordinate of the point to draw at.
658 * @param observer The image observer to notify as data becomes available.
660 * @return <code>true</code> if all the image data is available,
661 * <code>false</code> otherwise.
663 public abstract boolean
664 drawImage(Image image, int x, int y, ImageObserver observer);
666 /*************************************************************************/
669 * Draws all of the image that is available and returns. The image
670 * is scaled to fit in the specified rectangle. If the image
671 * is not completely loaded, <code>false</code> is returned and
672 * the specified iamge observer is notified as more data becomes
673 * available.
675 * @param image The image to draw.
676 * @param x The X coordinate of the point to draw at.
677 * @param y The Y coordinate of the point to draw at.
678 * @param width The width of the rectangle to draw in.
679 * @param height The height of the rectangle to draw in.
680 * @param observer The image observer to notify as data becomes available.
682 * @return <code>true</code> if all the image data is available,
683 * <code>false</code> otherwise.
685 public abstract boolean
686 drawImage(Image image, int x, int y, int width, int height,
687 ImageObserver observer);
689 /*************************************************************************/
692 * Draws all of the image that is available and returns. If the image
693 * is not completely loaded, <code>false</code> is returned and
694 * the specified iamge observer is notified as more data becomes
695 * available.
697 * @param image The image to draw.
698 * @param x The X coordinate of the point to draw at.
699 * @param y The Y coordinate of the point to draw at.
700 * @param bgcolor The background color to use for the image.
701 * @param observer The image observer to notify as data becomes available.
703 * @return <code>true</code> if all the image data is available,
704 * <code>false</code> otherwise.
706 public abstract boolean
707 drawImage(Image image, int x, int y, Color bgcolor, ImageObserver observer);
709 /*************************************************************************/
712 * Draws all of the image that is available and returns. The image
713 * is scaled to fit in the specified rectangle. If the image
714 * is not completely loaded, <code>false</code> is returned and
715 * the specified iamge observer is notified as more data becomes
716 * available.
718 * @param image The image to draw.
719 * @param x The X coordinate of the point to draw at.
720 * @param y The Y coordinate of the point to draw at.
721 * @param width The width of the rectangle to draw in.
722 * @param height The height of the rectangle to draw in.
723 * @param bgcolor The background color to use for the image.
724 * @param observer The image observer to notify as data becomes available.
726 * @return <code>true</code> if all the image data is available,
727 * <code>false</code> otherwise.
729 public abstract boolean
730 drawImage(Image image, int x, int y, int width, int height, Color bgcolor,
731 ImageObserver observer);
733 /*************************************************************************/
736 * FIXME: Write Javadocs for this when you understand it.
738 public abstract boolean
739 drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
740 int sx2, int sy2, ImageObserver observer);
742 /*************************************************************************/
745 * FIXME: Write Javadocs for this when you understand it.
747 public abstract boolean
748 drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
749 int sx2, int sy2, Color bgcolor, ImageObserver observer);
751 /*************************************************************************/
754 * Free any resources held by this graphics context immediately instead
755 * of waiting for the object to be garbage collected and finalized.
757 public abstract void
758 dispose();
760 /*************************************************************************/
763 * Frees the resources held by this graphics context when it is
764 * garbage collected.
766 public void
767 finalize()
769 dispose();
772 /*************************************************************************/
775 * Returns a string representation of this object.
777 * @param A string representation of this object.
779 public String
780 toString()
782 return(super.toString());
785 public boolean
786 hitClip(int x, int y, int width, int height)
788 throw new UnsupportedOperationException("not implemented yet");
791 public Rectangle
792 getClipBounds(Rectangle r)
794 Rectangle clipBounds = getClipBounds();
796 if (r == null)
797 return clipBounds;
799 r.x = clipBounds.x;
800 r.y = clipBounds.y;
801 r.width = clipBounds.width;
802 r.height = clipBounds.height;
803 return r;
806 } // class Graphics