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)
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
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
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. */
41 import java
.awt
.image
.ImageObserver
;
42 import java
.text
.AttributedCharacterIterator
;
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
58 /*************************************************************************/
65 * Default constructor for subclasses.
72 /*************************************************************************/
79 * Returns a copy of this <code>Graphics</code> object.
81 * @return A copy of this object.
83 public abstract Graphics
86 /*************************************************************************/
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.
102 create(int x
, int y
, int width
, int height
)
104 Graphics g
= create();
107 // FIXME: I'm not sure if this will work. Are the old clip rect bounds
109 g
.clipRect(0, 0, width
, height
);
114 /*************************************************************************/
117 * Translates this context so that its new origin point is the point
120 * @param x The new X coordinate of the origin.
121 * @param y The new Y coordinate of the origin.
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
136 /*************************************************************************/
139 * Sets the current color for this object.
141 * @param color The new color.
144 setColor(Color color
);
146 /*************************************************************************/
149 * Sets this context into "paint" mode, where the target pixels are
150 * completely overwritten when drawn on.
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.
164 setXORMode(Color color
);
166 /*************************************************************************/
169 * Returns the current font for this graphics context.
171 * @return The current font.
176 /*************************************************************************/
179 * Sets the font for this graphics context to the specified value.
181 * @param font The new font.
186 /*************************************************************************/
189 * Returns the font metrics for the current font.
191 * @return The font metrics for the current font.
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
217 * @return The bounding rectangle for the clipping region.
219 public abstract Rectangle
222 /*************************************************************************/
225 * Returns the bounding rectangle of the clipping region for this
228 * @return The bounding rectangle for the clipping region.
230 * @deprecated This method is deprecated in favor of
231 * <code>getClipBounds()</code>.
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.
251 clipRect(int x
, int y
, int width
, int height
);
253 /*************************************************************************/
256 * Sets the clipping region to the rectangle determined by the specified
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.
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
277 /*************************************************************************/
280 * Sets the clipping region to the specified <code>Shape</code>.
282 * @param shape The new clipping region.
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.
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.
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.
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.
339 drawRect(int x
, int y
, int width
, int 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.
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.
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.
392 fillRoundRect(int x
, int y
, int width
, int height
, int arcWidth
, int arcHeight
);
394 /*************************************************************************/
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();
416 drawLine(x1
, y1
, x2
, y1
);
417 drawLine(x1
, y2
, x1
, y1
);
419 drawLine(x2
, y1
, x2
, y2
);
420 drawLine(x2
, y1
, x1
, y2
);
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.
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.
454 drawRoundRect(int x
, int y
, int width
, int height
, boolean raised
)
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.
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.
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.
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.
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.
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.
546 drawPolygon(int xPoints
[], int yPoints
[], int npoints
);
548 /*************************************************************************/
551 * Draws the specified polygon.
553 * @param polygon The polygon to draw.
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.
572 fillPolygon(int xPoints
[], int yPoints
[], int npoints
);
574 /*************************************************************************/
577 * Fills the specified polygon
579 * @param polygon The polygon to fill.
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.
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.
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.
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,
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
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
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
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
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.
760 /*************************************************************************/
763 * Frees the resources held by this graphics context when it is
772 /*************************************************************************/
775 * Returns a string representation of this object.
777 * @param A string representation of this object.
782 return(super.toString());
786 hitClip(int x
, int y
, int width
, int height
)
788 throw new UnsupportedOperationException("not implemented yet");
792 getClipBounds(Rectangle r
)
794 Rectangle clipBounds
= getClipBounds();
801 r
.width
= clipBounds
.width
;
802 r
.height
= clipBounds
.height
;