1468 lines · 48.9 KB · Java
RawDownload
/* * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.swing; import java.awt.*;import java.awt.image.*;import java.text.AttributedCharacterIterator; /** * Graphics subclass supporting graphics debugging. Overrides most methods * from Graphics.  DebugGraphics objects are rarely created by hand.  They * are most frequently created automatically when a JComponent's * debugGraphicsOptions are changed using the setDebugGraphicsOptions() * method. * <p> * NOTE: You must turn off double buffering to use DebugGraphics: *       RepaintManager repaintManager = RepaintManager.currentManager(component); *       repaintManager.setDoubleBufferingEnabled(false); * * @see JComponent#setDebugGraphicsOptions * @see RepaintManager#currentManager * @see RepaintManager#setDoubleBufferingEnabled * * @author Dave Karlton */public class DebugGraphics extends Graphics {    Graphics                    graphics;    Image                       buffer;    int                         debugOptions;    int                         graphicsID = graphicsCount++;    int                         xOffset, yOffset;    private static int          graphicsCount = 0;    private static ImageIcon    imageLoadingIcon = new ImageIcon();     /** Log graphics operations. */    public static final int     LOG_OPTION   = 1 << 0;    /** Flash graphics operations. */    public static final int     FLASH_OPTION = 1 << 1;    /** Show buffered operations in a separate <code>Frame</code>. */    public static final int     BUFFERED_OPTION = 1 << 2;    /** Don't debug graphics operations. */    public static final int     NONE_OPTION = -1;     static {        JComponent.DEBUG_GRAPHICS_LOADED = true;    }     /**     * Constructs a new debug graphics context that supports slowed     * down drawing.     */    public DebugGraphics() {        super();        buffer = null;        xOffset = yOffset = 0;    }     /**     * Constructs a debug graphics context from an existing graphics     * context that slows down drawing for the specified component.     *     * @param graphics  the Graphics context to slow down     * @param component the JComponent to draw slowly     */    public DebugGraphics(Graphics graphics, JComponent component) {        this(graphics);        setDebugOptions(component.shouldDebugGraphics());    }     /**     * Constructs a debug graphics context from an existing graphics     * context that supports slowed down drawing.     *     * @param graphics  the Graphics context to slow down     */    public DebugGraphics(Graphics graphics) {        this();        this.graphics = graphics;    }     /**     * Overrides <code>Graphics.create</code> to return a DebugGraphics object.     */    public Graphics create() {        DebugGraphics debugGraphics;         debugGraphics = new DebugGraphics();        debugGraphics.graphics = graphics.create();        debugGraphics.debugOptions = debugOptions;        debugGraphics.buffer = buffer;         return debugGraphics;    }     /**     * Overrides <code>Graphics.create</code> to return a DebugGraphics object.     */    public Graphics create(int x, int y, int width, int height) {        DebugGraphics debugGraphics;         debugGraphics = new DebugGraphics();        debugGraphics.graphics = graphics.create(x, y, width, height);        debugGraphics.debugOptions = debugOptions;        debugGraphics.buffer = buffer;        debugGraphics.xOffset = xOffset + x;        debugGraphics.yOffset = yOffset + y;         return debugGraphics;    }      //------------------------------------------------    //  NEW METHODS    //------------------------------------------------     /**     * Sets the Color used to flash drawing operations.     */    public static void setFlashColor(Color flashColor) {        info().flashColor = flashColor;    }     /**     * Returns the Color used to flash drawing operations.     * @see #setFlashColor     */    public static Color flashColor() {        return info().flashColor;    }     /**     * Sets the time delay of drawing operation flashing.     */    public static void setFlashTime(int flashTime) {        info().flashTime = flashTime;    }     /**     * Returns the time delay of drawing operation flashing.     * @see #setFlashTime     */    public static int flashTime() {        return info().flashTime;    }     /**     * Sets the number of times that drawing operations will flash.     */    public static void setFlashCount(int flashCount) {        info().flashCount = flashCount;    }     /** Returns the number of times that drawing operations will flash.      * @see #setFlashCount      */    public static int flashCount() {        return info().flashCount;    }     /** Sets the stream to which the DebugGraphics logs drawing operations.      */    public static void setLogStream(java.io.PrintStream stream) {        info().stream = stream;    }     /** Returns the stream to which the DebugGraphics logs drawing operations.      * @see #setLogStream      */    public static java.io.PrintStream logStream() {        return info().stream;    }     /** Sets the Font used for text drawing operations.      */    public void setFont(Font aFont) {        if (debugLog()) {            info().log(toShortString() + " Setting font: " + aFont);        }        graphics.setFont(aFont);    }     /** Returns the Font used for text drawing operations.      * @see #setFont      */    public Font getFont() {        return graphics.getFont();    }     /** Sets the color to be used for drawing and filling lines and shapes.      */    public void setColor(Color aColor) {        if (debugLog()) {            info().log(toShortString() + " Setting color: " + aColor);        }        graphics.setColor(aColor);    }     /** Returns the Color used for text drawing operations.      * @see #setColor      */    public Color getColor() {        return graphics.getColor();    }      //-----------------------------------------------    // OVERRIDDEN METHODS    //------------------------------------------------     /**     * Overrides <code>Graphics.getFontMetrics</code>.     */    public FontMetrics getFontMetrics() {        return graphics.getFontMetrics();    }     /**     * Overrides <code>Graphics.getFontMetrics</code>.     */    public FontMetrics getFontMetrics(Font f) {        return graphics.getFontMetrics(f);    }     /**     * Overrides <code>Graphics.translate</code>.     */    public void translate(int x, int y) {        if (debugLog()) {            info().log(toShortString() +                " Translating by: " + new Point(x, y));        }        xOffset += x;        yOffset += y;        graphics.translate(x, y);    }     /**     * Overrides <code>Graphics.setPaintMode</code>.     */    public void setPaintMode() {        if (debugLog()) {            info().log(toShortString() + " Setting paint mode");        }        graphics.setPaintMode();    }     /**     * Overrides <code>Graphics.setXORMode</code>.     */    public void setXORMode(Color aColor) {        if (debugLog()) {            info().log(toShortString() + " Setting XOR mode: " + aColor);        }        graphics.setXORMode(aColor);    }     /**     * Overrides <code>Graphics.getClipBounds</code>.     */    public Rectangle getClipBounds() {        return graphics.getClipBounds();    }     /**     * Overrides <code>Graphics.clipRect</code>.     */    public void clipRect(int x, int y, int width, int height) {        graphics.clipRect(x, y, width, height);        if (debugLog()) {            info().log(toShortString() +                " Setting clipRect: " + (new Rectangle(x, y, width, height)) +                " New clipRect: " + graphics.getClip());        }    }     /**     * Overrides <code>Graphics.setClip</code>.     */    public void setClip(int x, int y, int width, int height) {        graphics.setClip(x, y, width, height);        if (debugLog()) {            info().log(toShortString() +                        " Setting new clipRect: " + graphics.getClip());        }    }     /**     * Overrides <code>Graphics.getClip</code>.     */    public Shape getClip() {        return graphics.getClip();    }     /**     * Overrides <code>Graphics.setClip</code>.     */    public void setClip(Shape clip) {        graphics.setClip(clip);        if (debugLog()) {            info().log(toShortString() +                       " Setting new clipRect: " +  graphics.getClip());        }    }     /**     * Overrides <code>Graphics.drawRect</code>.     */    public void drawRect(int x, int y, int width, int height) {        DebugGraphicsInfo info = info();         if (debugLog()) {            info().log(toShortString() +                      " Drawing rect: " +                      new Rectangle(x, y, width, height));        }         if (isDrawingBuffer()) {            if (debugBuffered()) {                Graphics debugGraphics = debugGraphics();                 debugGraphics.drawRect(x, y, width, height);                debugGraphics.dispose();            }        } else if (debugFlash()) {            Color oldColor = getColor();            int i, count = (info.flashCount * 2) - 1;             for (i = 0; i < count; i++) {                graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);                graphics.drawRect(x, y, width, height);                Toolkit.getDefaultToolkit().sync();                sleep(info.flashTime);            }            graphics.setColor(oldColor);        }        graphics.drawRect(x, y, width, height);    }     /**     * Overrides <code>Graphics.fillRect</code>.     */    public void fillRect(int x, int y, int width, int height) {        DebugGraphicsInfo info = info();         if (debugLog()) {            info().log(toShortString() +                      " Filling rect: " +                      new Rectangle(x, y, width, height));        }         if (isDrawingBuffer()) {            if (debugBuffered()) {                Graphics debugGraphics = debugGraphics();                 debugGraphics.fillRect(x, y, width, height);                debugGraphics.dispose();            }        } else if (debugFlash()) {            Color oldColor = getColor();            int i, count = (info.flashCount * 2) - 1;             for (i = 0; i < count; i++) {                graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);                graphics.fillRect(x, y, width, height);                Toolkit.getDefaultToolkit().sync();                sleep(info.flashTime);            }            graphics.setColor(oldColor);        }        graphics.fillRect(x, y, width, height);    }     /**     * Overrides <code>Graphics.clearRect</code>.     */    public void clearRect(int x, int y, int width, int height) {        DebugGraphicsInfo info = info();         if (debugLog()) {            info().log(toShortString() +                      " Clearing rect: " +                      new Rectangle(x, y, width, height));        }         if (isDrawingBuffer()) {            if (debugBuffered()) {                Graphics debugGraphics = debugGraphics();                 debugGraphics.clearRect(x, y, width, height);                debugGraphics.dispose();            }        } else if (debugFlash()) {            Color oldColor = getColor();            int i, count = (info.flashCount * 2) - 1;             for (i = 0; i < count; i++) {                graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);                graphics.clearRect(x, y, width, height);                Toolkit.getDefaultToolkit().sync();                sleep(info.flashTime);            }            graphics.setColor(oldColor);        }        graphics.clearRect(x, y, width, height);    }     /**     * Overrides <code>Graphics.drawRoundRect</code>.     */    public void drawRoundRect(int x, int y, int width, int height,                              int arcWidth, int arcHeight) {        DebugGraphicsInfo info = info();         if (debugLog()) {            info().log(toShortString() +                      " Drawing round rect: " +                      new Rectangle(x, y, width, height) +                      " arcWidth: " + arcWidth +                      " archHeight: " + arcHeight);        }        if (isDrawingBuffer()) {            if (debugBuffered()) {                Graphics debugGraphics = debugGraphics();                 debugGraphics.drawRoundRect(x, y, width, height,                                            arcWidth, arcHeight);                debugGraphics.dispose();            }        } else if (debugFlash()) {            Color oldColor = getColor();            int i, count = (info.flashCount * 2) - 1;             for (i = 0; i < count; i++) {                graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);                graphics.drawRoundRect(x, y, width, height,                                       arcWidth, arcHeight);                Toolkit.getDefaultToolkit().sync();                sleep(info.flashTime);            }            graphics.setColor(oldColor);        }        graphics.drawRoundRect(x, y, width, height, arcWidth, arcHeight);    }     /**     * Overrides <code>Graphics.fillRoundRect</code>.     */    public void fillRoundRect(int x, int y, int width, int height,                              int arcWidth, int arcHeight) {        DebugGraphicsInfo info = info();         if (debugLog()) {            info().log(toShortString() +                      " Filling round rect: " +                      new Rectangle(x, y, width, height) +                      " arcWidth: " + arcWidth +                      " archHeight: " + arcHeight);        }        if (isDrawingBuffer()) {            if (debugBuffered()) {                Graphics debugGraphics = debugGraphics();                 debugGraphics.fillRoundRect(x, y, width, height,                                            arcWidth, arcHeight);                debugGraphics.dispose();            }        } else if (debugFlash()) {            Color oldColor = getColor();            int i, count = (info.flashCount * 2) - 1;             for (i = 0; i < count; i++) {                graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);                graphics.fillRoundRect(x, y, width, height,                                       arcWidth, arcHeight);                Toolkit.getDefaultToolkit().sync();                sleep(info.flashTime);            }

Lines 1–500 of 1468.