Graphics.java 23.1 KB
Newer Older
1
/* Graphics.java -- Abstract Java drawing class
2
   Copyright (C) 1999, 2000, 2002, 2004 Free Software Foundation, Inc.
Warren Levy committed
3

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */
Warren Levy committed
37 38 39 40


package java.awt;

41
import java.awt.image.ImageObserver;
42
import java.text.AttributedCharacterIterator;
43

Warren Levy committed
44
/**
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  * This is the abstract superclass of classes for drawing to graphics
  * devices such as the screen or printers.
  *
  * @author Aaron M. Renn (arenn@urbanophile.com)
  * @author Warren Levy <warrenl@cygnus.com>
  */
public abstract class Graphics
{

/*
 * Instance Variables
 */

/*************************************************************************/

/*
 * Constructors
Warren Levy committed
62 63 64
 */

/**
65 66 67 68 69 70 71 72 73 74 75
  * Default constructor for subclasses.
  */
protected
Graphics()
{
}

/*************************************************************************/

/*
 * Instance Methods
Warren Levy committed
76 77
 */

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
/**
  * Returns a copy of this <code>Graphics</code> object.
  *
  * @return A copy of this object.
  */
public abstract Graphics
create();

/*************************************************************************/

/**
  * Returns a copy of this <code>Graphics</code> object.  The origin point
  * will be translated to the point (x, y) and the cliping rectangle set
  * to the intersection of the clipping rectangle in this object and the
  * rectangle specified by the parameters to this method.
  *
  * @param x The new X coordinate of the clipping region rect.
  * @param y The new Y coordinate of the clipping region rect.
  * @param width The width of the clipping region intersect rectangle. 
  * @param height The height of the clipping region intersect rectangle. 
  *
  * @return A copy of this object, modified as specified.
  */
public Graphics
create(int x, int y, int width, int height)
Warren Levy committed
103
{
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  Graphics g = create();

  g.translate(x, y);
  // FIXME: I'm not sure if this will work.  Are the old clip rect bounds
  // translated above?
  g.clipRect(0, 0, width, height);

  return(g);
}

/*************************************************************************/

/**
  * Translates this context so that its new origin point is the point
  * (x, y).
  *
  * @param x The new X coordinate of the origin.
  * @param y The new Y coordinate of the origin.
  */
public abstract void
translate(int x, int y);

/*************************************************************************/

/**
  * Returns the current color for this object.
  *
  * @return The color for this object.
  */
public abstract Color
getColor();

/*************************************************************************/

/**
  * Sets the current color for this object.
  *
  * @param color The new color.
  */
public abstract void
setColor(Color color);

/*************************************************************************/

/**
  * Sets this context into "paint" mode, where the target pixels are
  * completely overwritten when drawn on.
  */
public abstract void
setPaintMode();

/*************************************************************************/

/**
  * Sets this context info "XOR" mode, where the targe pixles are
  * XOR-ed when drawn on. 
  *
  * @param color The color to XOR against.
  */
public abstract void
setXORMode(Color color);
165
  
166
/*************************************************************************/
167

168 169 170 171 172 173 174
/**
  * Returns the current font for this graphics context.
  *
  * @return The current font.
  */
public abstract Font
getFont();
175

176
/*************************************************************************/
177

178 179 180 181 182 183 184
/**
  * Sets the font for this graphics context to the specified value.
  *
  * @param font The new font.
  */
public abstract void
setFont(Font font);
185

186
/*************************************************************************/
187

188 189 190 191 192 193 194 195 196 197
/**
  * Returns the font metrics for the current font.
  *
  * @return The font metrics for the current font.
  */
public FontMetrics
getFontMetrics()
{
  return(getFontMetrics(getFont()));
}
198

199
/*************************************************************************/
200

201 202 203 204 205 206 207 208 209
/**
  * Returns the font metrics for the specified font.
  *
  * @param font The font to return metrics for.
  *
  * @return The requested font metrics.
  */
public abstract FontMetrics
getFontMetrics(Font font);
210

211
/*************************************************************************/
212

213 214 215 216 217 218 219 220
/**
  * Returns the bounding rectangle of the clipping region for this 
  * graphics context.
  *
  * @return The bounding rectangle for the clipping region.
  */
public abstract Rectangle
getClipBounds();
221

222
/*************************************************************************/
223

224 225 226 227 228 229 230 231 232 233 234 235 236 237
/**
  * Returns the bounding rectangle of the clipping region for this 
  * graphics context.
  *
  * @return The bounding rectangle for the clipping region.
  *
  * @deprecated This method is deprecated in favor of
  * <code>getClipBounds()</code>.
  */
public Rectangle
getClipRect()
{
  return(getClipBounds());
}
238

239
/*************************************************************************/
240

241 242 243 244 245 246 247 248 249 250 251
/**
  * Sets the clipping region to the intersection of the current clipping
  * region and the rectangle determined by the specified parameters.
  *
  * @param x The X coordinate of the upper left corner of the intersect rect.
  * @param Y The Y coordinate of the upper left corner of the intersect rect.
  * @param width The width of the intersect rect.
  * @param height The height of the intersect rect.
  */
public abstract void
clipRect(int x, int y, int width, int height);
252

253
/*************************************************************************/
254

255 256 257 258 259 260 261 262 263 264 265
/**
  * Sets the clipping region to the rectangle determined by the specified
  * parameters.
  *
  * @param x The X coordinate of the upper left corner of the rect.
  * @param y The Y coordinate of the upper left corner of the rect.
  * @param width The width of the rect.
  * @param height The height of the rect.
  */
public abstract void
setClip(int x, int y, int width, int height);
266

267
/*************************************************************************/
268

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
/**
  * Returns the current clipping region as a <code>Shape</code> object.
  *
  * @return The clipping region as a <code>Shape</code>.
  */
public abstract Shape
getClip();

/*************************************************************************/

/**
  * Sets the clipping region to the specified <code>Shape</code>.
  *
  * @param shape The new clipping region.
  */
public abstract void
setClip(Shape clip);

/*************************************************************************/

/**
  * Copies the specified rectangle to the specified offset location.
  *
  * @param x The X coordinate of the upper left corner of the copy rect.
  * @param y The Y coordinate of the upper left corner of the copy rect.
  * @param width The width of the copy rect.
  * @param height The height of the copy rect.
  * @param dx The offset from the X value to start drawing.
  * @param dy The offset from the Y value to start drawing.
  */
public abstract void
copyArea(int x, int y, int width, int height, int dx, int dy);

/*************************************************************************/

/**
  * Draws a line between the two specified points.
  *
  * @param x1 The X coordinate of the first point.
  * @param y1 The Y coordinate of the first point.
  * @param x2 The X coordinate of the second point.
  * @param y2 The Y coordinate of the second point.
  */
public abstract void
drawLine(int x1, int y1, int x2, int y2);

/*************************************************************************/

/**
  * Fills the area bounded by the specified rectangle.
  *
  * @param x The X coordinate of the upper left corner of the fill rect.
  * @param y The Y coordinate of the upper left corner of the fill rect.
  * @param width The width of the fill rect.
  * @param height The height of the fill rect.
  */
public abstract void
fillRect(int x, int y, int width, int height); 

/*************************************************************************/

/**
  * Draws the outline of the specified rectangle.
  *
  * @param x The X coordinate of the upper left corner of the draw rect.
  * @param y The Y coordinate of the upper left corner of the draw rect.
  * @param width The width of the draw rect.
  * @param height The height of the draw rect.
  */
public void
drawRect(int x, int y, int width, int height)
{
  int x1 = x;
  int y1 = y;
  int x2 = x + width;
  int y2 = y + height;
  drawLine(x1, y1, x2, y1);
  drawLine(x2, y1, x2, y2);
  drawLine(x2, y2, x1, y2);
  drawLine(x1, y2, x1, y1);
}

/*************************************************************************/

/**
  * Clears the specified rectangle.
  *
  * @param x The X coordinate of the upper left corner of the clear rect.
  * @param y The Y coordinate of the upper left corner of the clear rect.
  * @param width The width of the clear rect.
  * @param height The height of the clear rect.
  */
public abstract void
clearRect(int x, int y, int width, int height);

/*************************************************************************/

/**
  * Draws the outline of the specified rectangle with rounded cornders.
  *
  * @param x The X coordinate of the upper left corner of the draw rect.
  * @param y The Y coordinate of the upper left corner of the draw rect.
  * @param width The width of the draw rect.
  * @param height The height of the draw rect.
  * @param arcWidth The width of the corner arcs.
  * @param arcHeigth The height of the corner arcs.
  */
public abstract void
drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight);

/*************************************************************************/

/**
  * Fills the specified rectangle with rounded cornders.
  *
  * @param x The X coordinate of the upper left corner of the fill rect.
  * @param y The Y coordinate of the upper left corner of the fill rect.
  * @param width The width of the fill rect.
  * @param height The height of the fill rect.
  * @param arcWidth The width of the corner arcs.
  * @param arcHeigth The height of the corner arcs.
  */
public abstract void
fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight);

/*************************************************************************/

public void
draw3DRect(int x, int y, int width, int height, boolean raised)
{
  Color color = getColor();
  Color tl = color.brighter();
  Color br = color.darker();
402
    
403 404 405 406 407 408
  if (!raised)
    {
      Color tmp = tl;
      tl = br;
      br = tmp;
    }
409
    
410 411 412 413
  int x1 = x;
  int y1 = y;
  int x2 = x + width;
  int y2 = y + height;
414
    
415 416 417 418 419
  setColor(tl);
  drawLine(x1, y1, x2, y1);
  drawLine(x1, y2, x1, y1);
  setColor(br);
  drawLine(x2, y1, x2, y2);
Graydon Hoare committed
420
  drawLine(x2, y2, x1, y2);
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
  setColor(color);
}

/**
  * Fills the specified rectangle with a 3D effect
  *
  * @param x The X coordinate of the upper left corner of the fill rect.
  * @param y The Y coordinate of the upper left corner of the fill rect.
  * @param width The width of the fill rect.
  * @param height The height of the fill rect.
  * @param raised <code>true</code> if the rectangle appears raised,
  * <code>false</code> if it should appear etched.
  */
public void
fill3DRect(int x, int y, int width, int height, boolean raised)
{
  fillRect(x, y, width, height);
  draw3DRect(x, y, width-1, height-1, raised);
}

/*************************************************************************/

/**
  * Draws an oval that just fits within the specified rectangle.
  *
  * @param x The X coordinate of the upper left corner of the rect.
  * @param y The Y coordinate of the upper left corner of the rect.
  * @param width The width of the rect.
  * @param height The height of the rect.
  */
public abstract void
drawOval(int x, int y, int width, int height);

/*************************************************************************/

/**
  * Fills an oval that just fits within the specified rectangle.
  *
  * @param x The X coordinate of the upper left corner of the rect.
  * @param y The Y coordinate of the upper left corner of the rect.
  * @param width The width of the rect.
  * @param height The height of the rect.
  */
public abstract void
fillOval(int x, int y, int width, int height);

/*************************************************************************/

/**
  * Draws an arc using the specified bounding rectangle and the specified
  * angle parameter.  The arc is centered at the center of the rectangle.
  * The arc starts at the arcAngle position and extend for arcAngle
  * degrees.  The degree origin is at the 3 o'clock position.
  *
  * @param x The X coordinate of the upper left corner of the rect.
  * @param y The Y coordinate of the upper left corner of the rect.
  * @param width The width of the rect.
  * @param height The height of the rect.
  * @param arcStart The beginning angle of the arc.
  * @param arcAngle The extent of the arc.
  */
public abstract void
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle);

/*************************************************************************/

/**
  * Fills the arc define by the specified bounding rectangle and the specified
  * angle parameter.  The arc is centered at the center of the rectangle.
  * The arc starts at the arcAngle position and extend for arcAngle
  * degrees.  The degree origin is at the 3 o'clock position.
  *
  * @param x The X coordinate of the upper left corner of the rect.
  * @param y The Y coordinate of the upper left corner of the rect.
  * @param width The width of the rect.
  * @param height The height of the rect.
  * @param arcStart The beginning angle of the arc.
  * @param arcAngle The extent of the arc.
  */
public abstract void
fillArc(int x, int y, int width, int height, int startAngle, int arcAngle);

/*************************************************************************/

/**
  * Draws a series of interconnected lines determined by the arrays
  * of corresponding x and y coordinates.
  *
  * @param xPoints The X coordinate array.
  * @param yPoints The Y coordinate array.
  * @param npoints The number of points to draw.
  */
public abstract void
drawPolyline(int xPoints[], int yPoints[], int npoints);

/*************************************************************************/

/**
  * Draws a series of interconnected lines determined by the arrays
  * of corresponding x and y coordinates.  The figure is closed if necessary
  * by connecting the first and last points.
  *
  * @param xPoints The X coordinate array.
  * @param yPoints The Y coordinate array.
  * @param npoints The number of points to draw.
  */
public abstract void
drawPolygon(int xPoints[], int yPoints[], int npoints);

/*************************************************************************/

/**
  * Draws the specified polygon.
  *
  * @param polygon The polygon to draw.
  */
public void
drawPolygon(Polygon polygon)
{
  drawPolygon(polygon.xpoints, polygon.ypoints, polygon.npoints);
}

/*************************************************************************/

/**
  * Fills the polygon determined by the arrays
  * of corresponding x and y coordinates.
  *
  * @param xPoints The X coordinate array.
  * @param yPoints The Y coordinate array.
  * @param npoints The number of points to draw.
  */
public abstract void
fillPolygon(int xPoints[], int yPoints[], int npoints);

/*************************************************************************/

/**
  * Fills the specified polygon
  *
  * @param polygon The polygon to fill.
  */
public void
fillPolygon(Polygon polygon)
{
  fillPolygon(polygon.xpoints, polygon.ypoints, polygon.npoints);
}

/*************************************************************************/

/**
  * Draws the specified string starting at the specified point.
  *
  * @param string The string to draw.
  * @param x The X coordinate of the point to draw at.
  * @param y The Y coordinate of the point to draw at.
  */
public abstract void
drawString(String string, int x, int y);

581 582
public abstract void drawString (AttributedCharacterIterator ci, int x, int y);

583
/*************************************************************************/
584

585 586 587 588 589 590 591 592 593 594 595 596 597 598
/**
  * Draws the specified characters starting at the specified point.
  *
  * @param data The array of characters to draw.
  * @param offset The offset into the array to start drawing characters from.
  * @param length The number of characters to draw.
  * @param x The X coordinate of the point to draw at.
  * @param y The Y coordinate of the point to draw at.
  */
public void
drawChars(char data[], int offset, int length, int x, int y)
{
  drawString(new String(data, offset, length), x, y);
}
599

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
public void
drawBytes(byte[] data, int offset, int length, int x, int y)
{
  String str = new String(data, offset, length);
  drawString(str, x, y);
}

/*************************************************************************/

/**
  * Draws all of the image that is available and returns.  If the image
  * is not completely loaded, <code>false</code> is returned and 
  * the specified iamge observer is notified as more data becomes 
  * available.
  *
  * @param image The image to draw.
  * @param x The X coordinate of the point to draw at.
  * @param y The Y coordinate of the point to draw at.
  * @param observer The image observer to notify as data becomes available.
  *
  * @return <code>true</code> if all the image data is available,
  * <code>false</code> otherwise.
622
  */
623 624 625 626
public abstract boolean
drawImage(Image image, int x, int y, ImageObserver observer);
 
