Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/DebugGraphics.java
38829 views
/*1* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.swing;2627import java.awt.*;28import java.awt.image.*;29import java.text.AttributedCharacterIterator;3031/**32* Graphics subclass supporting graphics debugging. Overrides most methods33* from Graphics. DebugGraphics objects are rarely created by hand. They34* are most frequently created automatically when a JComponent's35* debugGraphicsOptions are changed using the setDebugGraphicsOptions()36* method.37* <p>38* NOTE: You must turn off double buffering to use DebugGraphics:39* RepaintManager repaintManager = RepaintManager.currentManager(component);40* repaintManager.setDoubleBufferingEnabled(false);41*42* @see JComponent#setDebugGraphicsOptions43* @see RepaintManager#currentManager44* @see RepaintManager#setDoubleBufferingEnabled45*46* @author Dave Karlton47*/48public class DebugGraphics extends Graphics {49Graphics graphics;50Image buffer;51int debugOptions;52int graphicsID = graphicsCount++;53int xOffset, yOffset;54private static int graphicsCount = 0;55private static ImageIcon imageLoadingIcon = new ImageIcon();5657/** Log graphics operations. */58public static final int LOG_OPTION = 1 << 0;59/** Flash graphics operations. */60public static final int FLASH_OPTION = 1 << 1;61/** Show buffered operations in a separate <code>Frame</code>. */62public static final int BUFFERED_OPTION = 1 << 2;63/** Don't debug graphics operations. */64public static final int NONE_OPTION = -1;6566static {67JComponent.DEBUG_GRAPHICS_LOADED = true;68}6970/**71* Constructs a new debug graphics context that supports slowed72* down drawing.73*/74public DebugGraphics() {75super();76buffer = null;77xOffset = yOffset = 0;78}7980/**81* Constructs a debug graphics context from an existing graphics82* context that slows down drawing for the specified component.83*84* @param graphics the Graphics context to slow down85* @param component the JComponent to draw slowly86*/87public DebugGraphics(Graphics graphics, JComponent component) {88this(graphics);89setDebugOptions(component.shouldDebugGraphics());90}9192/**93* Constructs a debug graphics context from an existing graphics94* context that supports slowed down drawing.95*96* @param graphics the Graphics context to slow down97*/98public DebugGraphics(Graphics graphics) {99this();100this.graphics = graphics;101}102103/**104* Overrides <code>Graphics.create</code> to return a DebugGraphics object.105*/106public Graphics create() {107DebugGraphics debugGraphics;108109debugGraphics = new DebugGraphics();110debugGraphics.graphics = graphics.create();111debugGraphics.debugOptions = debugOptions;112debugGraphics.buffer = buffer;113114return debugGraphics;115}116117/**118* Overrides <code>Graphics.create</code> to return a DebugGraphics object.119*/120public Graphics create(int x, int y, int width, int height) {121DebugGraphics debugGraphics;122123debugGraphics = new DebugGraphics();124debugGraphics.graphics = graphics.create(x, y, width, height);125debugGraphics.debugOptions = debugOptions;126debugGraphics.buffer = buffer;127debugGraphics.xOffset = xOffset + x;128debugGraphics.yOffset = yOffset + y;129130return debugGraphics;131}132133134//------------------------------------------------135// NEW METHODS136//------------------------------------------------137138/**139* Sets the Color used to flash drawing operations.140*/141public static void setFlashColor(Color flashColor) {142info().flashColor = flashColor;143}144145/**146* Returns the Color used to flash drawing operations.147* @see #setFlashColor148*/149public static Color flashColor() {150return info().flashColor;151}152153/**154* Sets the time delay of drawing operation flashing.155*/156public static void setFlashTime(int flashTime) {157info().flashTime = flashTime;158}159160/**161* Returns the time delay of drawing operation flashing.162* @see #setFlashTime163*/164public static int flashTime() {165return info().flashTime;166}167168/**169* Sets the number of times that drawing operations will flash.170*/171public static void setFlashCount(int flashCount) {172info().flashCount = flashCount;173}174175/** Returns the number of times that drawing operations will flash.176* @see #setFlashCount177*/178public static int flashCount() {179return info().flashCount;180}181182/** Sets the stream to which the DebugGraphics logs drawing operations.183*/184public static void setLogStream(java.io.PrintStream stream) {185info().stream = stream;186}187188/** Returns the stream to which the DebugGraphics logs drawing operations.189* @see #setLogStream190*/191public static java.io.PrintStream logStream() {192return info().stream;193}194195/** Sets the Font used for text drawing operations.196*/197public void setFont(Font aFont) {198if (debugLog()) {199info().log(toShortString() + " Setting font: " + aFont);200}201graphics.setFont(aFont);202}203204/** Returns the Font used for text drawing operations.205* @see #setFont206*/207public Font getFont() {208return graphics.getFont();209}210211/** Sets the color to be used for drawing and filling lines and shapes.212*/213public void setColor(Color aColor) {214if (debugLog()) {215info().log(toShortString() + " Setting color: " + aColor);216}217graphics.setColor(aColor);218}219220/** Returns the Color used for text drawing operations.221* @see #setColor222*/223public Color getColor() {224return graphics.getColor();225}226227228//-----------------------------------------------229// OVERRIDDEN METHODS230//------------------------------------------------231232/**233* Overrides <code>Graphics.getFontMetrics</code>.234*/235public FontMetrics getFontMetrics() {236return graphics.getFontMetrics();237}238239/**240* Overrides <code>Graphics.getFontMetrics</code>.241*/242public FontMetrics getFontMetrics(Font f) {243return graphics.getFontMetrics(f);244}245246/**247* Overrides <code>Graphics.translate</code>.248*/249public void translate(int x, int y) {250if (debugLog()) {251info().log(toShortString() +252" Translating by: " + new Point(x, y));253}254xOffset += x;255yOffset += y;256graphics.translate(x, y);257}258259/**260* Overrides <code>Graphics.setPaintMode</code>.261*/262public void setPaintMode() {263if (debugLog()) {264info().log(toShortString() + " Setting paint mode");265}266graphics.setPaintMode();267}268269/**270* Overrides <code>Graphics.setXORMode</code>.271*/272public void setXORMode(Color aColor) {273if (debugLog()) {274info().log(toShortString() + " Setting XOR mode: " + aColor);275}276graphics.setXORMode(aColor);277}278279/**280* Overrides <code>Graphics.getClipBounds</code>.281*/282public Rectangle getClipBounds() {283return graphics.getClipBounds();284}285286/**287* Overrides <code>Graphics.clipRect</code>.288*/289public void clipRect(int x, int y, int width, int height) {290graphics.clipRect(x, y, width, height);291if (debugLog()) {292info().log(toShortString() +293" Setting clipRect: " + (new Rectangle(x, y, width, height)) +294" New clipRect: " + graphics.getClip());295}296}297298/**299* Overrides <code>Graphics.setClip</code>.300*/301public void setClip(int x, int y, int width, int height) {302graphics.setClip(x, y, width, height);303if (debugLog()) {304info().log(toShortString() +305" Setting new clipRect: " + graphics.getClip());306}307}308309/**310* Overrides <code>Graphics.getClip</code>.311*/312public Shape getClip() {313return graphics.getClip();314}315316/**317* Overrides <code>Graphics.setClip</code>.318*/319public void setClip(Shape clip) {320graphics.setClip(clip);321if (debugLog()) {322info().log(toShortString() +323" Setting new clipRect: " + graphics.getClip());324}325}326327/**328* Overrides <code>Graphics.drawRect</code>.329*/330public void drawRect(int x, int y, int width, int height) {331DebugGraphicsInfo info = info();332333if (debugLog()) {334info().log(toShortString() +335" Drawing rect: " +336new Rectangle(x, y, width, height));337}338339if (isDrawingBuffer()) {340if (debugBuffered()) {341Graphics debugGraphics = debugGraphics();342343debugGraphics.drawRect(x, y, width, height);344debugGraphics.dispose();345}346} else if (debugFlash()) {347Color oldColor = getColor();348int i, count = (info.flashCount * 2) - 1;349350for (i = 0; i < count; i++) {351graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);352graphics.drawRect(x, y, width, height);353Toolkit.getDefaultToolkit().sync();354sleep(info.flashTime);355}356graphics.setColor(oldColor);357}358graphics.drawRect(x, y, width, height);359}360361/**362* Overrides <code>Graphics.fillRect</code>.363*/364public void fillRect(int x, int y, int width, int height) {365DebugGraphicsInfo info = info();366367if (debugLog()) {368info().log(toShortString() +369" Filling rect: " +370new Rectangle(x, y, width, height));371}372373if (isDrawingBuffer()) {374if (debugBuffered()) {375Graphics debugGraphics = debugGraphics();376377debugGraphics.fillRect(x, y, width, height);378debugGraphics.dispose();379}380} else if (debugFlash()) {381Color oldColor = getColor();382int i, count = (info.flashCount * 2) - 1;383384for (i = 0; i < count; i++) {385graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);386graphics.fillRect(x, y, width, height);387Toolkit.getDefaultToolkit().sync();388sleep(info.flashTime);389}390graphics.setColor(oldColor);391}392graphics.fillRect(x, y, width, height);393}394395/**396* Overrides <code>Graphics.clearRect</code>.397*/398public void clearRect(int x, int y, int width, int height) {399DebugGraphicsInfo info = info();400401if (debugLog()) {402info().log(toShortString() +403" Clearing rect: " +404new Rectangle(x, y, width, height));405}406407if (isDrawingBuffer()) {408if (debugBuffered()) {409Graphics debugGraphics = debugGraphics();410411debugGraphics.clearRect(x, y, width, height);412debugGraphics.dispose();413}414} else if (debugFlash()) {415Color oldColor = getColor();416int i, count = (info.flashCount * 2) - 1;417418for (i = 0; i < count; i++) {419graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);420graphics.clearRect(x, y, width, height);421Toolkit.getDefaultToolkit().sync();422sleep(info.flashTime);423}424graphics.setColor(oldColor);425}426graphics.clearRect(x, y, width, height);427}428429/**430* Overrides <code>Graphics.drawRoundRect</code>.431*/432public void drawRoundRect(int x, int y, int width, int height,433int arcWidth, int arcHeight) {434DebugGraphicsInfo info = info();435436if (debugLog()) {437info().log(toShortString() +438" Drawing round rect: " +439new Rectangle(x, y, width, height) +440" arcWidth: " + arcWidth +441" archHeight: " + arcHeight);442}443if (isDrawingBuffer()) {444if (debugBuffered()) {445Graphics debugGraphics = debugGraphics();446447debugGraphics.drawRoundRect(x, y, width, height,448arcWidth, arcHeight);449debugGraphics.dispose();450}451} else if (debugFlash()) {452Color oldColor = getColor();453int i, count = (info.flashCount * 2) - 1;454455for (i = 0; i < count; i++) {456graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);457graphics.drawRoundRect(x, y, width, height,458arcWidth, arcHeight);459Toolkit.getDefaultToolkit().sync();460sleep(info.flashTime);461}462graphics.setColor(oldColor);463}464graphics.drawRoundRect(x, y, width, height, arcWidth, arcHeight);465}466467/**468* Overrides <code>Graphics.fillRoundRect</code>.469*/470public void fillRoundRect(int x, int y, int width, int height,471int arcWidth, int arcHeight) {472DebugGraphicsInfo info = info();473474if (debugLog()) {475info().log(toShortString() +476" Filling round rect: " +477new Rectangle(x, y, width, height) +478" arcWidth: " + arcWidth +479" archHeight: " + arcHeight);480}481if (isDrawingBuffer()) {482if (debugBuffered()) {483Graphics debugGraphics = debugGraphics();484485debugGraphics.fillRoundRect(x, y, width, height,486arcWidth, arcHeight);487debugGraphics.dispose();488}489} else if (debugFlash()) {490Color oldColor = getColor();491int i, count = (info.flashCount * 2) - 1;492493for (i = 0; i < count; i++) {494graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);495graphics.fillRoundRect(x, y, width, height,496arcWidth, arcHeight);497Toolkit.getDefaultToolkit().sync();498sleep(info.flashTime);499}500graphics.setColor(oldColor);501}502graphics.fillRoundRect(x, y, width, height, arcWidth, arcHeight);503}504505/**506* Overrides <code>Graphics.drawLine</code>.507*/508public void drawLine(int x1, int y1, int x2, int y2) {509DebugGraphicsInfo info = info();510511if (debugLog()) {512info().log(toShortString() +513" Drawing line: from " + pointToString(x1, y1) +514" to " + pointToString(x2, y2));515}516517if (isDrawingBuffer()) {518if (debugBuffered()) {519Graphics debugGraphics = debugGraphics();520521debugGraphics.drawLine(x1, y1, x2, y2);522debugGraphics.dispose();523}524} else if (debugFlash()) {525Color oldColor = getColor();526int i, count = (info.flashCount * 2) - 1;527528for (i = 0; i < count; i++) {529graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);530graphics.drawLine(x1, y1, x2, y2);531Toolkit.getDefaultToolkit().sync();532sleep(info.flashTime);533}534graphics.setColor(oldColor);535}536graphics.drawLine(x1, y1, x2, y2);537}538539/**540* Overrides <code>Graphics.draw3DRect</code>.541*/542public void draw3DRect(int x, int y, int width, int height,543boolean raised) {544DebugGraphicsInfo info = info();545546if (debugLog()) {547info().log(toShortString() +548" Drawing 3D rect: " +549new Rectangle(x, y, width, height) +550" Raised bezel: " + raised);551}552if (isDrawingBuffer()) {553if (debugBuffered()) {554Graphics debugGraphics = debugGraphics();555556debugGraphics.draw3DRect(x, y, width, height, raised);557debugGraphics.dispose();558}559} else if (debugFlash()) {560Color oldColor = getColor();561int i, count = (info.flashCount * 2) - 1;562563for (i = 0; i < count; i++) {564graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);565graphics.draw3DRect(x, y, width, height, raised);566Toolkit.getDefaultToolkit().sync();567sleep(info.flashTime);568}569graphics.setColor(oldColor);570}571graphics.draw3DRect(x, y, width, height, raised);572}573574/**575* Overrides <code>Graphics.fill3DRect</code>.576*/577public void fill3DRect(int x, int y, int width, int height,578boolean raised) {579DebugGraphicsInfo info = info();580581if (debugLog()) {582info().log(toShortString() +583" Filling 3D rect: " +584new Rectangle(x, y, width, height) +585" Raised bezel: " + raised);586}587if (isDrawingBuffer()) {588if (debugBuffered()) {589Graphics debugGraphics = debugGraphics();590591debugGraphics.fill3DRect(x, y, width, height, raised);592debugGraphics.dispose();593}594} else if (debugFlash()) {595Color oldColor = getColor();596int i, count = (info.flashCount * 2) - 1;597598for (i = 0; i < count; i++) {599graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);600graphics.fill3DRect(x, y, width, height, raised);601Toolkit.getDefaultToolkit().sync();602sleep(info.flashTime);603}604graphics.setColor(oldColor);605}606graphics.fill3DRect(x, y, width, height, raised);607}608609/**610* Overrides <code>Graphics.drawOval</code>.611*/612public void drawOval(int x, int y, int width, int height) {613DebugGraphicsInfo info = info();614615if (debugLog()) {616info().log(toShortString() +617" Drawing oval: " +618new Rectangle(x, y, width, height));619}620if (isDrawingBuffer()) {621if (debugBuffered()) {622Graphics debugGraphics = debugGraphics();623624debugGraphics.drawOval(x, y, width, height);625debugGraphics.dispose();626}627} else if (debugFlash()) {628Color oldColor = getColor();629int i, count = (info.flashCount * 2) - 1;630631for (i = 0; i < count; i++) {632graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);633graphics.drawOval(x, y, width, height);634Toolkit.getDefaultToolkit().sync();635sleep(info.flashTime);636}637graphics.setColor(oldColor);638}639graphics.drawOval(x, y, width, height);640}641642/**643* Overrides <code>Graphics.fillOval</code>.644*/645public void fillOval(int x, int y, int width, int height) {646DebugGraphicsInfo info = info();647648if (debugLog()) {649info().log(toShortString() +650" Filling oval: " +651new Rectangle(x, y, width, height));652}653if (isDrawingBuffer()) {654if (debugBuffered()) {655Graphics debugGraphics = debugGraphics();656657debugGraphics.fillOval(x, y, width, height);658debugGraphics.dispose();659}660} else if (debugFlash()) {661Color oldColor = getColor();662int i, count = (info.flashCount * 2) - 1;663664for (i = 0; i < count; i++) {665graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);666graphics.fillOval(x, y, width, height);667Toolkit.getDefaultToolkit().sync();668sleep(info.flashTime);669}670graphics.setColor(oldColor);671}672graphics.fillOval(x, y, width, height);673}674675/**676* Overrides <code>Graphics.drawArc</code>.677*/678public void drawArc(int x, int y, int width, int height,679int startAngle, int arcAngle) {680DebugGraphicsInfo info = info();681682if (debugLog()) {683info().log(toShortString() +684" Drawing arc: " +685new Rectangle(x, y, width, height) +686" startAngle: " + startAngle +687" arcAngle: " + arcAngle);688}689if (isDrawingBuffer()) {690if (debugBuffered()) {691Graphics debugGraphics = debugGraphics();692693debugGraphics.drawArc(x, y, width, height,694startAngle, arcAngle);695debugGraphics.dispose();696}697} else if (debugFlash()) {698Color oldColor = getColor();699int i, count = (info.flashCount * 2) - 1;700701for (i = 0; i < count; i++) {702graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);703graphics.drawArc(x, y, width, height, startAngle, arcAngle);704Toolkit.getDefaultToolkit().sync();705sleep(info.flashTime);706}707graphics.setColor(oldColor);708}709graphics.drawArc(x, y, width, height, startAngle, arcAngle);710}711712/**713* Overrides <code>Graphics.fillArc</code>.714*/715public void fillArc(int x, int y, int width, int height,716int startAngle, int arcAngle) {717DebugGraphicsInfo info = info();718719if (debugLog()) {720info().log(toShortString() +721" Filling arc: " +722new Rectangle(x, y, width, height) +723" startAngle: " + startAngle +724" arcAngle: " + arcAngle);725}726if (isDrawingBuffer()) {727if (debugBuffered()) {728Graphics debugGraphics = debugGraphics();729730debugGraphics.fillArc(x, y, width, height,731startAngle, arcAngle);732debugGraphics.dispose();733}734} else if (debugFlash()) {735Color oldColor = getColor();736int i, count = (info.flashCount * 2) - 1;737738for (i = 0; i < count; i++) {739graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);740graphics.fillArc(x, y, width, height, startAngle, arcAngle);741Toolkit.getDefaultToolkit().sync();742sleep(info.flashTime);743}744graphics.setColor(oldColor);745}746graphics.fillArc(x, y, width, height, startAngle, arcAngle);747}748749/**750* Overrides <code>Graphics.drawPolyline</code>.751*/752public void drawPolyline(int xPoints[], int yPoints[], int nPoints) {753DebugGraphicsInfo info = info();754755if (debugLog()) {756info().log(toShortString() +757" Drawing polyline: " +758" nPoints: " + nPoints +759" X's: " + xPoints +760" Y's: " + yPoints);761}762if (isDrawingBuffer()) {763if (debugBuffered()) {764Graphics debugGraphics = debugGraphics();765766debugGraphics.drawPolyline(xPoints, yPoints, nPoints);767debugGraphics.dispose();768}769} else if (debugFlash()) {770Color oldColor = getColor();771int i, count = (info.flashCount * 2) - 1;772773for (i = 0; i < count; i++) {774graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);775graphics.drawPolyline(xPoints, yPoints, nPoints);776Toolkit.getDefaultToolkit().sync();777sleep(info.flashTime);778}779graphics.setColor(oldColor);780}781graphics.drawPolyline(xPoints, yPoints, nPoints);782}783784/**785* Overrides <code>Graphics.drawPolygon</code>.786*/787public void drawPolygon(int xPoints[], int yPoints[], int nPoints) {788DebugGraphicsInfo info = info();789790if (debugLog()) {791info().log(toShortString() +792" Drawing polygon: " +793" nPoints: " + nPoints +794" X's: " + xPoints +795" Y's: " + yPoints);796}797if (isDrawingBuffer()) {798if (debugBuffered()) {799Graphics debugGraphics = debugGraphics();800801debugGraphics.drawPolygon(xPoints, yPoints, nPoints);802debugGraphics.dispose();803}804} else if (debugFlash()) {805Color oldColor = getColor();806int i, count = (info.flashCount * 2) - 1;807808for (i = 0; i < count; i++) {809graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);810graphics.drawPolygon(xPoints, yPoints, nPoints);811Toolkit.getDefaultToolkit().sync();812sleep(info.flashTime);813}814graphics.setColor(oldColor);815}816graphics.drawPolygon(xPoints, yPoints, nPoints);817}818819/**820* Overrides <code>Graphics.fillPolygon</code>.821*/822public void fillPolygon(int xPoints[], int yPoints[], int nPoints) {823DebugGraphicsInfo info = info();824825if (debugLog()) {826info().log(toShortString() +827" Filling polygon: " +828" nPoints: " + nPoints +829" X's: " + xPoints +830" Y's: " + yPoints);831}832if (isDrawingBuffer()) {833if (debugBuffered()) {834Graphics debugGraphics = debugGraphics();835836debugGraphics.fillPolygon(xPoints, yPoints, nPoints);837debugGraphics.dispose();838}839} else if (debugFlash()) {840Color oldColor = getColor();841int i, count = (info.flashCount * 2) - 1;842843for (i = 0; i < count; i++) {844graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);845graphics.fillPolygon(xPoints, yPoints, nPoints);846Toolkit.getDefaultToolkit().sync();847sleep(info.flashTime);848}849graphics.setColor(oldColor);850}851graphics.fillPolygon(xPoints, yPoints, nPoints);852}853854/**855* Overrides <code>Graphics.drawString</code>.856*/857public void drawString(String aString, int x, int y) {858DebugGraphicsInfo info = info();859860if (debugLog()) {861info().log(toShortString() +862" Drawing string: \"" + aString +863"\" at: " + new Point(x, y));864}865866if (isDrawingBuffer()) {867if (debugBuffered()) {868Graphics debugGraphics = debugGraphics();869870debugGraphics.drawString(aString, x, y);871debugGraphics.dispose();872}873} else if (debugFlash()) {874Color oldColor = getColor();875int i, count = (info.flashCount * 2) - 1;876877for (i = 0; i < count; i++) {878graphics.setColor((i % 2) == 0 ? info.flashColor879: oldColor);880graphics.drawString(aString, x, y);881Toolkit.getDefaultToolkit().sync();882sleep(info.flashTime);883}884graphics.setColor(oldColor);885}886graphics.drawString(aString, x, y);887}888889/**890* Overrides <code>Graphics.drawString</code>.891*/892public void drawString(AttributedCharacterIterator iterator, int x, int y) {893DebugGraphicsInfo info = info();894895if (debugLog()) {896info().log(toShortString() +897" Drawing text: \"" + iterator +898"\" at: " + new Point(x, y));899}900901if (isDrawingBuffer()) {902if (debugBuffered()) {903Graphics debugGraphics = debugGraphics();904905debugGraphics.drawString(iterator, x, y);906debugGraphics.dispose();907}908} else if (debugFlash()) {909Color oldColor = getColor();910int i, count = (info.flashCount * 2) - 1;911912for (i = 0; i < count; i++) {913graphics.setColor((i % 2) == 0 ? info.flashColor914: oldColor);915graphics.drawString(iterator, x, y);916Toolkit.getDefaultToolkit().sync();917sleep(info.flashTime);918}919graphics.setColor(oldColor);920}921graphics.drawString(iterator, x, y);922}923924/**925* Overrides <code>Graphics.drawBytes</code>.926*/927public void drawBytes(byte data[], int offset, int length, int x, int y) {928DebugGraphicsInfo info = info();929930Font font = graphics.getFont();931932if (debugLog()) {933info().log(toShortString() +934" Drawing bytes at: " + new Point(x, y));935}936937if (isDrawingBuffer()) {938if (debugBuffered()) {939Graphics debugGraphics = debugGraphics();940941debugGraphics.drawBytes(data, offset, length, x, y);942debugGraphics.dispose();943}944} else if (debugFlash()) {945Color oldColor = getColor();946int i, count = (info.flashCount * 2) - 1;947948for (i = 0; i < count; i++) {949graphics.setColor((i % 2) == 0 ? info.flashColor950: oldColor);951graphics.drawBytes(data, offset, length, x, y);952Toolkit.getDefaultToolkit().sync();953sleep(info.flashTime);954}955graphics.setColor(oldColor);956}957graphics.drawBytes(data, offset, length, x, y);958}959960/**961* Overrides <code>Graphics.drawChars</code>.962*/963public void drawChars(char data[], int offset, int length, int x, int y) {964DebugGraphicsInfo info = info();965966Font font = graphics.getFont();967968if (debugLog()) {969info().log(toShortString() +970" Drawing chars at " + new Point(x, y));971}972973if (isDrawingBuffer()) {974if (debugBuffered()) {975Graphics debugGraphics = debugGraphics();976977debugGraphics.drawChars(data, offset, length, x, y);978debugGraphics.dispose();979}980} else if (debugFlash()) {981Color oldColor = getColor();982int i, count = (info.flashCount * 2) - 1;983984for (i = 0; i < count; i++) {985graphics.setColor((i % 2) == 0 ? info.flashColor986: oldColor);987graphics.drawChars(data, offset, length, x, y);988Toolkit.getDefaultToolkit().sync();989sleep(info.flashTime);990}991graphics.setColor(oldColor);992}993graphics.drawChars(data, offset, length, x, y);994}995996/**997* Overrides <code>Graphics.drawImage</code>.998*/999public boolean drawImage(Image img, int x, int y,1000ImageObserver observer) {1001DebugGraphicsInfo info = info();10021003if (debugLog()) {1004info.log(toShortString() +1005" Drawing image: " + img +1006" at: " + new Point(x, y));1007}10081009if (isDrawingBuffer()) {1010if (debugBuffered()) {1011Graphics debugGraphics = debugGraphics();10121013debugGraphics.drawImage(img, x, y, observer);1014debugGraphics.dispose();1015}1016} else if (debugFlash()) {1017int i, count = (info.flashCount * 2) - 1;1018ImageProducer oldProducer = img.getSource();1019ImageProducer newProducer1020= new FilteredImageSource(oldProducer,1021new DebugGraphicsFilter(info.flashColor));1022Image newImage1023= Toolkit.getDefaultToolkit().createImage(newProducer);1024DebugGraphicsObserver imageObserver1025= new DebugGraphicsObserver();10261027Image imageToDraw;1028for (i = 0; i < count; i++) {1029imageToDraw = (i % 2) == 0 ? newImage : img;1030loadImage(imageToDraw);1031graphics.drawImage(imageToDraw, x, y,1032imageObserver);1033Toolkit.getDefaultToolkit().sync();1034sleep(info.flashTime);1035}1036}1037return graphics.drawImage(img, x, y, observer);1038}10391040/**1041* Overrides <code>Graphics.drawImage</code>.1042*/1043public boolean drawImage(Image img, int x, int y, int width, int height,1044ImageObserver observer) {1045DebugGraphicsInfo info = info();10461047if (debugLog()) {1048info.log(toShortString() +1049" Drawing image: " + img +1050" at: " + new Rectangle(x, y, width, height));1051}10521053if (isDrawingBuffer()) {1054if (debugBuffered()) {1055Graphics debugGraphics = debugGraphics();10561057debugGraphics.drawImage(img, x, y, width, height, observer);1058debugGraphics.dispose();1059}1060} else if (debugFlash()) {1061int i, count = (info.flashCount * 2) - 1;1062ImageProducer oldProducer = img.getSource();1063ImageProducer newProducer1064= new FilteredImageSource(oldProducer,1065new DebugGraphicsFilter(info.flashColor));1066Image newImage1067= Toolkit.getDefaultToolkit().createImage(newProducer);1068DebugGraphicsObserver imageObserver1069= new DebugGraphicsObserver();10701071Image imageToDraw;1072for (i = 0; i < count; i++) {1073imageToDraw = (i % 2) == 0 ? newImage : img;1074loadImage(imageToDraw);1075graphics.drawImage(imageToDraw, x, y,1076width, height, imageObserver);1077Toolkit.getDefaultToolkit().sync();1078sleep(info.flashTime);1079}1080}1081return graphics.drawImage(img, x, y, width, height, observer);1082}10831084/**1085* Overrides <code>Graphics.drawImage</code>.1086*/1087public boolean drawImage(Image img, int x, int y,1088Color bgcolor,1089ImageObserver observer) {1090DebugGraphicsInfo info = info();10911092if (debugLog()) {1093info.log(toShortString() +1094" Drawing image: " + img +1095" at: " + new Point(x, y) +1096", bgcolor: " + bgcolor);1097}10981099if (isDrawingBuffer()) {1100if (debugBuffered()) {1101Graphics debugGraphics = debugGraphics();11021103debugGraphics.drawImage(img, x, y, bgcolor, observer);1104debugGraphics.dispose();1105}1106} else if (debugFlash()) {1107int i, count = (info.flashCount * 2) - 1;1108ImageProducer oldProducer = img.getSource();1109ImageProducer newProducer1110= new FilteredImageSource(oldProducer,1111new DebugGraphicsFilter(info.flashColor));1112Image newImage1113= Toolkit.getDefaultToolkit().createImage(newProducer);1114DebugGraphicsObserver imageObserver1115= new DebugGraphicsObserver();11161117Image imageToDraw;1118for (i = 0; i < count; i++) {1119imageToDraw = (i % 2) == 0 ? newImage : img;1120loadImage(imageToDraw);1121graphics.drawImage(imageToDraw, x, y,1122bgcolor, imageObserver);1123Toolkit.getDefaultToolkit().sync();1124sleep(info.flashTime);1125}1126}1127return graphics.drawImage(img, x, y, bgcolor, observer);1128}11291130/**1131* Overrides <code>Graphics.drawImage</code>.1132*/1133public boolean drawImage(Image img, int x, int y,int width, int height,1134Color bgcolor,1135ImageObserver observer) {1136DebugGraphicsInfo info = info();11371138if (debugLog()) {1139info.log(toShortString() +1140" Drawing image: " + img +1141" at: " + new Rectangle(x, y, width, height) +1142", bgcolor: " + bgcolor);1143}11441145if (isDrawingBuffer()) {1146if (debugBuffered()) {1147Graphics debugGraphics = debugGraphics();11481149debugGraphics.drawImage(img, x, y, width, height,1150bgcolor, observer);1151debugGraphics.dispose();1152}1153} else if (debugFlash()) {1154int i, count = (info.flashCount * 2) - 1;1155ImageProducer oldProducer = img.getSource();1156ImageProducer newProducer1157= new FilteredImageSource(oldProducer,1158new DebugGraphicsFilter(info.flashColor));1159Image newImage1160= Toolkit.getDefaultToolkit().createImage(newProducer);1161DebugGraphicsObserver imageObserver1162= new DebugGraphicsObserver();11631164Image imageToDraw;1165for (i = 0; i < count; i++) {1166imageToDraw = (i % 2) == 0 ? newImage : img;1167loadImage(imageToDraw);1168graphics.drawImage(imageToDraw, x, y,1169width, height, bgcolor, imageObserver);1170Toolkit.getDefaultToolkit().sync();1171sleep(info.flashTime);1172}1173}1174return graphics.drawImage(img, x, y, width, height, bgcolor, observer);1175}11761177/**1178* Overrides <code>Graphics.drawImage</code>.1179*/1180public boolean drawImage(Image img,1181int dx1, int dy1, int dx2, int dy2,1182int sx1, int sy1, int sx2, int sy2,1183ImageObserver observer) {1184DebugGraphicsInfo info = info();11851186if (debugLog()) {1187info.log(toShortString() +1188" Drawing image: " + img +1189" destination: " + new Rectangle(dx1, dy1, dx2, dy2) +1190" source: " + new Rectangle(sx1, sy1, sx2, sy2));1191}11921193if (isDrawingBuffer()) {1194if (debugBuffered()) {1195Graphics debugGraphics = debugGraphics();11961197debugGraphics.drawImage(img, dx1, dy1, dx2, dy2,1198sx1, sy1, sx2, sy2, observer);1199debugGraphics.dispose();1200}1201} else if (debugFlash()) {1202int i, count = (info.flashCount * 2) - 1;1203ImageProducer oldProducer = img.getSource();1204ImageProducer newProducer1205= new FilteredImageSource(oldProducer,1206new DebugGraphicsFilter(info.flashColor));1207Image newImage1208= Toolkit.getDefaultToolkit().createImage(newProducer);1209DebugGraphicsObserver imageObserver1210= new DebugGraphicsObserver();12111212Image imageToDraw;1213for (i = 0; i < count; i++) {1214imageToDraw = (i % 2) == 0 ? newImage : img;1215loadImage(imageToDraw);1216graphics.drawImage(imageToDraw,1217dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2,1218imageObserver);1219Toolkit.getDefaultToolkit().sync();1220sleep(info.flashTime);1221}1222}1223return graphics.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2,1224observer);1225}12261227/**1228* Overrides <code>Graphics.drawImage</code>.1229*/1230public boolean drawImage(Image img,1231int dx1, int dy1, int dx2, int dy2,1232int sx1, int sy1, int sx2, int sy2,1233Color bgcolor,1234ImageObserver observer) {1235DebugGraphicsInfo info = info();12361237if (debugLog()) {1238info.log(toShortString() +1239" Drawing image: " + img +1240" destination: " + new Rectangle(dx1, dy1, dx2, dy2) +1241" source: " + new Rectangle(sx1, sy1, sx2, sy2) +1242", bgcolor: " + bgcolor);1243}12441245if (isDrawingBuffer()) {1246if (debugBuffered()) {1247Graphics debugGraphics = debugGraphics();12481249debugGraphics.drawImage(img, dx1, dy1, dx2, dy2,1250sx1, sy1, sx2, sy2, bgcolor, observer);1251debugGraphics.dispose();1252}1253} else if (debugFlash()) {1254int i, count = (info.flashCount * 2) - 1;1255ImageProducer oldProducer = img.getSource();1256ImageProducer newProducer1257= new FilteredImageSource(oldProducer,1258new DebugGraphicsFilter(info.flashColor));1259Image newImage1260= Toolkit.getDefaultToolkit().createImage(newProducer);1261DebugGraphicsObserver imageObserver1262= new DebugGraphicsObserver();12631264Image imageToDraw;1265for (i = 0; i < count; i++) {1266imageToDraw = (i % 2) == 0 ? newImage : img;1267loadImage(imageToDraw);1268graphics.drawImage(imageToDraw,1269dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2,1270bgcolor, imageObserver);1271Toolkit.getDefaultToolkit().sync();1272sleep(info.flashTime);1273}1274}1275return graphics.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2,1276bgcolor, observer);1277}12781279static void loadImage(Image img) {1280imageLoadingIcon.loadImage(img);1281}128212831284/**1285* Overrides <code>Graphics.copyArea</code>.1286*/1287public void copyArea(int x, int y, int width, int height,1288int destX, int destY) {1289if (debugLog()) {1290info().log(toShortString() +1291" Copying area from: " +1292new Rectangle(x, y, width, height) +1293" to: " + new Point(destX, destY));1294}1295graphics.copyArea(x, y, width, height, destX, destY);1296}12971298final void sleep(int mSecs) {1299try {1300Thread.sleep(mSecs);1301} catch (Exception e) {1302}1303}13041305/**1306* Overrides <code>Graphics.dispose</code>.1307*/1308public void dispose() {1309graphics.dispose();1310graphics = null;1311}13121313// ALERT!1314/**1315* Returns the drawingBuffer value.1316*1317* @return true if this object is drawing from a Buffer1318*/1319public boolean isDrawingBuffer() {1320return buffer != null;1321}13221323String toShortString() {1324return "Graphics" + (isDrawingBuffer() ? "<B>" : "") + "(" + graphicsID + "-" + debugOptions + ")";1325}13261327String pointToString(int x, int y) {1328return "(" + x + ", " + y + ")";1329}13301331/** Enables/disables diagnostic information about every graphics1332* operation. The value of <b>options</b> indicates how this information1333* should be displayed. LOG_OPTION causes a text message to be printed.1334* FLASH_OPTION causes the drawing to flash several times. BUFFERED_OPTION1335* creates a new Frame that shows each operation on an1336* offscreen buffer. The value of <b>options</b> is bitwise OR'd into1337* the current value. To disable debugging use NONE_OPTION.1338*/1339public void setDebugOptions(int options) {1340if (options != 0) {1341if (options == NONE_OPTION) {1342if (debugOptions != 0) {1343System.err.println(toShortString() + " Disabling debug");1344debugOptions = 0;1345}1346} else {1347if (debugOptions != options) {1348debugOptions |= options;1349if (debugLog()) {1350System.err.println(toShortString() + " Enabling debug");1351}1352}1353}1354}1355}13561357/** Returns the current debugging options for this DebugGraphics.1358* @see #setDebugOptions1359*/1360public int getDebugOptions() {1361return debugOptions;1362}13631364/** Static wrapper method for DebugGraphicsInfo.setDebugOptions(). Stores1365* options on a per component basis.1366*/1367static void setDebugOptions(JComponent component, int options) {1368info().setDebugOptions(component, options);1369}13701371/** Static wrapper method for DebugGraphicsInfo.getDebugOptions().1372*/1373static int getDebugOptions(JComponent component) {1374DebugGraphicsInfo debugGraphicsInfo = info();1375if (debugGraphicsInfo == null) {1376return 0;1377} else {1378return debugGraphicsInfo.getDebugOptions(component);1379}1380}13811382/** Returns non-zero if <b>component</b> should display with DebugGraphics,1383* zero otherwise. Walks the JComponent's parent tree to determine if1384* any debugging options have been set.1385*/1386static int shouldComponentDebug(JComponent component) {1387DebugGraphicsInfo info = info();1388if (info == null) {1389return 0;1390} else {1391Container container = (Container)component;1392int debugOptions = 0;13931394while (container != null && (container instanceof JComponent)) {1395debugOptions |= info.getDebugOptions((JComponent)container);1396container = container.getParent();1397}13981399return debugOptions;1400}1401}14021403/** Returns the number of JComponents that have debugging options turned1404* on.1405*/1406static int debugComponentCount() {1407DebugGraphicsInfo debugGraphicsInfo = info();1408if (debugGraphicsInfo != null &&1409debugGraphicsInfo.componentToDebug != null) {1410return debugGraphicsInfo.componentToDebug.size();1411} else {1412return 0;1413}1414}14151416boolean debugLog() {1417return (debugOptions & LOG_OPTION) == LOG_OPTION;1418}14191420boolean debugFlash() {1421return (debugOptions & FLASH_OPTION) == FLASH_OPTION;1422}14231424boolean debugBuffered() {1425return (debugOptions & BUFFERED_OPTION) == BUFFERED_OPTION;1426}14271428/** Returns a DebugGraphics for use in buffering window.1429*/1430private Graphics debugGraphics() {1431DebugGraphics debugGraphics;1432DebugGraphicsInfo info = info();1433JFrame debugFrame;14341435if (info.debugFrame == null) {1436info.debugFrame = new JFrame();1437info.debugFrame.setSize(500, 500);1438}1439debugFrame = info.debugFrame;1440debugFrame.show();1441debugGraphics = new DebugGraphics(debugFrame.getGraphics());1442debugGraphics.setFont(getFont());1443debugGraphics.setColor(getColor());1444debugGraphics.translate(xOffset, yOffset);1445debugGraphics.setClip(getClipBounds());1446if (debugFlash()) {1447debugGraphics.setDebugOptions(FLASH_OPTION);1448}1449return debugGraphics;1450}14511452/** Returns DebugGraphicsInfo, or creates one if none exists.1453*/1454static DebugGraphicsInfo info() {1455DebugGraphicsInfo debugGraphicsInfo = (DebugGraphicsInfo)1456SwingUtilities.appContextGet(debugGraphicsInfoKey);1457if (debugGraphicsInfo == null) {1458debugGraphicsInfo = new DebugGraphicsInfo();1459SwingUtilities.appContextPut(debugGraphicsInfoKey,1460debugGraphicsInfo);1461}1462return debugGraphicsInfo;1463}1464private static final Class debugGraphicsInfoKey = DebugGraphicsInfo.class;146514661467}146814691470