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

📄 swtgraphics2d.java

📁 java图形利器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2007, 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, 2007, by Henry Proudhon and Contributors. * * Original Author:  Henry Proudhon (henry.proudhon AT insa-lyon.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); *  */package org.jfree.experimental.swt;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.image.BufferedImage;import java.awt.image.BufferedImageOp;import java.awt.image.DirectColorModel;import java.awt.image.ImageObserver;import java.awt.image.IndexColorModel;import java.awt.image.RenderedImage;import java.awt.image.WritableRaster;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.PaletteData;import org.eclipse.swt.graphics.Path;import org.eclipse.swt.graphics.RGB;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;        /** 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 color resources. */    private List resourcePool = new ArrayList();    /**     * Creates a new instance.     *      * @param gc  the graphics context.     */    public SWTGraphics2D(GC gc) {        super();        this.gc = gc;    }    /**     * Add given swt resource to the resource pool. All resources added     * to the resource pool will be dispose when {@link #dispose()} is called     *       * @param resource the resource to add to the pool.     * @return the swt <code>Resource</code> just added.     */    private Resource addToResourcePool(Resource resource) {    	resourcePool.add(resource);    	return resource;    }        /**     * Dispose the resource pool.     */    private void disposeResourcePool() {    	for (Iterator it = resourcePool.iterator();it.hasNext();) {    		Resource resource = (Resource)it.next();    		resource.dispose();    	}    	resourcePool.clear();    	colorsPool.clear();    	resourcePool.clear();    }    /**     * Internal method to convert a AWT font object into      * a SWT font resource. If a corresponding SWT font     * instance is already in the pool, it will be used      * instead of creating a new one. This is used in      * {@link #setFont()} for instance.      *      * @param font The AWT font to convert.     * @return The SWT font instance.     */    private org.eclipse.swt.graphics.Font getSwtFontFromPool(Font font) {    	org.eclipse.swt.graphics.Font swtFont = (org.eclipse.swt.graphics.Font)    	fontsPool.get(font);    	if (swtFont == null) {    	    swtFont = new org.eclipse.swt.graphics.Font(     		    gc.getDevice(),     		    SWTUtils.toSwtFontData(gc.getDevice(), font, true));    	    addToResourcePool(swtFont);    	    fontsPool.put(font, swtFont);    	}    	return swtFont;    }        /**     * Internal method to convert a AWT color object into      * a SWT color resource. If a corresponding SWT color     * instance is already in the pool, it will be used      * instead of creating a new one. This is used in      * {@link #setColor()} for instance.      *      * @param awtColor The AWT color to convert.     * @return A SWT color instance.     */    private org.eclipse.swt.graphics.Color getSwtColorFromPool(Color awtColor) {	org.eclipse.swt.graphics.Color swtColor = (org.eclipse.swt.graphics.Color)        // we can't use the following valueOf() method, because it won't         // compile with JDK1.4	//this.colorsPool.get(Integer.valueOf(awtColor.getRGB()));	this.colorsPool.get(new Integer(awtColor.getRGB()));        if (swtColor == null) {	    swtColor = SWTUtils.toSwtColor(gc.getDevice(), awtColor);	    addToResourcePool(swtColor);	    // see comment above            //this.colorsPool.put(Integer.valueOf(awtColor.getRGB()), swtColor);            this.colorsPool.put(new Integer(awtColor.getRGB()), swtColor);	}	return swtColor;    }    /**     * Perform a switch between foreground and background      * color of gc. This is needed for consistency with      * the awt behaviour, and is required notably for the      * filling methods.     */    private void switchColors() {        org.eclipse.swt.graphics.Color bg = gc.getBackground();        org.eclipse.swt.graphics.Color fg = gc.getForeground();        gc.setBackground(fg);        gc.setForeground(bg);    }        /**     * Converts an AWT <code>Shape</code> into a SWT <code>Path</code>.     *      * @param shape  the shape.     *      * @return The path.     */    private Path toSwtPath(Shape shape) {        int type;        float[] coords = new float[6];        Path path = new Path(this.gc.getDevice());        PathIterator pit = shape.getPathIterator(null);        while (!pit.isDone()) {            type = pit.currentSegment(coords);            switch (type) {                case (PathIterator.SEG_MOVETO):                    path.moveTo(coords[0], coords[1]);                    break;                case (PathIterator.SEG_LINETO):                    path.lineTo(coords[0], coords[1]);                    break;                case (PathIterator.SEG_QUADTO):                    path.quadTo(coords[0], coords[1], coords[2], coords[3]);                    break;                case (PathIterator.SEG_CUBICTO):                    path.cubicTo(coords[0], coords[1], coords[2],                             coords[3], coords[4], coords[5]);                    break;                case (PathIterator.SEG_CLOSE):                    path.close();                    break;                default:                    break;            }            pit.next();        }        return path;    }        /**     * Converts an AWT transform into the equivalent SWT transform.     *      * @param awtTransform  the AWT transform.     *      * @return The SWT transform.     */    private Transform toSwtTransform(AffineTransform awtTransform) {        Transform t = new Transform(gc.getDevice());        double[] matrix = new double[6];        awtTransform.getMatrix(matrix);        t.setElements((float) matrix[0], (float) matrix[1],                (float) matrix[2], (float) matrix[3],                (float) matrix[4], (float) matrix[5]);         return t;    }        /**     * Converts an SWT transform into the equivalent AWT transform.     *      * @param swtTransform  the SWT transform.     *      * @return The AWT transform.     */    private AffineTransform toAwtTransform(Transform swtTransform) {        float[] elements = new float[6];        swtTransform.getElements(elements);        AffineTransform awtTransform = new AffineTransform(elements);        return awtTransform;    }        /* (non-Javadoc)     * @see java.awt.Graphics2D#draw(java.awt.Shape)     */    public void draw(Shape shape) {        Path path = toSwtPath(shape);        gc.drawPath(path);        path.dispose();    }    /* (non-Javadoc)     * @see java.awt.Graphics2D#drawImage(java.awt.Image,      * java.awt.geom.AffineTransform, java.awt.image.ImageObserver)     */    public boolean drawImage(Image image, AffineTransform xform,            ImageObserver obs) {        // TODO Auto-generated method stub        return false;    }    /* (non-Javadoc)     * @see java.awt.Graphics2D#drawImage(java.awt.image.BufferedImage,      * java.awt.image.BufferedImageOp, int, int)     */    public void drawImage(BufferedImage image, BufferedImageOp op, int x,             int y) {        org.eclipse.swt.graphics.Image im             = new org.eclipse.swt.graphics.Image(gc.getDevice(),                     convertToSWT(image));        gc.drawImage(im, x, y);        im.dispose();    }    /**     * Draws an image at (x, y).     *      * @param image  the image.     * @param x  the x-coordinate.     * @param y  the y-coordinate.     */    public void drawImage(org.eclipse.swt.graphics.Image image, int x, int y) {        gc.drawImage(image, x, y);    }    /* (non-Javadoc)     * @see java.awt.Graphics2D#drawRenderedImage(java.awt.image.RenderedImage,     * java.awt.geom.AffineTransform)     */    public void drawRenderedImage(RenderedImage image, AffineTransform xform) {        // TODO Auto-generated method stub    }    /* (non-Javadoc)     * @see java.awt.Graphics2D#drawRenderableImage(     * java.awt.image.renderable.RenderableImage, java.awt.geom.AffineTransform)     */    public void drawRenderableImage(RenderableImage image,             AffineTransform xform) {        // TODO Auto-generated method stub    }    /**     * Draws a string on the receiver. note that      * to be consistent with the awt method, 

⌨️ 快捷键说明

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