/*************************************************************************/
627

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
/**
  * Draws all of the image that is available and returns.  The image
  * is scaled to fit in the specified rectangle.  If the image
  * is not completely loaded, <code>false</code> is returned and 
  * the specified iamge observer is notified as more data becomes 
  * available.
  *
  * @param image The image to draw.
  * @param x The X coordinate of the point to draw at.
  * @param y The Y coordinate of the point to draw at.
  * @param width The width of the rectangle to draw in.
  * @param height The height of the rectangle to draw in.
  * @param observer The image observer to notify as data becomes available.
  *
  * @return <code>true</code> if all the image data is available,
  * <code>false</code> otherwise.
  */
public abstract boolean
drawImage(Image image, int x, int y, int width, int height, 
          ImageObserver observer);
 
/*************************************************************************/
650

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
/**
  * Draws all of the image that is available and returns.  If the image
  * is not completely loaded, <code>false</code> is returned and 
  * the specified iamge observer is notified as more data becomes 
  * available.
  *
  * @param image The image to draw.
  * @param x The X coordinate of the point to draw at.
  * @param y The Y coordinate of the point to draw at.
  * @param bgcolor The background color to use for the image.
  * @param observer The image observer to notify as data becomes available.
  *
  * @return <code>true</code> if all the image data is available,
  * <code>false</code> otherwise.
  */
public abstract boolean
drawImage(Image image, int x, int y, Color bgcolor, ImageObserver observer);
 
/*************************************************************************/
670

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
/**
  * Draws all of the image that is available and returns.  The image
  * is scaled to fit in the specified rectangle.  If the image
  * is not completely loaded, <code>false</code> is returned and 
  * the specified iamge observer is notified as more data becomes 
  * available.
  *
  * @param image The image to draw.
  * @param x The X coordinate of the point to draw at.
  * @param y The Y coordinate of the point to draw at.
  * @param width The width of the rectangle to draw in.
  * @param height The height of the rectangle to draw in.
  * @param bgcolor The background color to use for the image.
  * @param observer The image observer to notify as data becomes available.
  *
  * @return <code>true</code> if all the image data is available,
  * <code>false</code> otherwise.
  */
public abstract boolean
drawImage(Image image, int x, int y, int width, int height, Color bgcolor,
          ImageObserver observer);
 
/*************************************************************************/
694

695 696 697 698 699 700
/**
  * FIXME: Write Javadocs for this when you understand it.
  */
public abstract boolean
drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
          int sx2, int sy2, ImageObserver observer);
701

702 703 704 705 706 707 708 709 710 711
/*************************************************************************/

/**
  * FIXME: Write Javadocs for this when you understand it.
  */
public abstract boolean
drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
          int sx2, int sy2, Color bgcolor, ImageObserver observer);

/*************************************************************************/
712

713 714 715 716 717 718
/**
  * Free any resources held by this graphics context immediately instead
  * of waiting for the object to be garbage collected and finalized.
  */
public abstract void
dispose();
719

720 721 722 723 724 725 726 727 728 729 730
/*************************************************************************/

/**
  * Frees the resources held by this graphics context when it is
  * garbage collected.
  */
public void
finalize()
{
  dispose();
}
731

732 733 734
/*************************************************************************/

/**
Graydon Hoare committed
735 736 737 738
 * Returns a string representation of this object.
 *
 * @param A string representation of this object. 
 */
739 740 741
public String
toString()
{
Graydon Hoare committed
742
  return getClass ().getName () + "[font=" + getFont () + ",color=" + getColor () + "]";
743 744 745 746 747 748 749 750 751 752 753 754
}

public boolean
hitClip(int x, int y, int width, int height)
{
  throw new UnsupportedOperationException("not implemented yet");
}

public Rectangle
getClipBounds(Rectangle r)
{
  Rectangle clipBounds = getClipBounds();
755
  
756 757 758 759 760 761 762 763
  if (r == null)
    return clipBounds;

  r.x      = clipBounds.x;
  r.y      = clipBounds.y;
  r.width  = clipBounds.width;
  r.height = clipBounds.height;
  return r;
Warren Levy committed
764
}
765 766 767

} // class Graphics