qtgraphics.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 856 行 · 第 1/2 页

JAVA
856
字号
/* * @(#)QtGraphics.java	1.9 06/10/10 *  * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program 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.  *  * This program 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 at /legal/license.txt).  *  * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions.  */package java.awt;import java.io.*;import java.util.*;import java.awt.image.BufferedImage;import java.awt.image.ImageObserver;import sun.awt.ConstrainableGraphics;import java.text.AttributedCharacterIterator;import java.awt.font.TextAttribute;/** * QtGraphics is an object that encapsulates a graphics context for drawing with Qt. * * @version 1.12, 11/27/01 */final class QtGraphics extends Graphics2D implements ConstrainableGraphics {	/** The default font to use when no font has been set or null is used. */	private static final Font DEFAULT_FONT = new Font("Dialog", Font.PLAIN, 12);	private AlphaComposite composite;	/** The current foreground color. */	private Color   foreground;	/** The current background color. This is used by clearRect. */	private Color   background;	/** The current xor color. If null then we are in paint mode. */	private Color   xorColor;	/** The currently selected font for text operations. */	private Font    font;	/** The font metrics for the font. Cached for performance. */	private QtFontMetrics fontMetrics;	/** Translated X offset from native offset. */	private int     originX;	/** Translated Y offset from native offset. */	private int     originY;	/** The index of the native peer for this graphics object. */	int             psd;	/** The current user clip rectangle or null if no clip has been set. This is stored in the	 native coordinate system and not the possibly translated Java coordinate system. */	private Rectangle      clip;	private Stroke		stroke;	private int		lineWidth;//	private int	clipWidth, clipHeight;	/** The rectangle this graphics object has been constrained too. This is stored in the	native coordinate system and not the (possibly) translated Java coordinate system.	 If it is null then this graphics has not been constrained. The constrained rectangle	 is another layer of clipping independant of the user clip. The actaul clip used is the	 intersection */	private Rectangle constrainedRect;	/** The GraphicsConfiguration that this graphics uses. */	private GraphicsConfiguration graphicsConfiguration;	static native int pCloneGraphPSD(int Gpsd, boolean fresh);	static native int pCloneImagePSD(int Ipsd, boolean fresh);	static native void pDisposePSD(int psd);	private static native void pSetColor(int psd, int argb);	private static native void pSetFont(int psd, int font);	private static native void pSetStroke(int psd, int width, int cap, int join, int style);	private static native void pSetComposite(int psd, int rule, int extraAlpha);	private static native void pSetPaintMode(int psd);	private static native void pSetXORMode(int psd, int rgb);	private static native void pChangeClipRect(int psd, int x, int y, int w, int h);	private static native void pRemoveClip(int psd);	private static native void pDrawString(int psd, String string, int x, int y);	private static native void pFillRect(int psd, int x, int y, int width, int height);	private static native void pClearRect(int psd, int x, int y, int width, int height, int rgb);	private static native void pDrawRect(int psd, int x, int y, int width, int height);	private static native void pCopyArea(int psd, int x, int y, int width, int height, int dx, int dy);	private static native void pDrawLine(int psd, int x1, int y1, int x2, int y2);	private static native void pDrawPolygon(int psd, int orinX, int originY, int xPoints[], int yPoints[], int nPoints, boolean close);	private static native void pFillPolygon(int psd, int orinX, int originY, int xPoints[], int yPoints[], int nPoints);	private static native void pDrawArc(int psd, int x, int y, int width, int height, int start, int end);	private static native void pFillArc(int psd, int x, int y, int width, int height, int start, int end);	private static native void pDrawOval(int psd, int x, int y, int width, int height);	private static native void pFillOval(int psd, int x, int y, int width, int height);	private static native void pDrawRoundRect(int psd, int x, int y, int width, int height, int arcWidth, int arcHeight);	private static native void pFillRoundRect(int psd, int x, int y, int width, int height, int arcWidth, int arcHeight);	private static native QtImage getBufferedImagePeer(BufferedImage bi);	QtGraphics(Window window) {		try{			psd = pCloneGraphPSD(((QtGraphicsDevice)                 window.getGraphicsConfiguration().getDevice()).psd, false);		} catch(OutOfMemoryError e)		{			System.gc();			psd = pCloneGraphPSD(((QtGraphicsDevice) window.getGraphicsConfiguration().getDevice()).psd, true);		}		foreground = window.foreground;		if (foreground == null)			foreground = Color.black;//		clipWidth = window.getWidth()-1;//		clipHeight = window.getHeight()-1;//		clip = null;//		setupClip();		background = window.background;		if (background == null)			background = Color.black;		composite = AlphaComposite.SrcOver;		graphicsConfiguration = window.getGraphicsConfiguration();		setFont(DEFAULT_FONT);		setColorNative(); // 6241180	}	QtGraphics(QtGraphics g) {		try{			psd = pCloneGraphPSD(g.psd, false);		} catch(OutOfMemoryError e)		{			System.gc();			psd = pCloneGraphPSD(g.psd, true);		}		composite = g.composite;		graphicsConfiguration = g.graphicsConfiguration;		originX = g.originX;		originY = g.originY;//		clipWidth = g.clipWidth;//		clipHeight = g.clipHeight;//		clip = g.clip;        if(g.clip!=null) {                                clip = new Rectangle(g.clip);            setupClip();        }//		constrainedRect = g.constrainedRect;		stroke = g.stroke;//		setupClip();		foreground = g.foreground;		background = g.background;		setColorNative(); // 6241180		setFont(g.font);	}	QtGraphics(QtOffscreenImage image) {		try{			psd = pCloneImagePSD(image.psd, false); 		} catch(OutOfMemoryError e)		{			System.gc();			psd = pCloneImagePSD(image.psd, true);		}		foreground = image.foreground;		if (foreground == null)			foreground = Color.black;		background = image.background;		if (background == null)			background = Color.black;		composite = AlphaComposite.SrcOver;		graphicsConfiguration = image.gc;        // 5104620        // If the image.font is null, use the default font. Fixes the         // crash in the native code		setFont(((image.font == null) ? DEFAULT_FONT : image.font));        // 6205694,6205693,6206487,6206315        // Changed the width and height to the image's width and         // height. Got sidetracked by the "clipWidth" and "clipHeight"		pChangeClipRect(psd, 0,0,image.getWidth(),image.getHeight());        // 6205694,6205693,6206487,6206315,	}	QtGraphics(QtSubimage image, int x, int y) {		try{			psd = pCloneImagePSD(image.psd, false);		} catch(OutOfMemoryError e)		{			System.gc();			psd = pCloneImagePSD(image.psd, true);		}		composite = AlphaComposite.SrcOver;		originX = x;		originY = y;//		clipWidth = image.getWidth() - 1 + originX;//		clipHeight = image.getHeight() - 1 + originY;		clip = null;        pChangeClipRect(psd, x, y,                         image.getWidth()-1 + x,                         image.getHeight()-1 + y);		}	/**	 * Create a new QtGraphics Object based on this one.	 */	public Graphics create() {		return new QtGraphics(this);	}	/**	 * Translate	 */	public void     translate(int x, int y) {		originX += x;		originY += y;	}	public void finalize() {		dispose();		try { super.finalize(); } catch(Throwable t) { };	}	public final void dispose() {		if (psd > 0)		{            pDisposePSD(psd);			psd = -1;		}	}	private void setFont(Font font, boolean backwardCompat)	{		this.font = font;		fontMetrics = QtFontMetrics.getFontMetrics(font, backwardCompat);	    pSetFont(psd, fontMetrics.nativeFont);	}		public void setFont(Font font) {		if (font != null && !font.equals(this.font))			setFont(font, true);				}	public Font     getFont() {		return font;	}	/**	 * Gets the font metrics of the current font.	 * @return    the font metrics of this graphics	 *                    context's current font.	 * @see       java.awt.Graphics#getFont	 * @see       java.awt.FontMetrics	 * @see       java.awt.Graphics#getFontMetrics(Font)	 * @since     JDK1.0	 */	public FontMetrics getFontMetrics() {		return fontMetrics;	}	/**	 * Gets font metrics for the given font.	 */	public FontMetrics getFontMetrics(Font font) {		return QtFontMetrics.getFontMetrics(font, true);	}	public void setStroke(Stroke stroke)	{		/* Don't know about this case !!! FIXME!!! */		if(stroke == null) {			this.stroke = null;			return;		}		if(!(stroke instanceof BasicStroke))            // 6189931: Throwing IAE as per the spec instead of            // UnsupportedOperationException			throw new IllegalArgumentException("Only BasicStroke Supported!");            // 6189931		this.stroke = stroke;		BasicStroke bs = (BasicStroke)stroke;		int cap, join;		lineWidth = (int)bs.getLineWidth();		cap = bs.getEndCap();		join = bs.getLineJoin();		pSetStroke(psd, lineWidth, cap, join, 0);	}	public Stroke getStroke() {		return stroke;	}	/**	 * Sets the foreground color.	 */	public void     setColor(Color c) {		if ((c != null) && (c != foreground)) {			foreground = c;            setColorNative(); // 6241180		}	}    // 6241180    // Uses this.foreground and this.composite to figure out if a new    // foreground color should be set for SRC rule and Extra Alpha 0.0    private void setColorNative() {         float[] rgbaSrc = foreground.getRGBComponents(null);         // compute the actual alpha of the source color using the         // composite's extraAlpha and source color alpha         float alphasource = composite.getAlpha() * rgbaSrc[3];         // if the colormodel does not have an alpha channel, and the         // source has zero intensity alpha, then the source color         // that should be copied as part of the SRC rule is forced         // to black (the color channels multiplied by the alpha)         Color fg = this.foreground;         if ( this.composite.getRule() == AlphaComposite.SRC &&              !this.graphicsConfiguration.getColorModel().hasAlpha() &&              alphasource == 0.0f) {             fg = new Color(0.0f, 0.0f, 0.0f, 1.0f);         }         pSetColor(psd, fg.value);    }    // 6241180	public Color    getColor() {		return foreground;	}	public Composite getComposite()	{		return composite;	}	public GraphicsConfiguration getDeviceConfiguration()	{		return graphicsConfiguration;	}	public void setComposite(Composite comp)	{        if ((comp != null) && (comp != composite)) {		    if (!(comp instanceof AlphaComposite))			    throw new IllegalArgumentException("Only AlphaComposite is supported");		    composite = (AlphaComposite)comp;			pSetComposite(psd, composite.rule,                           (int)(composite.extraAlpha*0xFF));            setColorNative(); // 6241180	    }	}	/**	 * Sets the paint mode to overwrite the destination with the

⌨️ 快捷键说明

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