GNU Classpath (0.18) | ||
Frames | No Frames |
1: /* DebugGraphics.java -- 2: Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 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. 10: 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. 15: 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., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 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. 25: 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. */ 37: 38: package javax.swing; 39: 40: import java.awt.Color; 41: import java.awt.Font; 42: import java.awt.FontMetrics; 43: import java.awt.Graphics; 44: import java.awt.Image; 45: import java.awt.Rectangle; 46: import java.awt.Shape; 47: import java.awt.image.ImageObserver; 48: import java.io.PrintStream; 49: import java.text.AttributedCharacterIterator; 50: 51: 52: /** 53: * An extension of {@link Graphics} that can be used for debugging 54: * custom Swing widgets. <code>DebugGraphics</code> has the ability to 55: * draw slowly and can log drawing actions. 56: * 57: * @author Andrew Selkirk 58: */ 59: public class DebugGraphics extends Graphics 60: { 61: /** 62: * LOG_OPTION 63: */ 64: public static final int LOG_OPTION = 1; 65: 66: /** 67: * FLASH_OPTION 68: */ 69: public static final int FLASH_OPTION = 2; 70: 71: /** 72: * BUFFERED_OPTION 73: */ 74: public static final int BUFFERED_OPTION = 4; 75: 76: /** 77: * NONE_OPTION 78: */ 79: public static final int NONE_OPTION = -1; 80: 81: static Color debugFlashColor = Color.RED; 82: static int debugFlashCount = 10; 83: static int debugFlashTime = 1000; 84: static PrintStream debugLogStream = System.out; 85: 86: /** 87: * graphics 88: */ 89: Graphics graphics; 90: 91: /** 92: * color 93: */ 94: Color color = Color.BLACK; 95: 96: /** 97: * buffer 98: */ 99: Image buffer; 100: 101: /** 102: * debugOptions 103: */ 104: int debugOptions; 105: 106: /** 107: * graphicsID 108: */ 109: int graphicsID; 110: 111: /** 112: * xOffset 113: */ 114: int xOffset; 115: 116: /** 117: * yOffset 118: */ 119: int yOffset; 120: 121: /** 122: * Creates a <code>DebugGraphics</code> object. 123: */ 124: public DebugGraphics() 125: { 126: // TODO 127: } 128: 129: /** 130: * Creates a <code>DebugGraphics</code> object. 131: * 132: * @param graphics The <code>Graphics</code> object to wrap 133: * @param component TODO 134: */ 135: public DebugGraphics(Graphics graphics, JComponent component) 136: { 137: this.graphics = graphics; 138: // FIXME: What shall we do with component ? 139: } 140: 141: /** 142: * Creates a <code>DebugGraphics</code> object. 143: * 144: * @param graphics The <code>Graphics</code> object to wrap 145: */ 146: public DebugGraphics(Graphics graphics) 147: { 148: this.graphics = graphics; 149: } 150: 151: /** 152: * Sets the color to draw stuff with. 153: * 154: * @param color The color 155: */ 156: public void setColor(Color color) 157: { 158: this.color = color; 159: } 160: 161: /** 162: * Creates a overrides <code>Graphics.create</code> to create a 163: * <code>DebugGraphics</code> object. 164: * 165: * @return a new <code>DebugGraphics</code> object. 166: */ 167: public Graphics create() 168: { 169: return new DebugGraphics(graphics.create()); 170: } 171: 172: /** 173: * Creates a overrides <code>Graphics.create</code> to create a 174: * <code>DebugGraphics</code> object. 175: * 176: * @param x the x coordinate 177: * @param y the y coordinate 178: * @param width the width 179: * @param height the height 180: * 181: * @return a new <code>DebugGraphics</code> object. 182: */ 183: public Graphics create(int x, int y, int width, int height) 184: { 185: return new DebugGraphics(graphics.create(x, y, width, height)); 186: } 187: 188: /** 189: * flashColor 190: * 191: * @return Color 192: */ 193: public static Color flashColor() 194: { 195: return debugFlashColor; 196: } 197: 198: /** 199: * setFlashColor 200: * 201: * @param color the color to use for flashing 202: */ 203: public static void setFlashColor(Color color) 204: { 205: debugFlashColor = color; 206: } 207: 208: /** 209: * flashTime 210: * 211: * @return The time in milliseconds 212: */ 213: public static int flashTime() 214: { 215: return debugFlashTime; 216: } 217: 218: /** 219: * setFlashTime 220: * 221: * @param time The time in milliseconds 222: */ 223: public static void setFlashTime(int time) 224: { 225: debugFlashTime = time; 226: } 227: 228: /** 229: * flashCount 230: * 231: * @return The number of flashes 232: */ 233: public static int flashCount() 234: { 235: return debugFlashCount; 236: } 237: 238: /** 239: * setFlashCount 240: * 241: * @param count The number of flashes 242: */ 243: public static void setFlashCount(int count) 244: { 245: debugFlashCount = count; 246: } 247: 248: /** 249: * logStream 250: * 251: * @return The <code>PrintStream</code> to write logging messages to 252: */ 253: public static PrintStream logStream() 254: { 255: return debugLogStream; 256: } 257: 258: /** 259: * setLogStream 260: * 261: * @param stream The currently set <code>PrintStream</code>. 262: */ 263: public static void setLogStream(PrintStream stream) 264: { 265: debugLogStream = stream; 266: } 267: 268: /** 269: * getFont 270: * 271: * @return The font 272: */ 273: public Font getFont() 274: { 275: return graphics.getFont(); 276: } 277: 278: /** 279: * setFont 280: * 281: * @param font The font to use for drawing text 282: */ 283: public void setFont(Font font) 284: { 285: graphics.setFont(font); 286: } 287: 288: /** 289: * Returns the color used for drawing. 290: * 291: * @return The color. 292: */ 293: public Color getColor() 294: { 295: return color; 296: } 297: 298: /** 299: * Returns the font metrics of the current font. 300: * 301: * @return a <code>FontMetrics</code> object 302: */ 303: public FontMetrics getFontMetrics() 304: { 305: return graphics.getFontMetrics(); 306: } 307: 308: /** 309: * Returns the font metrics for a given font. 310: * 311: * @param font the font to get the metrics for 312: * 313: * @return a <code>FontMetrics</code> object 314: */ 315: public FontMetrics getFontMetrics(Font font) 316: { 317: return graphics.getFontMetrics(font); 318: } 319: 320: /** 321: * translate 322: * 323: * @param x the x coordinate 324: * @param y the y coordinate 325: */ 326: public void translate(int x, int y) 327: { 328: graphics.translate(x, y); 329: } 330: 331: /** 332: * setPaintMode 333: */ 334: public void setPaintMode() 335: { 336: graphics.setPaintMode(); 337: } 338: 339: /** 340: * setXORMode 341: * 342: * @param color the color 343: */ 344: public void setXORMode(Color color) 345: { 346: graphics.setXORMode(color); 347: } 348: 349: /** 350: * getClipBounds 351: * 352: * @return Rectangle 353: */ 354: public Rectangle getClipBounds() 355: { 356: return graphics.getClipBounds(); 357: } 358: 359: /** 360: * Intersects the current clip region with the given region. 361: * 362: * @param x The x-position of the region 363: * @param y The y-position of the region 364: * @param width The width of the region 365: * @param height The height of the region 366: */ 367: public void clipRect(int x, int y, int width, int height) 368: { 369: graphics.clipRect(x, y, width, height); 370: } 371: 372: /** 373: * Sets the clipping region. 374: * 375: * @param x The x-position of the region 376: * @param y The y-position of the region 377: * @param width The width of the region 378: * @param height The height of the region 379: */ 380: public void setClip(int x, int y, int width, int height) 381: { 382: graphics.setClip(x, y, width, height); 383: } 384: 385: /** 386: * Returns the current clipping region. 387: * 388: * @return Shape 389: */ 390: public Shape getClip() 391: { 392: return graphics.getClip(); 393: } 394: 395: /** 396: * Sets the current clipping region 397: * 398: * @param shape The clippin region 399: */ 400: public void setClip(Shape shape) 401: { 402: graphics.setClip(shape); 403: } 404: 405: private void sleep(int milliseconds) 406: { 407: try 408: { 409: Thread.sleep(milliseconds); 410: } 411: catch (InterruptedException e) 412: { 413: // Ignore this. 414: } 415: } 416: 417: /** 418: * Draws a rectangle. 419: * 420: * @param x The x-position of the rectangle 421: * @param y The y-position of the rectangle 422: * @param width The width of the rectangle 423: * @param height The height of the rectangle 424: */ 425: public void drawRect(int x, int y, int width, int height) 426: { 427: for (int index = 0; index < (debugFlashCount - 1); ++index) 428: { 429: graphics.setColor(color); 430: graphics.drawRect(x, y, width, height); 431: sleep(debugFlashTime); 432: 433: graphics.setColor(debugFlashColor); 434: graphics.drawRect(x, y, width, height); 435: sleep(debugFlashTime); 436: } 437: 438: graphics.setColor(color); 439: graphics.drawRect(x, y, width, height); 440: } 441: 442: /** 443: * Draws a filled rectangle. 444: * 445: * @param x The x-position of the rectangle 446: * @param y The y-position of the rectangle 447: * @param width The width of the rectangle 448: * @param height The height of the rectangle 449: */ 450: public void fillRect(int x, int y, int width, int height) 451: { 452: for (int index = 0; index < (debugFlashCount - 1); ++index) 453: { 454: graphics.setColor(color); 455: graphics.fillRect(x, y, width, height); 456: sleep(debugFlashTime); 457: 458: graphics.setColor(debugFlashColor); 459: graphics.fillRect(x, y, width, height); 460: sleep(debugFlashTime); 461: } 462: 463: graphics.setColor(color); 464: graphics.fillRect(x, y, width, height); 465: } 466: 467: /** 468: * clearRect 469: * 470: * @param x The x-position of the rectangle 471: * @param y The y-position of the rectangle 472: * @param width The width of the rectangle 473: * @param height The height of the rectangle 474: */ 475: public void clearRect(int x, int y, int width, int height) 476: { 477: graphics.clearRect(x, y, width, height); 478: } 479: 480: /** 481: * drawRoundRect 482: * 483: * @param x The x-position of the rectangle 484: * @param y The y-position of the rectangle 485: * @param width The width of the rectangle 486: * @param height The height of the rectangle 487: * @param arcWidth TODO 488: * @param arcHeight TODO 489: */ 490: public void drawRoundRect(int x, int y, int width, int height, 491: int arcWidth, int arcHeight) 492: { 493: graphics.drawRoundRect(x, y, width, height, arcWidth, arcHeight); 494: } 495: 496: /** 497: * fillRoundRect 498: * 499: * @param x The x-position of the rectangle 500: * @param y The y-position of the rectangle 501: * @param width The width of the rectangle 502: * @param height The height of the rectangle 503: * @param arcWidth TODO 504: * @param arcHeight TODO 505: */ 506: public void fillRoundRect(int x, int y, int width, int height, 507: int arcWidth, int arcHeight) 508: { 509: graphics.fillRoundRect(x, y, width, height, arcWidth, arcHeight); 510: } 511: 512: /** 513: * drawLine 514: * 515: * @param x1 The x-position of the start 516: * @param y1 The y-position of the start 517: * @param x2 The x-position of the end 518: * @param y2 The y-position of the end 519: */ 520: public void drawLine(int x1, int y1, int x2, int y2) 521: { 522: graphics.drawLine(x1, y1, x2, y2); 523: } 524: 525: /** 526: * draw3DRect 527: * 528: * @param x The x-position of the rectangle 529: * @param y The y-position of the rectangle 530: * @param width The width of the rectangle 531: * @param height The height of the rectangle 532: * @param raised TODO 533: */ 534: public void draw3DRect(int x, int y, int width, int height, boolean raised) 535: { 536: graphics.draw3DRect(x, y, width, height, raised); 537: } 538: 539: /** 540: * fill3DRect 541: * 542: * @param x The x-position of the rectangle 543: * @param y The y-position of the rectangle 544: * @param width The width of the rectangle 545: * @param height The height of the rectangle 546: * @param raised TODO 547: */ 548: public void fill3DRect(int x, int y, int width, int height, boolean raised) 549: { 550: graphics.fill3DRect(x, y, width, height, raised); 551: } 552: 553: /** 554: * drawOval 555: * 556: * @param x the x coordinate 557: * @param y the y coordiante 558: * @param width the width 559: * @param height the height 560: */ 561: public void drawOval(int x, int y, int width, int height) 562: { 563: graphics.drawOval(x, y, width, height); 564: } 565: 566: /** 567: * fillOval 568: * 569: * @param x the x coordinate 570: * @param y the y coordinate 571: * @param width the width 572: * @param height the height 573: */ 574: public void fillOval(int x, int y, int width, int height) 575: { 576: graphics.fillOval(x, y, width, height); 577: } 578: 579: /** 580: * drawArc 581: * 582: * @param x the x coordinate 583: * @param y the y coordinate 584: * @param width the width 585: * @param height the height 586: * @param startAngle TODO 587: * @param arcAngle TODO 588: */ 589: public void drawArc(int x, int y, int width, int height, 590: int startAngle, int arcAngle) 591: { 592: graphics.drawArc(x, y, width, height, startAngle, arcAngle); 593: } 594: 595: /** 596: * fillArc 597: * 598: * @param x the coordinate 599: * @param y the y coordinate 600: * @param width the width 601: * @param height the height 602: * @param startAngle TODO 603: * @param arcAngle TODO 604: */ 605: public void fillArc(int x, int y, int width, int height, 606: int startAngle, int arcAngle) 607: { 608: graphics.fillArc(x, y, width, height, startAngle, arcAngle); 609: } 610: 611: /** 612: * drawPolyline 613: * 614: * @param xpoints TODO 615: * @param ypoints TODO 616: * @param npoints TODO 617: */ 618: public void drawPolyline(int[] xpoints, int[] ypoints, int npoints) 619: { 620: graphics.drawPolyline(xpoints, ypoints, npoints); 621: } 622: 623: /** 624: * drawPolygon 625: * 626: * @param xpoints TODO 627: * @param ypoints TODO 628: * @param npoints TODO 629: */ 630: public void drawPolygon(int[] xpoints, int[] ypoints, int npoints) 631: { 632: graphics.drawPolygon(xpoints, ypoints, npoints); 633: } 634: 635: /** 636: * fillPolygon 637: * 638: * @param xpoints TODO 639: * @param ypoints TODO 640: * @param npoints TODO 641: */ 642: public void fillPolygon(int[] xpoints, int[] ypoints, int npoints) 643: { 644: graphics.fillPolygon(xpoints, ypoints, npoints); 645: } 646: 647: /** 648: * drawString 649: * 650: * @param string the string 651: * @param x the x coordinate 652: * @param y the y coordinate 653: */ 654: public void drawString(String string, int x, int y) 655: { 656: graphics.drawString(string, x, y); 657: } 658: 659: /** 660: * drawString 661: * 662: * @param iterator TODO 663: * @param x the x coordinate 664: * @param y the y coordinate 665: */ 666: public void drawString(AttributedCharacterIterator iterator, 667: int x, int y) 668: { 669: graphics.drawString(iterator, x, y); 670: } 671: 672: /** 673: * drawBytes 674: * 675: * @param data TODO 676: * @param offset TODO 677: * @param length TODO 678: * @param x the x coordinate 679: * @param y the y coordinate 680: */ 681: public void drawBytes(byte[] data, int offset, int length, 682: int x, int y) 683: { 684: graphics.drawBytes(data, offset, length, x, y); 685: } 686: 687: /** 688: * drawChars 689: * 690: * @param data array of characters to draw 691: * @param offset offset in array 692: * @param length number of characters in array to draw 693: * @param x x-position 694: * @param y y-position 695: */ 696: public void drawChars(char[] data, int offset, int length, 697: int x, int y) 698: { 699: for (int index = 0; index < (debugFlashCount - 1); ++index) 700: { 701: graphics.setColor(color); 702: graphics.drawChars(data, offset, length, x, y); 703: sleep(debugFlashTime); 704: 705: graphics.setColor(debugFlashColor); 706: graphics.drawChars(data, offset, length, x, y); 707: sleep(debugFlashTime); 708: } 709: 710: graphics.setColor(color); 711: graphics.drawChars(data, offset, length, x, y); 712: } 713: 714: /** 715: * drawImage 716: * 717: * @param image The image to draw 718: * @param x The x position 719: * @param y The y position 720: * @param observer The image observer 721: * @return boolean 722: */ 723: public boolean drawImage(Image image, int x, int y, 724: ImageObserver observer) 725: { 726: return graphics.drawImage(image, x, y, observer); 727: } 728: 729: /** 730: * drawImage 731: * 732: * @param image The image to draw 733: * @param x The x position 734: * @param y The y position 735: * @param width The width of the area to draw the image 736: * @param height The height of the area to draw the image 737: * @param observer The image observer 738: * 739: * @return boolean 740: */ 741: public boolean drawImage(Image image, int x, int y, int width, 742: int height, ImageObserver observer) 743: { 744: return graphics.drawImage(image, x, y, width, height, observer); 745: } 746: 747: /** 748: * drawImage 749: * 750: * @param image The image to draw 751: * @param x The x position 752: * @param y The y position 753: * @param background The color for the background in the opaque regions 754: * of the image 755: * @param observer The image observer 756: * 757: * @return boolean 758: */ 759: public boolean drawImage(Image image, int x, int y, 760: Color background, ImageObserver observer) 761: { 762: return graphics.drawImage(image, x, y, background, observer); 763: } 764: 765: /** 766: * drawImage 767: * 768: * @param image The image to draw 769: * @param x The x position 770: * @param y The y position 771: * @param width The width of the area to draw the image 772: * @param height The height of the area to draw the image 773: * @param background The color for the background in the opaque regions 774: * of the image 775: * @param observer The image observer 776: * 777: * @return boolean 778: */ 779: public boolean drawImage(Image image, int x, int y, int width, int height, 780: Color background, ImageObserver observer) 781: { 782: return graphics.drawImage(image, x, y, width, height, background, observer); 783: } 784: 785: /** 786: * drawImage 787: * 788: * @param image The image to draw 789: * @param dx1 TODO 790: * @param dy1 TODO 791: * @param dx2 TODO 792: * @param dy2 TODO 793: * @param sx1 TODO 794: * @param sy1 TODO 795: * @param sx2 TODO 796: * @param sy2 TODO 797: * @param observer The image observer 798: * 799: * @return boolean 800: */ 801: public boolean drawImage(Image image, int dx1, int dy1, 802: int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, 803: ImageObserver observer) 804: { 805: return graphics.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer); 806: } 807: 808: /** 809: * drawImage 810: * 811: * @param image The image to draw 812: * @param dx1 TODO 813: * @param dy1 TODO 814: * @param dx2 TODO 815: * @param dy2 TODO 816: * @param sx1 TODO 817: * @param sy1 TODO 818: * @param sx2 TODO 819: * @param sy2 TODO 820: * @param background The color for the background in the opaque regions 821: * of the image 822: * @param observer The image observer 823: * 824: * @return boolean 825: */ 826: public boolean drawImage(Image image, int dx1, int dy1, 827: int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, 828: Color background, ImageObserver observer) 829: { 830: return graphics.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, background, observer); 831: } 832: 833: /** 834: * copyArea 835: * 836: * @param x The x position of the source area 837: * @param y The y position of the source area 838: * @param width The width of the area 839: * @param height The height of the area 840: * @param destx The x position of the destination area 841: * @param desty The y posiiton of the destination area 842: */ 843: public void copyArea(int x, int y, int width, int height, 844: int destx, int desty) 845: { 846: graphics.copyArea(x, y, width, height, destx, desty); 847: } 848: 849: /** 850: * Releases all system resources that this <code>Graphics</code> is using. 851: */ 852: public void dispose() 853: { 854: graphics.dispose(); 855: graphics = null; 856: } 857: 858: /** 859: * isDrawingBuffer 860: * 861: * @return boolean 862: */ 863: public boolean isDrawingBuffer() 864: { 865: return false; // TODO 866: } 867: 868: /** 869: * setDebugOptions 870: * 871: * @param options the debug options 872: */ 873: public void setDebugOptions(int options) 874: { 875: debugOptions = options; 876: } 877: 878: /** 879: * getDebugOptions 880: * 881: * @return the debug options 882: */ 883: public int getDebugOptions() 884: { 885: return debugOptions; 886: } 887: }
GNU Classpath (0.18) |