source: trunk/gcc/libjava/java/awt/Graphics.java

Last change on this file was 2, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 24.1 KB
Line 
1/* Graphics.java -- Abstract Java drawing class
2 Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.image.ImageObserver;
42
43/**
44 * This is the abstract superclass of classes for drawing to graphics
45 * devices such as the screen or printers.
46 *
47 * @author Aaron M. Renn (arenn@urbanophile.com)
48 * @author Warren Levy <warrenl@cygnus.com>
49 */
50public abstract class Graphics
51{
52
53/*
54 * Instance Variables
55 */
56
57/*************************************************************************/
58
59/*
60 * Constructors
61 */
62
63/**
64 * Default constructor for subclasses.
65 */
66protected
67Graphics()
68{
69}
70
71/*************************************************************************/
72
73/*
74 * Instance Methods
75 */
76
77/**
78 * Returns a copy of this <code>Graphics</code> object.
79 *
80 * @return A copy of this object.
81 */
82public abstract Graphics
83create();
84
85/*************************************************************************/
86
87/**
88 * Returns a copy of this <code>Graphics</code> object. The origin point
89 * will be translated to the point (x, y) and the cliping rectangle set
90 * to the intersection of the clipping rectangle in this object and the
91 * rectangle specified by the parameters to this method.
92 *
93 * @param x The new X coordinate of the clipping region rect.
94 * @param y The new Y coordinate of the clipping region rect.
95 * @param width The width of the clipping region intersect rectangle.
96 * @param height The height of the clipping region intersect rectangle.
97 *
98 * @return A copy of this object, modified as specified.
99 */
100public Graphics
101create(int x, int y, int width, int height)
102{
103 Graphics g = create();
104
105 g.translate(x, y);
106 // FIXME: I'm not sure if this will work. Are the old clip rect bounds
107 // translated above?
108 g.clipRect(0, 0, width, height);
109
110 return(g);
111}
112
113/*************************************************************************/
114
115/**
116 * Translates this context so that its new origin point is the point
117 * (x, y).
118 *
119 * @param x The new X coordinate of the origin.
120 * @param y The new Y coordinate of the origin.
121 */
122public abstract void
123translate(int x, int y);
124
125/*************************************************************************/
126
127/**
128 * Returns the current color for this object.
129 *
130 * @return The color for this object.
131 */
132public abstract Color
133getColor();
134
135/*************************************************************************/
136
137/**
138 * Sets the current color for this object.
139 *
140 * @param color The new color.
141 */
142public abstract void
143setColor(Color color);
144
145/*************************************************************************/
146
147/**
148 * Sets this context into "paint" mode, where the target pixels are
149 * completely overwritten when drawn on.
150 */
151public abstract void
152setPaintMode();
153
154/*************************************************************************/
155
156/**
157 * Sets this context info "XOR" mode, where the targe pixles are
158 * XOR-ed when drawn on.
159 *
160 * @param color The color to XOR against.
161 */
162public abstract void
163setXORMode(Color color);
164
165/*************************************************************************/
166
167/**
168 * Returns the current font for this graphics context.
169 *
170 * @return The current font.
171 */
172public abstract Font
173getFont();
174
175/*************************************************************************/
176
177/**
178 * Sets the font for this graphics context to the specified value.
179 *
180 * @param font The new font.
181 */
182public abstract void
183setFont(Font font);
184
185/*************************************************************************/
186
187/**
188 * Returns the font metrics for the current font.
189 *
190 * @return The font metrics for the current font.
191 */
192public FontMetrics
193getFontMetrics()
194{
195 return(getFontMetrics(getFont()));
196}
197
198/*************************************************************************/
199
200/**
201 * Returns the font metrics for the specified font.
202 *
203 * @param font The font to return metrics for.
204 *
205 * @return The requested font metrics.
206 */
207public abstract FontMetrics
208getFontMetrics(Font font);
209
210/*************************************************************************/
211
212/**
213 * Returns the bounding rectangle of the clipping region for this
214 * graphics context.
215 *
216 * @return The bounding rectangle for the clipping region.
217 */
218public abstract Rectangle
219getClipBounds();
220
221/*************************************************************************/
222
223/**
224 * Returns the bounding rectangle of the clipping region for this
225 * graphics context.
226 *
227 * @return The bounding rectangle for the clipping region.
228 *
229 * @deprecated This method is deprecated in favor of
230 * <code>getClipBounds()</code>.
231 */
232public Rectangle
233getClipRect()
234{
235 return(getClipBounds());
236}
237
238/*************************************************************************/
239
240/**
241 * Sets the clipping region to the intersection of the current clipping
242 * region and the rectangle determined by the specified parameters.
243 *
244 * @param x The X coordinate of the upper left corner of the intersect rect.
245 * @param Y The Y coordinate of the upper left corner of the intersect rect.
246 * @param width The width of the intersect rect.
247 * @param height The height of the intersect rect.
248 */
249public abstract void
250clipRect(int x, int y, int width, int height);
251
252/*************************************************************************/
253
254/**
255 * Sets the clipping region to the rectangle determined by the specified
256 * parameters.
257 *
258 * @param x The X coordinate of the upper left corner of the rect.
259 * @param y The Y coordinate of the upper left corner of the rect.
260 * @param width The width of the rect.
261 * @param height The height of the rect.
262 */
263public abstract void
264setClip(int x, int y, int width, int height);
265
266/*************************************************************************/
267
268/**
269 * Returns the current clipping region as a <code>Shape</code> object.
270 *
271 * @return The clipping region as a <code>Shape</code>.
272 */
273public abstract Shape
274getClip();
275
276/*************************************************************************/
277
278/**
279 * Sets the clipping region to the specified <code>Shape</code>.
280 *
281 * @param shape The new clipping region.
282 */
283public abstract void
284setClip(Shape clip);
285
286/*************************************************************************/
287
288/**
289 * Copies the specified rectangle to the specified offset location.
290 *
291 * @param x The X coordinate of the upper left corner of the copy rect.
292 * @param y The Y coordinate of the upper left corner of the copy rect.
293 * @param width The width of the copy rect.
294 * @param height The height of the copy rect.
295 * @param dx The offset from the X value to start drawing.
296 * @param dy The offset from the Y value to start drawing.
297 */
298public abstract void
299copyArea(int x, int y, int width, int height, int dx, int dy);
300
301/*************************************************************************/
302
303/**
304 * Draws a line between the two specified points.
305 *
306 * @param x1 The X coordinate of the first point.
307 * @param y1 The Y coordinate of the first point.
308 * @param x2 The X coordinate of the second point.
309 * @param y2 The Y coordinate of the second point.
310 */
311public abstract void
312drawLine(int x1, int y1, int x2, int y2);
313
314/*************************************************************************/
315
316/**
317 * Fills the area bounded by the specified rectangle.
318 *
319 * @param x The X coordinate of the upper left corner of the fill rect.
320 * @param y The Y coordinate of the upper left corner of the fill rect.
321 * @param width The width of the fill rect.
322 * @param height The height of the fill rect.
323 */
324public abstract void
325fillRect(int x, int y, int width, int height);
326
327/*************************************************************************/
328
329/**
330 * Draws the outline of the specified rectangle.
331 *
332 * @param x The X coordinate of the upper left corner of the draw rect.
333 * @param y The Y coordinate of the upper left corner of the draw rect.
334 * @param width The width of the draw rect.
335 * @param height The height of the draw rect.
336 */
337public void
338drawRect(int x, int y, int width, int height)
339{
340 int x1 = x;
341 int y1 = y;
342 int x2 = x + width;
343 int y2 = y + height;
344 drawLine(x1, y1, x2, y1);
345 drawLine(x2, y1, x2, y2);
346 drawLine(x2, y2, x1, y2);
347 drawLine(x1, y2, x1, y1);
348}
349
350/*************************************************************************/
351
352/**
353 * Clears the specified rectangle.
354 *
355 * @param x The X coordinate of the upper left corner of the clear rect.
356 * @param y The Y coordinate of the upper left corner of the clear rect.
357 * @param width The width of the clear rect.
358 * @param height The height of the clear rect.
359 */
360public abstract void
361clearRect(int x, int y, int width, int height);
362
363/*************************************************************************/
364
365/**
366 * Draws the outline of the specified rectangle with rounded cornders.
367 *
368 * @param x The X coordinate of the upper left corner of the draw rect.
369 * @param y The Y coordinate of the upper left corner of the draw rect.
370 * @param width The width of the draw rect.
371 * @param height The height of the draw rect.
372 * @param arcWidth The width of the corner arcs.
373 * @param arcHeigth The height of the corner arcs.
374 */
375public abstract void
376drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight);
377
378/*************************************************************************/
379
380/**
381 * Fills the specified rectangle with rounded cornders.
382 *
383 * @param x The X coordinate of the upper left corner of the fill rect.
384 * @param y The Y coordinate of the upper left corner of the fill rect.
385 * @param width The width of the fill rect.
386 * @param height The height of the fill rect.
387 * @param arcWidth The width of the corner arcs.
388 * @param arcHeigth The height of the corner arcs.
389 */
390public abstract void
391fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight);
392
393/*************************************************************************/
394
395public void
396draw3DRect(int x, int y, int width, int height, boolean raised)
397{
398 Color color = getColor();
399 Color tl = color.brighter();
400 Color br = color.darker();
401
402 if (!raised)
403 {
404 Color tmp = tl;
405 tl = br;
406 br = tmp;
407 }
408
409 int x1 = x;
410 int y1 = y;
411 int x2 = x + width;
412 int y2 = y + height;
413
414 setColor(tl);
415 drawLine(x1, y1, x2, y1);
416 drawLine(x1, y2, x1, y1);
417 setColor(br);
418 drawLine(x2, y1, x2, y2);
419 drawLine(x2, y1, x1, y2);
420 setColor(color);
421}
422
423/**
424 * Fills the specified rectangle with a 3D effect
425 *
426 * @param x The X coordinate of the upper left corner of the fill rect.
427 * @param y The Y coordinate of the upper left corner of the fill rect.
428 * @param width The width of the fill rect.
429 * @param height The height of the fill rect.
430 * @param raised <code>true</code> if the rectangle appears raised,
431 * <code>false</code> if it should appear etched.
432 */
433public void
434fill3DRect(int x, int y, int width, int height, boolean raised)
435{
436 fillRect(x, y, width, height);
437 draw3DRect(x, y, width-1, height-1, raised);
438}
439
440/*************************************************************************/
441
442/**
443 * Draws the outline of the specified rectangle with a 3D effect
444 *
445 * @param x The X coordinate of the upper left corner of the draw rect.
446 * @param y The Y coordinate of the upper left corner of the draw rect.
447 * @param width The width of the draw rect.
448 * @param height The height of the draw rect.
449 * @param raised <code>true</code> if the rectangle appears raised,
450 * <code>false</code> if it should appear etched.
451 */
452public void
453drawRoundRect(int x, int y, int width, int height, boolean raised)
454{
455 // FIXME: ???
456}
457
458/*************************************************************************/
459
460/**
461 * Draws an oval that just fits within the specified rectangle.
462 *
463 * @param x The X coordinate of the upper left corner of the rect.
464 * @param y The Y coordinate of the upper left corner of the rect.
465 * @param width The width of the rect.
466 * @param height The height of the rect.
467 */
468public abstract void
469drawOval(int x, int y, int width, int height);
470
471/*************************************************************************/
472
473/**
474 * Fills an oval that just fits within the specified rectangle.
475 *
476 * @param x The X coordinate of the upper left corner of the rect.
477 * @param y The Y coordinate of the upper left corner of the rect.
478 * @param width The width of the rect.
479 * @param height The height of the rect.
480 */
481public abstract void
482fillOval(int x, int y, int width, int height);
483
484/*************************************************************************/
485
486/**
487 * Draws an arc using the specified bounding rectangle and the specified
488 * angle parameter. The arc is centered at the center of the rectangle.
489 * The arc starts at the arcAngle position and extend for arcAngle
490 * degrees. The degree origin is at the 3 o'clock position.
491 *
492 * @param x The X coordinate of the upper left corner of the rect.
493 * @param y The Y coordinate of the upper left corner of the rect.
494 * @param width The width of the rect.
495 * @param height The height of the rect.
496 * @param arcStart The beginning angle of the arc.
497 * @param arcAngle The extent of the arc.
498 */
499public abstract void
500drawArc(int x, int y, int width, int height, int startAngle, int arcAngle);
501
502/*************************************************************************/
503
504/**
505 * Fills the arc define by the specified bounding rectangle and the specified
506 * angle parameter. The arc is centered at the center of the rectangle.
507 * The arc starts at the arcAngle position and extend for arcAngle
508 * degrees. The degree origin is at the 3 o'clock position.
509 *
510 * @param x The X coordinate of the upper left corner of the rect.
511 * @param y The Y coordinate of the upper left corner of the rect.
512 * @param width The width of the rect.
513 * @param height The height of the rect.
514 * @param arcStart The beginning angle of the arc.
515 * @param arcAngle The extent of the arc.
516 */
517public abstract void
518fillArc(int x, int y, int width, int height, int startAngle, int arcAngle);
519
520/*************************************************************************/
521
522/**
523 * Draws a series of interconnected lines determined by the arrays
524 * of corresponding x and y coordinates.
525 *
526 * @param xPoints The X coordinate array.
527 * @param yPoints The Y coordinate array.
528 * @param npoints The number of points to draw.
529 */
530public abstract void
531drawPolyline(int xPoints[], int yPoints[], int npoints);
532
533/*************************************************************************/
534
535/**
536 * Draws a series of interconnected lines determined by the arrays
537 * of corresponding x and y coordinates. The figure is closed if necessary
538 * by connecting the first and last points.
539 *
540 * @param xPoints The X coordinate array.
541 * @param yPoints The Y coordinate array.
542 * @param npoints The number of points to draw.
543 */
544public abstract void
545drawPolygon(int xPoints[], int yPoints[], int npoints);
546
547/*************************************************************************/
548
549/**
550 * Draws the specified polygon.
551 *
552 * @param polygon The polygon to draw.
553 */
554public void
555drawPolygon(Polygon polygon)
556{
557 drawPolygon(polygon.xpoints, polygon.ypoints, polygon.npoints);
558}
559
560/*************************************************************************/
561
562/**
563 * Fills the polygon determined by the arrays
564 * of corresponding x and y coordinates.
565 *
566 * @param xPoints The X coordinate array.
567 * @param yPoints The Y coordinate array.
568 * @param npoints The number of points to draw.
569 */
570public abstract void
571fillPolygon(int xPoints[], int yPoints[], int npoints);
572
573/*************************************************************************/
574
575/**
576 * Fills the specified polygon
577 *
578 * @param polygon The polygon to fill.
579 */
580public void
581fillPolygon(Polygon polygon)
582{
583 fillPolygon(polygon.xpoints, polygon.ypoints, polygon.npoints);
584}
585
586/*************************************************************************/
587
588/**
589 * Draws the specified string starting at the specified point.
590 *
591 * @param string The string to draw.
592 * @param x The X coordinate of the point to draw at.
593 * @param y The Y coordinate of the point to draw at.
594 */
595public abstract void
596drawString(String string, int x, int y);
597
598/*************************************************************************/
599
600/**
601 * Draws the specified characters starting at the specified point.
602 *
603 * @param data The array of characters to draw.
604 * @param offset The offset into the array to start drawing characters from.
605 * @param length The number of characters to draw.
606 * @param x The X coordinate of the point to draw at.
607 * @param y The Y coordinate of the point to draw at.
608 */
609public void
610drawChars(char data[], int offset, int length, int x, int y)
611{
612 drawString(new String(data, offset, length), x, y);
613}
614
615/*************************************************************************/
616
617/**
618 * Draws the specified bytes as text starting at the specified point.
619 *
620 * @param data The array of bytes to draw.
621 * @param offset The offset into the array to start drawing bytes from.
622 * @param length The number of bytes to draw.
623 * @param x The X coordinate of the point to draw at.
624 * @param y The Y coordinate of the point to draw at.
625 */
626public void
627drawChars(byte data[], int offset, int length, int x, int y)
628{
629 drawString(new String(data, offset, length), x, y);
630}
631
632/*
633public abstract void drawString(AttributedCharacterIterator iterator,
634 int x, int y)
635*/
636
637public void
638drawBytes(byte[] data, int offset, int length, int x, int y)
639{
640 String str = new String(data, offset, length);
641 drawString(str, x, y);
642}
643
644/*************************************************************************/
645
646/**
647 * Draws all of the image that is available and returns. If the image
648 * is not completely loaded, <code>false</code> is returned and
649 * the specified iamge observer is notified as more data becomes
650 * available.
651 *
652 * @param image The image to draw.
653 * @param x The X coordinate of the point to draw at.
654 * @param y The Y coordinate of the point to draw at.
655 * @param observer The image observer to notify as data becomes available.
656 *
657 * @return <code>true</code> if all the image data is available,
658 * <code>false</code> otherwise.
659 */
660public abstract boolean
661drawImage(Image image, int x, int y, ImageObserver observer);
662
663/*************************************************************************/
664
665/**
666 * Draws all of the image that is available and returns. The image
667 * is scaled to fit in the specified rectangle. If the image
668 * is not completely loaded, <code>false</code> is returned and
669 * the specified iamge observer is notified as more data becomes
670 * available.
671 *
672 * @param image The image to draw.
673 * @param x The X coordinate of the point to draw at.
674 * @param y The Y coordinate of the point to draw at.
675 * @param width The width of the rectangle to draw in.
676 * @param height The height of the rectangle to draw in.
677 * @param observer The image observer to notify as data becomes available.
678 *
679 * @return <code>true</code> if all the image data is available,
680 * <code>false</code> otherwise.
681 */
682public abstract boolean
683drawImage(Image image, int x, int y, int width, int height,
684 ImageObserver observer);
685
686/*************************************************************************/
687
688/**
689 * Draws all of the image that is available and returns. If the image
690 * is not completely loaded, <code>false</code> is returned and
691 * the specified iamge observer is notified as more data becomes
692 * available.
693 *
694 * @param image The image to draw.
695 * @param x The X coordinate of the point to draw at.
696 * @param y The Y coordinate of the point to draw at.
697 * @param bgcolor The background color to use for the image.
698 * @param observer The image observer to notify as data becomes available.
699 *
700 * @return <code>true</code> if all the image data is available,
701 * <code>false</code> otherwise.
702 */
703public abstract boolean
704drawImage(Image image, int x, int y, Color bgcolor, ImageObserver observer);
705
706/*************************************************************************/
707
708/**
709 * Draws all of the image that is available and returns. The image
710 * is scaled to fit in the specified rectangle. If the image
711 * is not completely loaded, <code>false</code> is returned and
712 * the specified iamge observer is notified as more data becomes
713 * available.
714 *
715 * @param image The image to draw.
716 * @param x The X coordinate of the point to draw at.
717 * @param y The Y coordinate of the point to draw at.
718 * @param width The width of the rectangle to draw in.
719 * @param height The height of the rectangle to draw in.
720 * @param bgcolor The background color to use for the image.
721 * @param observer The image observer to notify as data becomes available.
722 *
723 * @return <code>true</code> if all the image data is available,
724 * <code>false</code> otherwise.
725 */
726public abstract boolean
727drawImage(Image image, int x, int y, int width, int height, Color bgcolor,
728 ImageObserver observer);
729
730/*************************************************************************/
731
732/**
733 * FIXME: Write Javadocs for this when you understand it.
734 */
735public abstract boolean
736drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
737 int sx2, int sy2, ImageObserver observer);
738
739/*************************************************************************/
740
741/**
742 * FIXME: Write Javadocs for this when you understand it.
743 */
744public abstract boolean
745drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
746 int sx2, int sy2, Color bgcolor, ImageObserver observer);
747
748/*************************************************************************/
749
750/**
751 * Free any resources held by this graphics context immediately instead
752 * of waiting for the object to be garbage collected and finalized.
753 */
754public abstract void
755dispose();
756
757/*************************************************************************/
758
759/**
760 * Frees the resources held by this graphics context when it is
761 * garbage collected.
762 */
763public void
764finalize()
765{
766 dispose();
767}
768
769/*************************************************************************/
770
771/**
772 * Returns a string representation of this object.
773 *
774 * @param A string representation of this object.
775 */
776public String
777toString()
778{
779 return(super.toString());
780}
781
782public boolean
783hitClip(int x, int y, int width, int height)
784{
785 throw new UnsupportedOperationException("not implemented yet");
786}
787
788public Rectangle
789getClipBounds(Rectangle r)
790{
791 Rectangle clipBounds = getClipBounds();
792
793 if (r == null)
794 return clipBounds;
795
796 r.x = clipBounds.x;
797 r.y = clipBounds.y;
798 r.width = clipBounds.width;
799 r.height = clipBounds.height;
800 return r;
801}
802
803} // class Graphics
804
Note: See TracBrowser for help on using the repository browser.