📄 swtgraphics2d.java
字号:
* * @see java.awt.Graphics#drawString(java.lang.String, int, int) */ public void drawString(String text, int x, int y) { float fm = this.gc.getFontMetrics().getAscent(); this.gc.drawString(text, x, (int) (y - fm), true); } /** * Draws a string at the specified position. * * @param text the string. * @param x the x-coordinate. * @param y the y-coordinate. */ public void drawString(String text, float x, float y) { float fm = this.gc.getFontMetrics().getAscent(); this.gc.drawString(text, (int) x, (int) (y - fm), true); } /** * Draws a string at the specified position. * * @param iterator the string. * @param x the x-coordinate. * @param y the y-coordinate. */ public void drawString(AttributedCharacterIterator iterator, int x, int y) { // TODO Auto-generated method stub } /** * Draws a string at the specified position. * * @param iterator the string. * @param x the x-coordinate. * @param y the y-coordinate. */ public void drawString(AttributedCharacterIterator iterator, float x, float y) { // TODO Auto-generated method stub } /** * Not implemented - see {@link Graphics2D#hit(Rectangle, Shape, boolean)}. * * @return <code>false</code>. */ public boolean hit(Rectangle rect, Shape text, boolean onStroke) { // TODO Auto-generated method stub return false; } /** * Not implemented - see {@link Graphics#copyArea(int, int, int, int, int, * int)}. */ public void copyArea(int x, int y, int width, int height, int dx, int dy) { // TODO Auto-generated method stub } /** * Not implemented - see {@link Graphics2D#drawImage(Image, * AffineTransform, ImageObserver)}. * * @param image the image. * @param xform the transform. * @param obs an image observer. * * @return A boolean. */ public boolean drawImage(Image image, AffineTransform xform, ImageObserver obs) { // TODO Auto-generated method stub return false; } /** * Draws an image. * * @param image the image. * @param op the image operation. * @param x the x-coordinate. * @param y the y-coordinate. */ public void drawImage(BufferedImage image, BufferedImageOp op, int x, int y) { org.eclipse.swt.graphics.Image im = new org.eclipse.swt.graphics.Image( this.gc.getDevice(), SWTUtils.convertToSWT(image)); this.gc.drawImage(im, x, y); im.dispose(); } /** * Draws an SWT image with the top left corner of the image aligned to the * point (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) { this.gc.drawImage(image, x, y); } /** * Not implemented - see {@link Graphics2D#drawRenderedImage(RenderedImage, * AffineTransform)}. * * @param image the image. * @param xform the transform. */ public void drawRenderedImage(RenderedImage image, AffineTransform xform) { // TODO Auto-generated method stub } /** * Not implemented - see {@link Graphics2D#drawRenderableImage( * RenderableImage, AffineTransform)}. * * @param image the image. * @param xform the transform. */ public void drawRenderableImage(RenderableImage image, AffineTransform xform) { // TODO Auto-generated method stub } /** * Draws an image with the top left corner aligned to the point (x, y). * * @param image the image. * @param x the x-coordinate. * @param y the y-coordinate. * @param observer ignored here. * * @return <code>true</code> if the image has been drawn. */ public boolean drawImage(Image image, int x, int y, ImageObserver observer) { ImageData data = SWTUtils.convertAWTImageToSWT(image); if (data == null) { return false; } org.eclipse.swt.graphics.Image im = new org.eclipse.swt.graphics.Image( this.gc.getDevice(), data); this.gc.drawImage(im, x, y); im.dispose(); return true; } /** * Draws an image with the top left corner aligned to the point (x, y), * and scaled to the specified width and height. * * @param image the image. * @param x the x-coordinate. * @param y the y-coordinate. * @param width the width for the rendered image. * @param height the height for the rendered image. * @param observer ignored here. * * @return <code>true</code> if the image has been drawn. */ public boolean drawImage(Image image, int x, int y, int width, int height, ImageObserver observer) { ImageData data = SWTUtils.convertAWTImageToSWT(image); if (data == null) { return false; } org.eclipse.swt.graphics.Image im = new org.eclipse.swt.graphics.Image( this.gc.getDevice(), data); org.eclipse.swt.graphics.Rectangle bounds = im.getBounds(); this.gc.drawImage(im, 0, 0, bounds.width, bounds.height, x, y, width, height); im.dispose(); return true; } /** * Draws an image. * * @param image (<code>null</code> not permitted). * @param x the x-coordinate. * @param y the y-coordinate. * @param bgcolor the background color. * @param observer an image observer. * * @return A boolean. */ public boolean drawImage(Image image, int x, int y, Color bgcolor, ImageObserver observer) { if (image == null) { throw new IllegalArgumentException("Null 'image' argument."); } int w = image.getWidth(null); int h = image.getHeight(null); if (w == -1 || h == -1) { return false; } Paint savedPaint = getPaint(); fill(new Rectangle2D.Double(x, y, w, h)); setPaint(savedPaint); return drawImage(image, x, y, observer); } /** * Draws an image. * * @param image the image (<code>null</code> not permitted). * @param x the x-coordinate. * @param y the y-coordinate. * @param width the width. * @param height the height. * @param bgcolor the background colour. * @param observer an image observer. * * @return A boolean. */ public boolean drawImage(Image image, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) { if (image == null) { throw new IllegalArgumentException("Null 'image' argument."); } int w = image.getWidth(null); int h = image.getHeight(null); if (w == -1 || h == -1) { return false; } Paint savedPaint = getPaint(); fill(new Rectangle2D.Double(x, y, w, h)); setPaint(savedPaint); return drawImage(image, x, y, width, height, observer); } /** * Not implemented - see {@link Graphics#drawImage(Image, int, int, int, * int, int, int, int, int, ImageObserver)}. * * @param image the image. * @param dx1 * @param dy1 * @param dx2 * @param dy2 * @param sx1 * @param sy1 * @param sx2 * @param sy2 * @param observer */ public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) { // TODO Auto-generated method stub return false; } /** * Not implemented - see {@link Graphics#drawImage(Image, int, int, int, * int, int, int, int, int, Color, ImageObserver)}. * * @param image the image. * @param dx1 * @param dy1 * @param dx2 * @param dy2 * @param sx1 * @param sy1 * @param sx2 * @param sy2 * @param bgcolor * @param observer */ public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) { // TODO Auto-generated method stub return false; } /** * Releases resources held by this instance (but note that the caller * must dispose of the 'GC' passed to the constructor). * * @see java.awt.Graphics#dispose() */ public void dispose() { // we dispose resources we own but user must dispose gc disposeResourcePool(); } /** * Add given swt resource to the resource pool. All resources added * to the resource pool will be disposed 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) { this.resourcePool.add(resource); return resource; } /** * Dispose the resource pool. */ private void disposeResourcePool() { for (Iterator it = this.resourcePool.iterator(); it.hasNext();) { Resource resource = (Resource) it.next(); resource.dispose(); } this.fontsPool.clear(); this.colorsPool.clear(); this.transformsPool.clear(); this.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) this.fontsPool.get(font); if (swtFont == null) { swtFont = new org.eclipse.swt.graphics.Font(this.gc.getDevice(), SWTUtils.toSwtFontData(this.gc.getDevice(), font, true)); addToResourcePool(swtFont); this.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(this.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; } /** * Internal method to convert a AWT transform object into * a SWT transform resource. If a corresponding SWT transform * instance is already in the pool, it will be used * instead of creating a new one. This is used in * {@link #setTransform()} for instance. * * @param awtTransform The AWT transform to convert. * @return A SWT transform instance. */ private Transform getSwtTransformFromPool(AffineTransform awtTransform) { Transform t = (Transform) this.transformsPool.get(awtTransform); if (t == null) { t = new Transform(this.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]); addToResourcePool(t); this.transformsPool.put(awtTransform, t); } return t; } /** * 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 = this.gc.getBackground(); org.eclipse.swt.graphics.Color fg = this.gc.getForeground(); this.gc.setBackground(fg); this.gc.setForeground(bg); } /** * Converts an AWT <code>Shape</code> into a SWT <code>Path</code>. * * @param shape the shape (<code>null</code> not permitted). * * @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 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; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -