📄 debuggraphics.java
字号:
/* DebugGraphics.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Image;import java.awt.Point;import java.awt.Rectangle;import java.awt.Shape;import java.awt.image.ImageObserver;import java.io.PrintStream;import java.text.AttributedCharacterIterator;/** * An extension of {@link Graphics} that can be used for debugging * custom Swing widgets. <code>DebugGraphics</code> has the ability to * draw slowly and can log drawing actions. * * @author Andrew Selkirk */public class DebugGraphics extends Graphics{ /** * LOG_OPTION */ public static final int LOG_OPTION = 1; /** * FLASH_OPTION */ public static final int FLASH_OPTION = 2; /** * BUFFERED_OPTION */ public static final int BUFFERED_OPTION = 4; /** * NONE_OPTION */ public static final int NONE_OPTION = -1; static Color debugFlashColor = Color.RED; static int debugFlashCount = 10; static int debugFlashTime = 1000; static PrintStream debugLogStream = System.out; /** * Counts the created DebugGraphics objects. This is used by the * logging facility. */ static int counter = 0; /** * graphics */ Graphics graphics; /** * buffer */ Image buffer; /** * debugOptions */ int debugOptions; /** * graphicsID */ int graphicsID; /** * xOffset */ int xOffset; /** * yOffset */ int yOffset; /** * Creates a <code>DebugGraphics</code> object. */ public DebugGraphics() { counter++; } /** * Creates a <code>DebugGraphics</code> object. * * @param graphics The <code>Graphics</code> object to wrap * @param component TODO */ public DebugGraphics(Graphics graphics, JComponent component) { this(graphics); // FIXME: What shall we do with component ? } /** * Creates a <code>DebugGraphics</code> object. * * @param graphics The <code>Graphics</code> object to wrap */ public DebugGraphics(Graphics graphics) { this(); this.graphics = graphics; } /** * Sets the color to draw stuff with. * * @param color The color */ public void setColor(Color color) { if ((debugOptions & LOG_OPTION) != 0) logStream().println(prefix() + " Setting color: " + color); graphics.setColor(color); } /** * Creates a overrides <code>Graphics.create</code> to create a * <code>DebugGraphics</code> object. * * @return a new <code>DebugGraphics</code> object. */ public Graphics create() { DebugGraphics copy = new DebugGraphics(graphics.create()); copy.debugOptions = debugOptions; return copy; } /** * Creates a overrides <code>Graphics.create</code> to create a * <code>DebugGraphics</code> object. * * @param x the x coordinate * @param y the y coordinate * @param width the width * @param height the height * * @return a new <code>DebugGraphics</code> object. */ public Graphics create(int x, int y, int width, int height) { DebugGraphics copy = new DebugGraphics(graphics.create(x, y, width, height)); copy.debugOptions = debugOptions; return copy; } /** * flashColor * * @return Color */ public static Color flashColor() { return debugFlashColor; } /** * setFlashColor * * @param color the color to use for flashing */ public static void setFlashColor(Color color) { debugFlashColor = color; } /** * flashTime * * @return The time in milliseconds */ public static int flashTime() { return debugFlashTime; } /** * setFlashTime * * @param time The time in milliseconds */ public static void setFlashTime(int time) { debugFlashTime = time; } /** * flashCount * * @return The number of flashes */ public static int flashCount() { return debugFlashCount; } /** * setFlashCount * * @param count The number of flashes */ public static void setFlashCount(int count) { debugFlashCount = count; } /** * logStream * * @return The <code>PrintStream</code> to write logging messages to */ public static PrintStream logStream() { return debugLogStream; } /** * setLogStream * * @param stream The currently set <code>PrintStream</code>. */ public static void setLogStream(PrintStream stream) { debugLogStream = stream; } /** * getFont * * @return The font */ public Font getFont() { return graphics.getFont(); } /** * setFont * * @param font The font to use for drawing text */ public void setFont(Font font) { if ((debugOptions & LOG_OPTION) != 0) logStream().println(prefix() + " Setting font: " + font); graphics.setFont(font); } /** * Returns the color used for drawing. * * @return The color. */ public Color getColor() { return graphics.getColor(); } /** * Returns the font metrics of the current font. * * @return a <code>FontMetrics</code> object */ public FontMetrics getFontMetrics() { return graphics.getFontMetrics(); } /** * Returns the font metrics for a given font. * * @param font the font to get the metrics for * * @return a <code>FontMetrics</code> object */ public FontMetrics getFontMetrics(Font font) { return graphics.getFontMetrics(font); } /** * translate * * @param x the x coordinate * @param y the y coordinate */ public void translate(int x, int y) { if ((debugOptions & LOG_OPTION) != 0) logStream().println(prefix() + " Translating by: " + new Point(x, y)); graphics.translate(x, y); } /** * setPaintMode */ public void setPaintMode() { if ((debugOptions & LOG_OPTION) != 0) logStream().println(prefix() + " Setting paint mode"); graphics.setPaintMode(); } /** * setXORMode * * @param color the color */ public void setXORMode(Color color) { if ((debugOptions & LOG_OPTION) != 0) logStream().println(prefix() + " Setting XOR mode: " + color); graphics.setXORMode(color); } /** * getClipBounds * * @return Rectangle */ public Rectangle getClipBounds() { return graphics.getClipBounds(); } /** * Intersects the current clip region with the given region. * * @param x The x-position of the region * @param y The y-position of the region * @param width The width of the region * @param height The height of the region */ public void clipRect(int x, int y, int width, int height) { if ((debugOptions & LOG_OPTION) != 0) { logStream().print(prefix() + " Setting clipRect: " + new Rectangle(x, y, width, height)); } graphics.clipRect(x, y, width, height); if ((debugOptions & LOG_OPTION) != 0) logStream().println(" Netting clipRect: " + graphics.getClipBounds()); } /** * Sets the clipping region. * * @param x The x-position of the region * @param y The y-position of the region * @param width The width of the region * @param height The height of the region */ public void setClip(int x, int y, int width, int height) { if ((debugOptions & LOG_OPTION) != 0) { logStream().println(prefix() + " Setting new clipRect: " + new Rectangle(x, y, width, height)); } graphics.setClip(x, y, width, height); } /** * Returns the current clipping region. * * @return Shape */ public Shape getClip() { return graphics.getClip(); } /** * Sets the current clipping region * * @param shape The clippin region */ public void setClip(Shape shape) { if ((debugOptions & LOG_OPTION) != 0) logStream().println(prefix() + " Setting new clipRect: " + shape); graphics.setClip(shape); } private void sleep(int milliseconds) { try { Thread.sleep(milliseconds); } catch (InterruptedException e) { // Ignore this. } } /** * Draws a rectangle. * * @param x The x-position of the rectangle * @param y The y-position of the rectangle * @param width The width of the rectangle * @param height The height of the rectangle */ public void drawRect(int x, int y, int width, int height) { if ((debugOptions & LOG_OPTION) != 0) { logStream().println(prefix() + " Drawing rect: " + new Rectangle(x, y, width, height)); } if ((debugOptions & FLASH_OPTION) != 0) { Color color = graphics.getColor(); for (int index = 0; index < (debugFlashCount - 1); ++index) { graphics.setColor(color); graphics.drawRect(x, y, width, height); sleep(debugFlashTime); graphics.setColor(debugFlashColor); graphics.drawRect(x, y, width, height); sleep(debugFlashTime); } graphics.setColor(color); } graphics.drawRect(x, y, width, height); } /** * Draws a filled rectangle. * * @param x The x-position of the rectangle * @param y The y-position of the rectangle * @param width The width of the rectangle * @param height The height of the rectangle */ public void fillRect(int x, int y, int width, int height) { if ((debugOptions & LOG_OPTION) != 0) { logStream().println(prefix() + " Filling rect: " + new Rectangle(x, y, width, height)); } if ((debugOptions & FLASH_OPTION) != 0) { Color color = graphics.getColor(); for (int index = 0; index < (debugFlashCount - 1); ++index) { graphics.setColor(color); graphics.fillRect(x, y, width, height); sleep(debugFlashTime); graphics.setColor(debugFlashColor); graphics.fillRect(x, y, width, height); sleep(debugFlashTime); } graphics.setColor(color); } graphics.fillRect(x, y, width, height); } /** * clearRect * * @param x The x-position of the rectangle * @param y The y-position of the rectangle * @param width The width of the rectangle * @param height The height of the rectangle */ public void clearRect(int x, int y, int width, int height) { if ((debugOptions & LOG_OPTION) != 0) { logStream().println(prefix() + " Clearing rect: " + new Rectangle(x, y, width, height)); } graphics.clearRect(x, y, width, height); } /** * drawRoundRect * * @param x The x-position of the rectangle * @param y The y-position of the rectangle * @param width The width of the rectangle * @param height The height of the rectangle * @param arcWidth TODO * @param arcHeight TODO */ public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) { if ((debugOptions & LOG_OPTION) != 0) { logStream().println(prefix() + " Drawing round rect: " + new Rectangle(x, y, width, height) + " arcWidth: " + arcWidth + " arcHeight: " + arcHeight);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -