📄 nativegraphics.java
字号:
/* * NativeGraphics - concrete, hidden implementation of abstract Graphics * * The approach of using an abstract Graphics with a concrete NativeGraphics * (for both factory purposes and screen drawing) is used in favor of a * branched, concrete Graphics (with a "upward-overloaded" subGraphics() * method) because of two reasons: * * (1) the subGraphics() can be assumed to be a non-public quirk in one * possible subclass (SwingGraphics), it might be changed anytime (or become * invisible because of changed attributes / attribute behavior) * * (2) the subGraphics-related branch (depending on a method call) has to be * used in some potentially high-frequently called methods (e.g. the * out-of-update relevant drawChars(), which is heavily used in editors). It * is assumed to be more efficient to rely completely on overloading of methods * (accepting a rather small speed degradation in Container.paintChildren, where * we can't directly use Graphics fields (e.g. clip rect) anymore). * * Copyright (c) 1998 * Transvirtual Technologies, Inc. All rights reserved. * * Copyright (c) 2004, 2005 * The Kaffe.org's developers. See ChangeLog for details. * * See the file "license.terms" for information on usage and redistribution * of this file. * * @author P.C.Mehlitz */package java.awt;import java.awt.image.ImageObserver;import gnu.classpath.Pointer;class NativeGraphics extends Graphics{ Pointer nativeData; int xOffset; int yOffset; Color foreground; Color background; Font font; int xClip; int yClip; int wClip; int wClipDefault; int hClip; int hClipDefault; Color xClr;/* * this field can be used to link a Graphics object to a non-native * Component (e.g. Panel, Canvas), so that it can be notified in case the * component changes visibility and/or location/size. */ Component target; GraphicsLink link; NativeGraphics next; static NativeGraphics cache; static Object lock = new Object(); final static int TGT_TYPE_WINDOW = 0; final static int TGT_TYPE_IMAGE = 1; final static int TGT_TYPE_GRAPHICS = 2;public void clearRect ( int x, int y, int width, int height ) { Toolkit.graClearRect( nativeData, x, y, width, height);}public void clipRect ( int x, int y, int width, int height ) { // according to the specs, this only shrinks the clip region to the // intersection of the current region and the specified rect, i.e. it // cannot be used to implement multi-rectangular clipping regions int xNew = (x > xClip) ? x : xClip; int yNew = (y > yClip) ? y : yClip; int clipXw = xClip + wClip; int clipYh = yClip + hClip; int xw = x + width; int yh = y + height; int wNew = ((xw > clipXw) ? clipXw : xw) - xNew; int hNew = ((yh > clipYh) ? clipYh : yh) - yNew; if ( wNew < 0 ) wNew = 0; if ( hNew < 0 ) hNew = 0; xClip = xNew; yClip = yNew; wClip = wNew; hClip = hNew; Toolkit.graSetClip( nativeData, xClip, yClip, wClip, hClip);}public void copyArea ( int x, int y, int width, int height, int dx, int dy ){ Toolkit.graCopyArea( nativeData, x, y, width, height, dx, dy);}public Graphics create () { NativeGraphics g = getGraphics( this, nativeData, TGT_TYPE_GRAPHICS, xOffset, yOffset, xClip, yClip, wClip, hClip, foreground, background, font, false); if ( xClr != null ) g.setXORMode( xClr); if ( target != null ){ target.linkGraphics( g); } return g;}public Graphics create ( int x, int y, int width, int height ) { // pretty redundant with setClip, but since it might be called frequently // (Swing, again), it is a good idea to save a native call int xw = x + width; int yh = y + height; int cxw = xClip + wClip; int cyh = yClip + hClip; int clx = (x > xClip) ? x : xClip; int cly = (y > yClip) ? y : yClip; int clw = ((xw > cxw) ? cxw : xw) - clx; int clh = ((yh > cyh) ? cyh : yh) - cly; NativeGraphics g = getGraphics( this, nativeData, TGT_TYPE_GRAPHICS, x + xOffset, y + yOffset, clx - x, cly - y, clw, clh, foreground, background, font, false); if ( xClr != null ) g.setXORMode( xClr); if ( target != null ){ target.linkGraphics( g); } return g;}public void dispose () { if ( background == null ) { // We have to provide some protection against double disposes // since it would make the whole cache inconsistent (leads to // simultanous use). We shouldn't store the target here because // this would lead to a memory leak if Graphics objects are // not disposed. Each of the colors should be a good choice // (since they can't get null while in use) return; } if ( target != null ) { // make sure we don't get a interspersed updateLinkedGraphics() synchronized ( target ) { xClr = null; font = null; foreground = null; background = null; target.unlinkGraphics( this); target = null; } } else { xClr = null; font = null; foreground = null; background = null; } if ( nativeData != null ) { Toolkit.graFreeGraphics( nativeData); nativeData = null; } synchronized ( lock ) { next = cache; cache = this; }}public void draw3DRect ( int x, int y, int width, int height, boolean raised ){ // we pass the rgb color value because it might be stored on the native // side as pixel value (requiring an additional color conversion) Toolkit.graDraw3DRect( nativeData, x, y, width, height, raised, foreground.value);}public void drawArc ( int x, int y, int width, int height, int startAngle, int arcAngle ){ Toolkit.graDrawArc( nativeData, x, y, width, height, startAngle, arcAngle);}public void drawBytes ( byte data[], int offset, int length, int x, int y ){ Toolkit.graDrawBytes( nativeData, data, offset, length, x, y);}public void drawChars ( char data[], int offset, int length, int x, int y ){ Toolkit.graDrawChars( nativeData, data, offset, length, x, y);}public boolean drawImage (Image img, int x, int y, Color bgcolor, ImageObserver observer) { // if the image isn't loaded yet, start production and return false if ( !Image.loadImage( img, -1, -1, observer)) { return (false); } else { drawImg( img, x, y, 0, 0, img.width, img.height, bgcolor); return true; }}public boolean drawImage ( Image img, int x, int y, ImageObserver observer) { if ( !Image.loadImage( img, -1, -1, observer)) { return (false); } else { drawImg( img, x, y, 0, 0, img.width, img.height, null); return true; }}public boolean drawImage ( Image img, int x, int y, int width, int height, Color background, ImageObserver observer ) { // Load image if it's not loaded - we don't scale because we // can do this while drawing. if ( !Image.loadImage( img, -1, -1, observer)) { return (false); } // Handle proportional widths and heights if (width < 0) { width = img.width; if (height < 0) { height = img.height; } else { width = (width * height) / img.height; } } else if (height < 0) { height = (img.height * width) / img.width; } if ( (img.width != width) || (img.height != height) ){ drawImgScaled( img, x, y, x+width, y+height, 0, 0, img.width, img.height, background); } else { drawImg( img, x, y, 0, 0, width, height, background); } return true;}public boolean drawImage ( Image img, int x, int y, int width, int height, ImageObserver observer){ return drawImage( img, x, y, width, height, null, observer);}public boolean drawImage ( Image img, int dx0, int dy0, int dx1, int dy1, int sx0, int sy0, int sx1, int sy1, Color bgColor, ImageObserver observer) { // Load image if it's not loaded - we don't scale because we // can do this while drawing. if (!Image.loadImage( img, -1, -1, observer)) { return (false); } // If any of the source points are negative then error if (sx0 < 0 || sy0 < 0 || sx1 < 0 || sy1 < 0) { return (false); } if ( ((sx1 - sx0) == (dx1 - dx0)) && ((sy1 - sy0) == (dy1 - dy0)) ) { // bozo. don't you know about the costs of image scaling? drawImg( img, dx0, dy0, sx0, sy0, (sx1 - sx0), (sy1 - sy0), bgColor); } else { // We don't create a scaled Image instance since we can draw scaled drawImgScaled( img, dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, bgColor); } return true;}public boolean drawImage ( Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) { return drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null, observer);}void drawImg ( Image img, int x, int y, int sx, int sy, int width, int height, Color background ){ if ( (img.flags & Image.BLOCK_FRAMELOADER) != 0 ){ img.activateFrameLoader(); } else if ( (img.flags & Image.IS_ANIMATION) != 0 ) { Toolkit.imgSetFrame( img.nativeData, 0); } Toolkit.graDrawImage( nativeData, img.nativeData, sx, sy, x, y, width, height, (background == null) ? -1 : Toolkit.clrGetPixelValue(background.getRGB()));}void drawImgScaled ( Image img, int dx0, int dy0, int dx1, int dy1, int sx0, int sy0, int sx1, int sy1, Color background ){ if ( img.nativeData != null ) { if ( (img.flags & Image.BLOCK_FRAMELOADER) != 0 ) img.activateFrameLoader(); Toolkit.graDrawImageScaled( nativeData, img.nativeData, dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, (background == null) ? -1 : Toolkit.clrGetPixelValue(background.getRGB())); }}public void drawLine ( int x1, int y1, int x2, int y2 ){ Toolkit.graDrawLine( nativeData, x1, y1, x2, y2);}public void drawOval ( int x, int y, int width, int height ){ Toolkit.graDrawOval( nativeData, x, y, width, height);}public void drawPolygon ( Polygon p ){ Toolkit.graDrawPolygon( nativeData, p.xpoints, p.ypoints, p.npoints);}public void drawPolygon ( int xPoints[], int yPoints[], int nPoints ){ Toolkit.graDrawPolygon( nativeData, xPoints, yPoints, nPoints);}public void drawPolyline ( int xPoints[], int yPoints[], int nPoints ){ Toolkit.graDrawPolyline( nativeData, xPoints, yPoints, nPoints);}public void drawRect ( int x, int y, int width, int height ) { Toolkit.graDrawRect( nativeData, x, y, width, height);}public void drawRoundRect ( int x, int y, int width, int height, int arcWidth, int arcHeight){ Toolkit.graDrawRoundRect( nativeData, x, y, width, height, arcWidth, arcHeight);}public void drawString ( String str, int x, int y ){ Toolkit.graDrawString( nativeData, str, x, y);}public void fill3DRect ( int x, int y, int width, int height, boolean raised ){ Toolkit.graFill3DRect( nativeData, x, y, width, height, raised, foreground.value);}public void fillArc ( int x, int y, int width, int height, int startAngle, int arcAngle ){ Toolkit.graFillArc( nativeData, x, y, width, height, startAngle, arcAngle);}public void fillOval ( int x, int y, int width, int height ){ Toolkit.graFillOval( nativeData, x, y, width, height);}public void fillPolygon ( Polygon p ){ Toolkit.graFillPolygon( nativeData, p.xpoints, p.ypoints, p.npoints);}public void fillPolygon ( int xPoints[], int yPoints[], int nPoints ){ Toolkit.graFillPolygon( nativeData, xPoints, yPoints, nPoints);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -