imagegraphicsbaseimpl.java
来自「java 3d game jme 工程开发源代码」· Java 代码 · 共 777 行 · 第 1/2 页
JAVA
777 行
/* * Copyright (c) 2003-2009 jMonkeyEngine * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of 'jMonkeyEngine' nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package com.jmex.awt.swingui;import java.awt.image.BufferedImage;import java.awt.image.ImageObserver;import java.awt.image.BufferedImageOp;import java.awt.image.RenderedImage;import java.awt.image.renderable.RenderableImage;import java.awt.Rectangle;import java.awt.Graphics2D;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Shape;import java.awt.Composite;import java.awt.AlphaComposite;import java.awt.GraphicsConfiguration;import java.awt.Paint;import java.awt.Stroke;import java.awt.RenderingHints;import java.awt.Point;import java.awt.font.GlyphVector;import java.awt.font.FontRenderContext;import java.awt.geom.Rectangle2D;import java.awt.geom.AffineTransform;import java.text.AttributedCharacterIterator;import java.util.Map;import java.util.logging.Logger;import java.nio.ByteBuffer;import java.nio.IntBuffer;import com.jme.image.Image;import com.jme.util.geom.BufferUtils;import com.jme.math.FastMath;public abstract class ImageGraphicsBaseImpl extends ImageGraphics { protected static final Logger logger = Logger.getLogger(ImageGraphics.class.getName()); protected final BufferedImage awtImage; protected final Graphics2D delegate; protected final byte[] data; protected final Rectangle dirty; protected final Point translation = new Point(); protected ByteBuffer tmp_byteBuffer; protected final Color TRANSPARENT = new Color( 0, 0, 0, 0 ); protected final int paintedMipMapCount; protected int mipMapLevel = 0; protected ImageGraphicsBaseImpl mipMapChild; protected boolean glTexSubImage2DSupported = true; protected IntBuffer idBuff = BufferUtils.createIntBuffer(16); private Rectangle imageBounds; private Rectangle clip = new Rectangle(); private Rectangle tmp_dirty = new Rectangle(); protected float scaleX = 1; protected float scaleY = 1; protected ImageGraphicsBaseImpl( BufferedImage awtImage, byte[] data, Graphics2D delegate, com.jme.image.Image image, Rectangle dirty, int translationX, int translationY, float scaleX, float scaleY, int mipMapCount, ImageGraphicsBaseImpl mipMapChild, int mipMapLevel ) { super( image ); this.awtImage = awtImage; this.data = data; this.delegate = delegate; this.dirty = dirty; translation.x = translationX; translation.y = translationY; this.scaleX = scaleX; this.scaleY = scaleY; this.paintedMipMapCount = mipMapCount; this.mipMapChild = mipMapChild; this.mipMapLevel = mipMapLevel; } public ImageGraphicsBaseImpl( int width, int height, int paintedMipMapCount, int mipMapLevel, float scale ) { super( new com.jme.image.Image() ); if ( paintedMipMapCount > 1 && ( !FastMath.isPowerOfTwo( width ) || !FastMath.isPowerOfTwo( height ) ) ) { throw new IllegalArgumentException( "Size must be power of 2 if mipmaps should be generated" ); } awtImage = new BufferedImage( width, height, BufferedImage.TYPE_4BYTE_ABGR ); // Get a pointer to the image memory ByteBuffer scratch = BufferUtils.createByteBuffer(4 * width * height ); tmp_byteBuffer = BufferUtils.createByteBuffer(4 * width * height ); data = (byte[]) awtImage.getRaster().getDataElements( 0, 0, awtImage.getWidth(), awtImage.getHeight(), null ); scratch.clear(); scratch.put( data ); scratch.flip(); image.setFormat( Image.Format.RGBA8 ); image.setWidth( width ); image.setHeight( height ); image.setData( scratch ); delegate = (Graphics2D) awtImage.getGraphics(); dirty = new Rectangle( 0, 0, width, height ); this.mipMapLevel = mipMapLevel; scale( scale, scale ); this.paintedMipMapCount = paintedMipMapCount; setBackground( TRANSPARENT ); } /** * This method allows access to internal data of this class. Use for reading only. (Don't expect any * direct modifications on this image to take effect immediately.) * @return the BufferedImage used internally to draw at before updating the native image */ public BufferedImage getAwtImage() { return awtImage; } protected Rectangle getImageBounds() { if ( imageBounds == null ) { imageBounds = new Rectangle( 0, 0, getImage().getWidth(), getImage().getHeight() ); } return imageBounds; } public boolean isDirty() { return !dirty.isEmpty(); } protected void makeDirty( int x, int y, int width, int height ) { if ( width < 0 ) { x = x + width; width = -width; } if ( height < 0 ) { y = y + height; height = -height; } tmp_dirty.setBounds( x, y, width, height ); makeDirty( tmp_dirty ); } private void makeDirty( Rectangle rectangle ) { synchronized ( dirty ) { getClipBounds( clip ); //debug-:// final StackTraceElement[] stackTrace = new Exception().getStackTrace();// for ( int i=0; i < stackTrace.length; i++ )// {// final String methodName = stackTrace[i].getMethodName();// if ( !"makeDirty".equals( methodName ) )// {// logger.info( methodName +"["+ stackTrace[i].getLineNumber() + "]" );// break;// }// } Rectangle2D.intersect( clip, rectangle, rectangle ); if ( !rectangle.isEmpty() ) { rectangle.x *= scaleX; rectangle.y *= scaleY; rectangle.width *= scaleX; rectangle.height *= scaleY; rectangle.translate( translation.x, translation.y ); Rectangle2D.intersect( rectangle, getImageBounds(), rectangle ); if ( !rectangle.isEmpty() ) { if ( !dirty.isEmpty() ) { dirty.add( rectangle ); } else { dirty.setBounds( rectangle ); } } } } } /** * todo: don't be lazy - compute the actual area! */ private void makeDirty() { makeDirty( 0, 0, getImage().getWidth(), getImage().getHeight() ); } public Color getColor() { return delegate.getColor(); } public void setColor( Color c ) { if ( mipMapChild != null ) { mipMapChild.setColor( c ); } delegate.setColor( c ); } public void setPaintMode() { if ( mipMapChild != null ) { mipMapChild.setPaintMode(); } delegate.setPaintMode(); } public void setXORMode( Color c1 ) { if ( mipMapChild != null ) { mipMapChild.setXORMode( c1 ); } delegate.setXORMode( c1 ); } public Font getFont() { return delegate.getFont(); } public void setFont( Font font ) { if ( mipMapChild != null ) { mipMapChild.setFont( font ); } delegate.setFont( font ); } public FontMetrics getFontMetrics( Font f ) { return delegate.getFontMetrics( f ); } public Rectangle getClipBounds() { return delegate.getClipBounds(); } public void clipRect( int x, int y, int width, int height ) { if ( mipMapChild != null ) { mipMapChild.clipRect( x, y, width, height ); } delegate.clipRect( x, y, width, height ); } public void setClip( int x, int y, int width, int height ) { if ( mipMapChild != null ) { mipMapChild.setClip( x, y, width, height ); } delegate.setClip( x, y, width, height ); } public Shape getClip() { return delegate.getClip(); } public void setClip( Shape clip ) { if ( mipMapChild != null ) { mipMapChild.setClip( clip ); } delegate.setClip( clip ); } public void copyArea( int x, int y, int width, int height, int dx, int dy ) { if ( mipMapChild != null ) { mipMapChild.copyArea( x, y, width, height, dx, dy ); } synchronized ( dirty ) { makeDirty( x + dx, y + dy, width, height ); delegate.copyArea( x, y, width, height, dx, dy ); } } public void drawLine( int x1, int y1, int x2, int y2 ) { if ( mipMapChild != null ) { mipMapChild.drawLine( x1, y1, x2, y2 ); } synchronized ( dirty ) { makeDirty( x1, y1, x2 - x1, y2 - y1 ); delegate.drawLine( x1, y1, x2, y2 ); } } public void fillRect( int x, int y, int width, int height ) { if ( mipMapChild != null ) { mipMapChild.fillRect( x, y, width, height ); } synchronized ( dirty ) { makeDirty( x, y, width, height ); delegate.fillRect( x, y, width, height ); } } public void clearRect( int x, int y, int width, int height ) { if ( mipMapChild != null ) { mipMapChild.clearRect( x, y, width, height ); } synchronized ( dirty ) { makeDirty( x, y, width, height ); //works in JDK1.5:// delegate.clearRect( x, y, width, height ); //fix for bug in JDK1.4: Color color = delegate.getColor(); delegate.setColor( TRANSPARENT ); Composite composite = delegate.getComposite(); delegate.setComposite( AlphaComposite.Clear ); delegate.fillRect( x, y, width, height ); delegate.setComposite( composite ); delegate.setColor( color ); } } public void drawRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight ) { if ( mipMapChild != null ) { mipMapChild.drawRoundRect( x, y, width, height, arcWidth, arcHeight ); } synchronized ( dirty ) { makeDirty( x, y, width, height ); delegate.drawRoundRect( x, y, width, height, arcWidth, arcHeight ); } } public void fillRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight ) { if ( mipMapChild != null ) { mipMapChild.fillRoundRect( x, y, width, height, arcWidth, arcHeight ); } synchronized ( dirty ) { makeDirty( x, y, width, height ); delegate.fillRoundRect( x, y, width, height, arcWidth, arcHeight ); } } public void drawOval( int x, int y, int width, int height ) { if ( mipMapChild != null ) { mipMapChild.drawOval( x, y, width, height ); } synchronized ( dirty ) { makeDirty( x, y, width, height ); delegate.drawOval( x, y, width, height ); } } public void fillOval( int x, int y, int width, int height ) { if ( mipMapChild != null ) { mipMapChild.fillOval( x, y, width, height ); } synchronized ( dirty ) { makeDirty( x, y, width, height ); delegate.fillOval( x, y, width, height ); } } public void drawArc( int x, int y, int width, int height, int startAngle, int arcAngle ) { if ( mipMapChild != null ) { mipMapChild.drawArc( x, y, width, height, startAngle, arcAngle ); } synchronized ( dirty ) { makeDirty( x, y, width, height ); delegate.drawArc( x, y, width, height, startAngle, arcAngle ); } } public void fillArc( int x, int y, int width, int height, int startAngle, int arcAngle ) { if ( mipMapChild != null ) { mipMapChild.fillArc( x, y, width, height, startAngle, arcAngle ); } synchronized ( dirty ) { makeDirty( x, y, width, height ); delegate.fillArc( x, y, width, height, startAngle, arcAngle ); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?