⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swtgraphics2d.java

📁 JFreeChart它主要是用来制作各种各样的图表
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors. * * Project Info:  http://www.jfree.org/jfreechart/index.html * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library 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 Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, * USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * ------------------ * SWTGraphics2D.java * ------------------ * (C) Copyright 2006-2008, by Henry Proudhon and Contributors. * * Original Author:  Henry Proudhon (henry.proudhon AT ensmp.fr); * Contributor(s):   Cedric Chabanois (cchabanois AT no-log.org); *                   David Gilbert (for Object Refinery Limited); * * Changes * ------- * 14-Jun-2006 : New class (HP); * 29-Jan-2007 : Fixed the fillRect method (HP); * 31-Jan-2007 : Moved the dummy JPanel to SWTUtils.java, *               implemented the drawLine method (HP); * 07-Apr-2007 : Dispose some of the swt ressources, *               thanks to silent for pointing this out (HP); * 23-May-2007 : Removed resource leaks by adding a resource pool (CC); * 15-Jun-2007 : Fixed compile error for JDK 1.4 (DG); * 22-Oct-2007 : Implemented clipping (HP); * 22-Oct-2007 : Implemented some AlphaComposite support (HP); * 23-Oct-2007 : Added mechanism for storing RenderingHints (which are *               still ignored at this point) (DG); * 23-Oct-2007 : Implemented drawPolygon(), drawPolyline(), drawOval(), *               fillOval(), drawArc() and fillArc() (DG); * 27-Nov-2007 : Implemented a couple of drawImage() methods (DG); * */package org.jfree.experimental.swt;import java.awt.AlphaComposite;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Composite;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GraphicsConfiguration;import java.awt.Image;import java.awt.Paint;import java.awt.Rectangle;import java.awt.RenderingHints;import java.awt.Shape;import java.awt.Stroke;import java.awt.RenderingHints.Key;import java.awt.font.FontRenderContext;import java.awt.font.GlyphVector;import java.awt.geom.AffineTransform;import java.awt.geom.PathIterator;import java.awt.geom.Rectangle2D;import java.awt.image.BufferedImage;import java.awt.image.BufferedImageOp;import java.awt.image.ImageObserver;import java.awt.image.RenderedImage;import java.awt.image.renderable.RenderableImage;import java.text.AttributedCharacterIterator;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.eclipse.swt.SWT;import org.eclipse.swt.graphics.FontData;import org.eclipse.swt.graphics.GC;import org.eclipse.swt.graphics.ImageData;import org.eclipse.swt.graphics.Path;import org.eclipse.swt.graphics.Resource;import org.eclipse.swt.graphics.Transform;/** * This is a class utility to draw Graphics2D stuff on a swt composite. * It is presently developed to use JFreeChart with the Standard * Widget Toolkit but may be of a wider use later. */public class SWTGraphics2D extends Graphics2D {    /** The swt graphic composite */    private GC gc;    /**     * The rendering hints.  For now, these are not used, but at least the     * basic mechanism is present.     */    private RenderingHints hints;    /** A reference to the compositing rule to apply. This is necessary     * due to the poor compositing interface of the SWT toolkit. */    private java.awt.Composite composite;    /** A HashMap to store the Swt color resources. */    private Map colorsPool = new HashMap();    /** A HashMap to store the Swt font resources. */    private Map fontsPool = new HashMap();    /** A HashMap to store the Swt transform resources. */    private Map transformsPool = new HashMap();    /** A List to store the Swt resources. */    private List resourcePool = new ArrayList();    /**     * Creates a new instance.     *     * @param gc  the graphics context.     */    public SWTGraphics2D(GC gc) {        super();        this.gc = gc;        this.hints = new RenderingHints(null);        this.composite = AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f);    }    /**     * Not implemented yet - see {@link Graphics#create()}.     *     * @return <code>null</code>.     */    public Graphics create() {        // TODO Auto-generated method stub        return null;    }    /**     * Not implemented yet - see {@link Graphics2D#getDeviceConfiguration()}.     *     * @return <code>null</code>.     */    public GraphicsConfiguration getDeviceConfiguration() {        // TODO Auto-generated method stub        return null;    }    /**     * Returns the current value for the specified hint key, or     * <code>null</code> if no value is set.     *     * @param hintKey  the hint key (<code>null</code> permitted).     *     * @return The hint value, or <code>null</code>.     *     * @see #setRenderingHint(RenderingHints.Key, Object)     */    public Object getRenderingHint(Key hintKey) {        return this.hints.get(hintKey);    }    /**     * Sets the value for a rendering hint.  For now, this graphics context     * ignores all hints.     *     * @param hintKey  the key (<code>null</code> not permitted).     * @param hintValue  the value (must be compatible with the specified key).     *     * @throws IllegalArgumentException if <code>hintValue</code> is not     *         compatible with the <code>hintKey</code>.     *     * @see #getRenderingHint(RenderingHints.Key)     */    public void setRenderingHint(Key hintKey, Object hintValue) {        this.hints.put(hintKey, hintValue);    }    /**     * Returns a copy of the hints collection for this graphics context.     *     * @return A copy of the hints collection.     */    public RenderingHints getRenderingHints() {        return (RenderingHints) this.hints.clone();    }    /**     * Adds the hints in the specified map to the graphics context, replacing     * any existing hints.  For now, this graphics context ignores all hints.     *     * @param hints  the hints (<code>null</code> not permitted).     *     * @see #setRenderingHints(Map)     */    public void addRenderingHints(Map hints) {        this.hints.putAll(hints);    }    /**     * Replaces the existing hints with those contained in the specified     * map.  Note that, for now, this graphics context ignores all hints.     *     * @param hints  the hints (<code>null</code> not permitted).     *     * @see #addRenderingHints(Map)     */    public void setRenderingHints(Map hints) {        if (hints == null) {            throw new NullPointerException("Null 'hints' argument.");        }        this.hints = new RenderingHints(hints);    }    /**     * Returns the current paint for this graphics context.     *     * @return The current paint.     *     * @see #setPaint(Paint)     */    public Paint getPaint() {        // TODO: it might be a good idea to keep a reference to the color        // specified in setPaint() or setColor(), rather than creating a        // new object every time getPaint() is called.        return SWTUtils.toAwtColor(this.gc.getForeground());    }    /**     * Sets the paint for this graphics context.  For now, this graphics     * context only supports instances of {@link Color}.     *     * @param paint  the paint (<code>null</code> not permitted).     *     * @see #getPaint()     * @see #setColor(Color)     */    public void setPaint(Paint paint) {        if (paint instanceof Color) {            setColor((Color) paint);        }        else {            throw new RuntimeException("Can only handle 'Color' at present.");        }    }    /**     * Returns the current color for this graphics context.     *     * @return The current color.     *     * @see #setColor(Color)     */    public Color getColor() {        // TODO: it might be a good idea to keep a reference to the color        // specified in setPaint() or setColor(), rather than creating a        // new object every time getPaint() is called.        return SWTUtils.toAwtColor(this.gc.getForeground());    }    /**     * Sets the current color for this graphics context.     *     * @param color  the color.     *     * @see #getColor()     */    public void setColor(Color color) {        org.eclipse.swt.graphics.Color swtColor = getSwtColorFromPool(color);        this.gc.setForeground(swtColor);        // handle transparency and compositing.        if (this.composite instanceof AlphaComposite) {            AlphaComposite acomp = (AlphaComposite) this.composite;            switch (acomp.getRule()) {            case AlphaComposite.SRC_OVER:                this.gc.setAlpha((int) (color.getAlpha() * acomp.getAlpha()));                break;            default:                this.gc.setAlpha(color.getAlpha());                break;            }        }    }    /**     * Sets the background colour.     *     * @param color  the colour.     */    public void setBackground(Color color) {        org.eclipse.swt.graphics.Color swtColor = getSwtColorFromPool(color);        this.gc.setBackground(swtColor);    }    /**     * Returns the background colour.     *     * @return The background colour.     */    public Color getBackground() {        return SWTUtils.toAwtColor(this.gc.getBackground());    }    /**     * Not implemented - see {@link Graphics#setPaintMode()}.     */    public void setPaintMode() {        // TODO Auto-generated method stub    }    /**     * Not implemented - see {@link Graphics#setXORMode(Color)}.     *     * @param color  the colour.     */    public void setXORMode(Color color) {        // TODO Auto-generated method stub    }    /**     * Returns the current composite.     *     * @return The current composite.     *     * @see #setComposite(Composite)     */    public Composite getComposite() {        return this.composite;    }    /**     * Sets the current composite.  This implementation currently supports     * only the {@link AlphaComposite} class.     *     * @param comp  the composite.     */    public void setComposite(Composite comp) {        this.composite = comp;        if (comp instanceof AlphaComposite) {            AlphaComposite acomp = (AlphaComposite) comp;            int alpha = (int) (acomp.getAlpha() * 0xFF);            this.gc.setAlpha(alpha);        }        else {            System.out.println("warning, can only handle alpha composite at the moment.");        }    }    /**     * Returns the current stroke for this graphics context.     *     * @return The current stroke.     *     * @see #setStroke(Stroke)     */    public Stroke getStroke() {        return new BasicStroke(this.gc.getLineWidth(), this.gc.getLineCap(),                this.gc.getLineJoin());    }    /**     * Sets the stroke for this graphics context.  For now, this implementation     * only recognises the {@link BasicStroke} class.     *     * @param stroke  the stroke (<code>null</code> not permitted).     *     * @see #getStroke()     */    public void setStroke(Stroke stroke) {        if (stroke instanceof BasicStroke) {            BasicStroke bs = (BasicStroke) stroke;            // linewidth            this.gc.setLineWidth((int) bs.getLineWidth());            // line join            switch (bs.getLineJoin()) {                case BasicStroke.JOIN_BEVEL :                    this.gc.setLineJoin(SWT.JOIN_BEVEL);                    break;                case BasicStroke.JOIN_MITER :                    this.gc.setLineJoin(SWT.JOIN_MITER);                    break;                case BasicStroke.JOIN_ROUND :                    this.gc.setLineJoin(SWT.JOIN_ROUND);                    break;            }            // line cap            switch (bs.getEndCap()) {                case BasicStroke.CAP_BUTT :                    this.gc.setLineCap(SWT.CAP_FLAT);                    break;                case BasicStroke.CAP_ROUND :                    this.gc.setLineCap(SWT.CAP_ROUND);                    break;                case BasicStroke.CAP_SQUARE :                    this.gc.setLineCap(SWT.CAP_SQUARE);                    break;            }            // set the line style to solid by default            this.gc.setLineStyle(SWT.LINE_SOLID);            // apply dash style if any            float[] dashes = bs.getDashArray();            if (dashes != null) {                int[] swtDashes = new int[dashes.length];                for (int i = 0; i < swtDashes.length; i++) {                    swtDashes[i] = (int) dashes[i];                }                this.gc.setLineDash(swtDashes);            }        }        else {            throw new RuntimeException(                    "Can only handle 'Basic Stroke' at present.");        }    }    /**     * Applies the specified clip.     *     * @param s  the shape for the clip.     */    public void clip(Shape s) {        Path path = toSwtPath(s);        this.gc.setClipping(path);        path.dispose();    }    /**     * Returns the clip bounds.     *     * @return The clip bounds.     */    public Rectangle getClipBounds() {        org.eclipse.swt.graphics.Rectangle clip = this.gc.getClipping();        return new Rectangle(clip.x, clip.y, clip.width, clip.height);    }    /**     * Sets the clipping to the intersection of the current clip region and     * the specified rectangle.     *     * @param x  the x-coordinate.     * @param y  the y-coordinate.     * @param width  the width.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -