0001: /* ===========================================================
0002: * JFreeChart : a free chart library for the Java(tm) platform
0003: * ===========================================================
0004: *
0005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
0006: *
0007: * Project Info: http://www.jfree.org/jfreechart/index.html
0008: *
0009: * This library is free software; you can redistribute it and/or modify it
0010: * under the terms of the GNU Lesser General Public License as published by
0011: * the Free Software Foundation; either version 2.1 of the License, or
0012: * (at your option) any later version.
0013: *
0014: * This library is distributed in the hope that it will be useful, but
0015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
0016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
0017: * License for more details.
0018: *
0019: * You should have received a copy of the GNU Lesser General Public
0020: * License along with this library; if not, write to the Free Software
0021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
0022: * USA.
0023: *
0024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
0025: * in the United States and other countries.]
0026: *
0027: * ------------------
0028: * SWTGraphics2D.java
0029: * ------------------
0030: * (C) Copyright 2006, 2007, by Henry Proudhon and Contributors.
0031: *
0032: * Original Author: Henry Proudhon (henry.proudhon AT insa-lyon.fr);
0033: * Contributor(s): Cedric Chabanois (cchabanois AT no-log.org);
0034: * David Gilbert (for Object Refinery Limited);
0035: *
0036: * Changes
0037: * -------
0038: * 14-Jun-2006 : New class (HP);
0039: * 29-Jan-2007 : fixed the fillRect method (HP);
0040: * 31-Jan-2007 : moved the dummy JPanel to SWTUtils.java,
0041: * implemented the drawLine method (HP);
0042: * 07-Apr-2007 : dispose some of the swt ressources,
0043: * thanks to silent for pointing this out (HP);
0044: * 23-May-2007 : removed resource leaks by adding a resource pool (CC);
0045: * 15-Jun-2007 : Fixed compile error for JDK 1.4 (DG);
0046: *
0047: */
0048:
0049: package org.jfree.experimental.swt;
0050:
0051: import java.awt.BasicStroke;
0052: import java.awt.Color;
0053: import java.awt.Composite;
0054: import java.awt.Font;
0055: import java.awt.FontMetrics;
0056: import java.awt.Graphics;
0057: import java.awt.Graphics2D;
0058: import java.awt.GraphicsConfiguration;
0059: import java.awt.Image;
0060: import java.awt.Paint;
0061: import java.awt.Rectangle;
0062: import java.awt.RenderingHints;
0063: import java.awt.Shape;
0064: import java.awt.Stroke;
0065: import java.awt.RenderingHints.Key;
0066: import java.awt.font.FontRenderContext;
0067: import java.awt.font.GlyphVector;
0068: import java.awt.geom.AffineTransform;
0069: import java.awt.geom.PathIterator;
0070: import java.awt.image.BufferedImage;
0071: import java.awt.image.BufferedImageOp;
0072: import java.awt.image.DirectColorModel;
0073: import java.awt.image.ImageObserver;
0074: import java.awt.image.IndexColorModel;
0075: import java.awt.image.RenderedImage;
0076: import java.awt.image.WritableRaster;
0077: import java.awt.image.renderable.RenderableImage;
0078: import java.text.AttributedCharacterIterator;
0079: import java.util.ArrayList;
0080: import java.util.HashMap;
0081: import java.util.Iterator;
0082: import java.util.List;
0083: import java.util.Map;
0084:
0085: import org.eclipse.swt.SWT;
0086: import org.eclipse.swt.graphics.FontData;
0087: import org.eclipse.swt.graphics.GC;
0088: import org.eclipse.swt.graphics.ImageData;
0089: import org.eclipse.swt.graphics.PaletteData;
0090: import org.eclipse.swt.graphics.Path;
0091: import org.eclipse.swt.graphics.RGB;
0092: import org.eclipse.swt.graphics.Resource;
0093: import org.eclipse.swt.graphics.Transform;
0094:
0095: /**
0096: * This is a class utility to draw Graphics2D stuff on a swt composite.
0097: * It is presently developed to use JFreeChart with the Standard
0098: * Widget Toolkit but may be of a wider use later.
0099: */
0100: public class SWTGraphics2D extends Graphics2D {
0101:
0102: /** The swt graphic composite */
0103: private GC gc;
0104:
0105: /** A HashMap to store the Swt color resources. */
0106: private Map colorsPool = new HashMap();
0107:
0108: /** A HashMap to store the Swt font resources. */
0109: private Map fontsPool = new HashMap();
0110:
0111: /** A HashMap to store the Swt color resources. */
0112: private List resourcePool = new ArrayList();
0113:
0114: /**
0115: * Creates a new instance.
0116: *
0117: * @param gc the graphics context.
0118: */
0119: public SWTGraphics2D(GC gc) {
0120: super ();
0121: this .gc = gc;
0122: }
0123:
0124: /**
0125: * Add given swt resource to the resource pool. All resources added
0126: * to the resource pool will be dispose when {@link #dispose()} is called
0127: *
0128: * @param resource the resource to add to the pool.
0129: * @return the swt <code>Resource</code> just added.
0130: */
0131: private Resource addToResourcePool(Resource resource) {
0132: resourcePool.add(resource);
0133: return resource;
0134: }
0135:
0136: /**
0137: * Dispose the resource pool.
0138: */
0139: private void disposeResourcePool() {
0140: for (Iterator it = resourcePool.iterator(); it.hasNext();) {
0141: Resource resource = (Resource) it.next();
0142: resource.dispose();
0143: }
0144: resourcePool.clear();
0145: colorsPool.clear();
0146: resourcePool.clear();
0147: }
0148:
0149: /**
0150: * Internal method to convert a AWT font object into
0151: * a SWT font resource. If a corresponding SWT font
0152: * instance is already in the pool, it will be used
0153: * instead of creating a new one. This is used in
0154: * {@link #setFont()} for instance.
0155: *
0156: * @param font The AWT font to convert.
0157: * @return The SWT font instance.
0158: */
0159: private org.eclipse.swt.graphics.Font getSwtFontFromPool(Font font) {
0160: org.eclipse.swt.graphics.Font swtFont = (org.eclipse.swt.graphics.Font) fontsPool
0161: .get(font);
0162: if (swtFont == null) {
0163: swtFont = new org.eclipse.swt.graphics.Font(gc.getDevice(),
0164: SWTUtils.toSwtFontData(gc.getDevice(), font, true));
0165: addToResourcePool(swtFont);
0166: fontsPool.put(font, swtFont);
0167: }
0168: return swtFont;
0169: }
0170:
0171: /**
0172: * Internal method to convert a AWT color object into
0173: * a SWT color resource. If a corresponding SWT color
0174: * instance is already in the pool, it will be used
0175: * instead of creating a new one. This is used in
0176: * {@link #setColor()} for instance.
0177: *
0178: * @param awtColor The AWT color to convert.
0179: * @return A SWT color instance.
0180: */
0181: private org.eclipse.swt.graphics.Color getSwtColorFromPool(
0182: Color awtColor) {
0183: org.eclipse.swt.graphics.Color swtColor = (org.eclipse.swt.graphics.Color)
0184: // we can't use the following valueOf() method, because it won't
0185: // compile with JDK1.4
0186: //this.colorsPool.get(Integer.valueOf(awtColor.getRGB()));
0187: this .colorsPool.get(new Integer(awtColor.getRGB()));
0188: if (swtColor == null) {
0189: swtColor = SWTUtils.toSwtColor(gc.getDevice(), awtColor);
0190: addToResourcePool(swtColor);
0191: // see comment above
0192: //this.colorsPool.put(Integer.valueOf(awtColor.getRGB()), swtColor);
0193: this .colorsPool.put(new Integer(awtColor.getRGB()),
0194: swtColor);
0195: }
0196: return swtColor;
0197: }
0198:
0199: /**
0200: * Perform a switch between foreground and background
0201: * color of gc. This is needed for consistency with
0202: * the awt behaviour, and is required notably for the
0203: * filling methods.
0204: */
0205: private void switchColors() {
0206: org.eclipse.swt.graphics.Color bg = gc.getBackground();
0207: org.eclipse.swt.graphics.Color fg = gc.getForeground();
0208: gc.setBackground(fg);
0209: gc.setForeground(bg);
0210: }
0211:
0212: /**
0213: * Converts an AWT <code>Shape</code> into a SWT <code>Path</code>.
0214: *
0215: * @param shape the shape.
0216: *
0217: * @return The path.
0218: */
0219: private Path toSwtPath(Shape shape) {
0220: int type;
0221: float[] coords = new float[6];
0222: Path path = new Path(this .gc.getDevice());
0223: PathIterator pit = shape.getPathIterator(null);
0224: while (!pit.isDone()) {
0225: type = pit.currentSegment(coords);
0226: switch (type) {
0227: case (PathIterator.SEG_MOVETO):
0228: path.moveTo(coords[0], coords[1]);
0229: break;
0230: case (PathIterator.SEG_LINETO):
0231: path.lineTo(coords[0], coords[1]);
0232: break;
0233: case (PathIterator.SEG_QUADTO):
0234: path.quadTo(coords[0], coords[1], coords[2], coords[3]);
0235: break;
0236: case (PathIterator.SEG_CUBICTO):
0237: path.cubicTo(coords[0], coords[1], coords[2],
0238: coords[3], coords[4], coords[5]);
0239: break;
0240: case (PathIterator.SEG_CLOSE):
0241: path.close();
0242: break;
0243: default:
0244: break;
0245: }
0246: pit.next();
0247: }
0248: return path;
0249: }
0250:
0251: /**
0252: * Converts an AWT transform into the equivalent SWT transform.
0253: *
0254: * @param awtTransform the AWT transform.
0255: *
0256: * @return The SWT transform.
0257: */
0258: private Transform toSwtTransform(AffineTransform awtTransform) {
0259: Transform t = new Transform(gc.getDevice());
0260: double[] matrix = new double[6];
0261: awtTransform.getMatrix(matrix);
0262: t.setElements((float) matrix[0], (float) matrix[1],
0263: (float) matrix[2], (float) matrix[3],
0264: (float) matrix[4], (float) matrix[5]);
0265: return t;
0266: }
0267:
0268: /**
0269: * Converts an SWT transform into the equivalent AWT transform.
0270: *
0271: * @param swtTransform the SWT transform.
0272: *
0273: * @return The AWT transform.
0274: */
0275: private AffineTransform toAwtTransform(Transform swtTransform) {
0276: float[] elements = new float[6];
0277: swtTransform.getElements(elements);
0278: AffineTransform awtTransform = new AffineTransform(elements);
0279: return awtTransform;
0280: }
0281:
0282: /* (non-Javadoc)
0283: * @see java.awt.Graphics2D#draw(java.awt.Shape)
0284: */
0285: public void draw(Shape shape) {
0286: Path path = toSwtPath(shape);
0287: gc.drawPath(path);
0288: path.dispose();
0289: }
0290:
0291: /* (non-Javadoc)
0292: * @see java.awt.Graphics2D#drawImage(java.awt.Image,
0293: * java.awt.geom.AffineTransform, java.awt.image.ImageObserver)
0294: */
0295: public boolean drawImage(Image image, AffineTransform xform,
0296: ImageObserver obs) {
0297: // TODO Auto-generated method stub
0298: return false;
0299: }
0300:
0301: /* (non-Javadoc)
0302: * @see java.awt.Graphics2D#drawImage(java.awt.image.BufferedImage,
0303: * java.awt.image.BufferedImageOp, int, int)
0304: */
0305: public void drawImage(BufferedImage image, BufferedImageOp op,
0306: int x, int y) {
0307: org.eclipse.swt.graphics.Image im = new org.eclipse.swt.graphics.Image(
0308: gc.getDevice(), convertToSWT(image));
0309: gc.drawImage(im, x, y);
0310: im.dispose();
0311: }
0312:
0313: /**
0314: * Draws an image at (x, y).
0315: *
0316: * @param image the image.
0317: * @param x the x-coordinate.
0318: * @param y the y-coordinate.
0319: */
0320: public void drawImage(org.eclipse.swt.graphics.Image image, int x,
0321: int y) {
0322: gc.drawImage(image, x, y);
0323: }
0324:
0325: /* (non-Javadoc)
0326: * @see java.awt.Graphics2D#drawRenderedImage(java.awt.image.RenderedImage,
0327: * java.awt.geom.AffineTransform)
0328: */
0329: public void drawRenderedImage(RenderedImage image,
0330: AffineTransform xform) {
0331: // TODO Auto-generated method stub
0332: }
0333:
0334: /* (non-Javadoc)
0335: * @see java.awt.Graphics2D#drawRenderableImage(
0336: * java.awt.image.renderable.RenderableImage, java.awt.geom.AffineTransform)
0337: */
0338: public void drawRenderableImage(RenderableImage image,
0339: AffineTransform xform) {
0340: // TODO Auto-generated method stub
0341:
0342: }
0343:
0344: /**
0345: * Draws a string on the receiver. note that
0346: * to be consistent with the awt method,
0347: * the y has to be modified with the ascent of the font.
0348: *
0349: * @see java.awt.Graphics#drawString(java.lang.String, int, int)
0350: */
0351: public void drawString(String text, int x, int y) {
0352: float fm = gc.getFontMetrics().getAscent();
0353: gc.drawString(text, x, (int) (y - fm), true);
0354: }
0355:
0356: /* (non-Javadoc)
0357: * @see java.awt.Graphics2D#drawString(java.lang.String, float, float)
0358: */
0359: public void drawString(String text, float x, float y) {
0360: float fm = gc.getFontMetrics().getAscent();
0361: gc.drawString(text, (int) x, (int) (y - fm), true);
0362: }
0363:
0364: /* (non-Javadoc)
0365: * @see java.awt.Graphics2D#drawString(
0366: * java.text.AttributedCharacterIterator, int, int)
0367: */
0368: public void drawString(AttributedCharacterIterator iterator, int x,
0369: int y) {
0370: // TODO Auto-generated method stub
0371:
0372: }
0373:
0374: /* (non-Javadoc)
0375: * @see java.awt.Graphics2D#drawString(
0376: * java.text.AttributedCharacterIterator, float, float)
0377: */
0378: public void drawString(AttributedCharacterIterator iterator,
0379: float x, float y) {
0380: // TODO Auto-generated method stub
0381:
0382: }
0383:
0384: /** fill an arbitrary shape on the swt graphic composite
0385: * with the current stroke and paint.
0386: * note that for consistency with the awt method, it is needed
0387: * to switch temporarily the foreground and background colors.
0388: * @see java.awt.Graphics2D#fill(java.awt.Shape)
0389: */
0390: public void fill(Shape shape) {
0391: Path path = toSwtPath(shape);
0392: switchColors();
0393: this .gc.fillPath(path);
0394: switchColors();
0395: path.dispose();
0396: }
0397:
0398: /* (non-Javadoc)
0399: * @see java.awt.Graphics2D#hit(java.awt.Rectangle, java.awt.Shape, boolean)
0400: */
0401: public boolean hit(Rectangle rect, Shape text, boolean onStroke) {
0402: // TODO Auto-generated method stub
0403: return false;
0404: }
0405:
0406: /* (non-Javadoc)
0407: * @see java.awt.Graphics2D#getDeviceConfiguration()
0408: */
0409: public GraphicsConfiguration getDeviceConfiguration() {
0410: // TODO Auto-generated method stub
0411: return null;
0412: }
0413:
0414: /* (non-Javadoc)
0415: * @see java.awt.Graphics2D#setComposite(java.awt.Composite)
0416: */
0417: public void setComposite(Composite comp) {
0418: // TODO Auto-generated method stub
0419: }
0420:
0421: /**
0422: * Set the paint associated with the swt graphic composite.
0423: * @see java.awt.Graphics2D#setPaint(java.awt.Paint)
0424: */
0425: public void setPaint(Paint paint) {
0426: if (paint instanceof Color) {
0427: setColor((Color) paint);
0428: } else {
0429: throw new RuntimeException(
0430: "Can only handle 'Color' at present.");
0431: }
0432: }
0433:
0434: /* (non-Javadoc)
0435: * @see java.awt.Graphics2D#setStroke(java.awt.Stroke)
0436: */
0437: public void setStroke(Stroke stroke) {
0438: if (stroke instanceof BasicStroke) {
0439: BasicStroke bs = (BasicStroke) stroke;
0440: // linewidth
0441: gc.setLineWidth((int) bs.getLineWidth());
0442:
0443: // line join
0444: switch (bs.getLineJoin()) {
0445: case BasicStroke.JOIN_BEVEL:
0446: gc.setLineJoin(SWT.JOIN_BEVEL);
0447: break;
0448: case BasicStroke.JOIN_MITER:
0449: gc.setLineJoin(SWT.JOIN_MITER);
0450: break;
0451: case BasicStroke.JOIN_ROUND:
0452: gc.setLineJoin(SWT.JOIN_ROUND);
0453: break;
0454: }
0455:
0456: // line cap
0457: switch (bs.getEndCap()) {
0458: case BasicStroke.CAP_BUTT:
0459: gc.setLineCap(SWT.CAP_FLAT);
0460: break;
0461: case BasicStroke.CAP_ROUND:
0462: gc.setLineCap(SWT.CAP_ROUND);
0463: break;
0464: case BasicStroke.CAP_SQUARE:
0465: gc.setLineCap(SWT.CAP_SQUARE);
0466: break;
0467: }
0468:
0469: // set the line style to solid by default
0470: gc.setLineStyle(SWT.LINE_SOLID);
0471:
0472: // apply dash style if any
0473: float[] dashes = bs.getDashArray();
0474: if (dashes != null) {
0475: int[] swtDashes = new int[dashes.length];
0476: for (int i = 0; i < swtDashes.length; i++) {
0477: swtDashes[i] = (int) dashes[i];
0478: }
0479: gc.setLineDash(swtDashes);
0480: }
0481: } else {
0482: throw new RuntimeException(
0483: "Can only handle 'Basic Stroke' at present.");
0484: }
0485: }
0486:
0487: /* (non-Javadoc)
0488: * @see java.awt.Graphics2D#setRenderingHint(java.awt.RenderingHints.Key,
0489: * java.lang.Object)
0490: */
0491: public void setRenderingHint(Key hintKey, Object hintValue) {
0492: // TODO Auto-generated method stub
0493:
0494: }
0495:
0496: /* (non-Javadoc)
0497: * @see java.awt.Graphics2D#getRenderingHint(java.awt.RenderingHints.Key)
0498: */
0499: public Object getRenderingHint(Key hintKey) {
0500: // TODO Auto-generated method stub
0501: return null;
0502: }
0503:
0504: /* (non-Javadoc)
0505: * @see java.awt.Graphics2D#setRenderingHints(java.util.Map)
0506: */
0507: public void setRenderingHints(Map hints) {
0508: // TODO Auto-generated method stub
0509:
0510: }
0511:
0512: /* (non-Javadoc)
0513: * @see java.awt.Graphics2D#addRenderingHints(java.util.Map)
0514: */
0515: public void addRenderingHints(Map hints) {
0516: // TODO Auto-generated method stub
0517:
0518: }
0519:
0520: /* (non-Javadoc)
0521: * @see java.awt.Graphics2D#getRenderingHints()
0522: */
0523: public RenderingHints getRenderingHints() {
0524: // TODO Auto-generated method stub
0525: return null;
0526: }
0527:
0528: /* (non-Javadoc)
0529: * @see java.awt.Graphics2D#translate(int, int)
0530: */
0531: public void translate(int x, int y) {
0532: Transform swtTransform = new Transform(gc.getDevice());
0533: gc.getTransform(swtTransform);
0534: swtTransform.translate(x, y);
0535: gc.setTransform(swtTransform);
0536: swtTransform.dispose();
0537: }
0538:
0539: /* (non-Javadoc)
0540: * @see java.awt.Graphics2D#translate(double, double)
0541: */
0542: public void translate(double tx, double ty) {
0543: translate((int) tx, (int) ty);
0544: }
0545:
0546: /* (non-Javadoc)
0547: * @see java.awt.Graphics2D#rotate(double)
0548: */
0549: public void rotate(double theta) {
0550: Transform swtTransform = new Transform(gc.getDevice());
0551: gc.getTransform(swtTransform);
0552: swtTransform.rotate((float) (theta * 180 / Math.PI));
0553: gc.setTransform(swtTransform);
0554: swtTransform.dispose();
0555: }
0556:
0557: /* (non-Javadoc)
0558: * @see java.awt.Graphics2D#rotate(double, double, double)
0559: */
0560: public void rotate(double theta, double x, double y) {
0561: // TODO Auto-generated method stub
0562:
0563: }
0564:
0565: /* (non-Javadoc)
0566: * @see java.awt.Graphics2D#scale(double, double)
0567: */
0568: public void scale(double scaleX, double scaleY) {
0569: Transform swtTransform = new Transform(gc.getDevice());
0570: gc.getTransform(swtTransform);
0571: swtTransform.scale((float) scaleX, (float) scaleY);
0572: gc.setTransform(swtTransform);
0573: swtTransform.dispose();
0574: }
0575:
0576: /* (non-Javadoc)
0577: * @see java.awt.Graphics2D#shear(double, double)
0578: */
0579: public void shear(double shearX, double shearY) {
0580: Transform swtTransform = new Transform(gc.getDevice());
0581: gc.getTransform(swtTransform);
0582: Transform shear = new Transform(gc.getDevice(), 1f,
0583: (float) shearX, (float) shearY, 1f, 0, 0);
0584: swtTransform.multiply(shear);
0585: gc.setTransform(swtTransform);
0586: swtTransform.dispose();
0587: }
0588:
0589: /* (non-Javadoc)
0590: * @see java.awt.Graphics2D#transform(java.awt.geom.AffineTransform)
0591: */
0592: public void transform(AffineTransform Tx) {
0593: Transform swtTransform = new Transform(gc.getDevice());
0594: gc.getTransform(swtTransform);
0595: Transform swtMatrix = toSwtTransform(Tx);
0596: swtTransform.multiply(swtMatrix);
0597: gc.setTransform(swtTransform);
0598: swtMatrix.dispose();
0599: swtTransform.dispose();
0600: }
0601:
0602: /* (non-Javadoc)
0603: * @see java.awt.Graphics2D#setTransform(java.awt.geom.AffineTransform)
0604: */
0605: public void setTransform(AffineTransform Tx) {
0606: gc.setTransform(toSwtTransform(Tx));
0607: }
0608:
0609: /* (non-Javadoc)
0610: * @see java.awt.Graphics2D#getTransform()
0611: */
0612: public AffineTransform getTransform() {
0613: Transform swtTransform = new Transform(gc.getDevice());
0614: gc.getTransform(swtTransform);
0615: AffineTransform awtTransform = toAwtTransform(swtTransform);
0616: swtTransform.dispose();
0617: return awtTransform;
0618: }
0619:
0620: /* (non-Javadoc)
0621: * @see java.awt.Graphics2D#getPaint()
0622: */
0623: public Paint getPaint() {
0624: return SWTUtils.toAwtColor(gc.getForeground());
0625: }
0626:
0627: /* (non-Javadoc)
0628: * @see java.awt.Graphics2D#getComposite()
0629: */
0630: public Composite getComposite() {
0631: // TODO Auto-generated method stub
0632: return null;
0633: }
0634:
0635: /* (non-Javadoc)
0636: * @see java.awt.Graphics2D#setBackground(java.awt.Color)
0637: */
0638: public void setBackground(Color color) {
0639: gc.getBackground().dispose();
0640: org.eclipse.swt.graphics.Color swtColor = SWTUtils.toSwtColor(
0641: gc.getDevice(), color);
0642: gc.setBackground(swtColor);
0643: swtColor.dispose();
0644: }
0645:
0646: /* (non-Javadoc)
0647: * @see java.awt.Graphics2D#getBackground()
0648: */
0649: public Color getBackground() {
0650: return SWTUtils.toAwtColor(gc.getBackground());
0651: }
0652:
0653: /* (non-Javadoc)
0654: * @see java.awt.Graphics2D#getStroke()
0655: */
0656: public Stroke getStroke() {
0657: return new BasicStroke(gc.getLineWidth(), gc.getLineCap(), gc
0658: .getLineJoin());
0659: }
0660:
0661: /* (non-Javadoc)
0662: * @see java.awt.Graphics2D#clip(java.awt.Shape)
0663: */
0664: public void clip(Shape s) {
0665: Path path = toSwtPath(s);
0666: gc.setClipping(path);
0667: path.dispose();
0668: }
0669:
0670: /* (non-Javadoc)
0671: * @see java.awt.Graphics2D#getFontRenderContext()
0672: */
0673: public FontRenderContext getFontRenderContext() {
0674: FontRenderContext fontRenderContext = new FontRenderContext(
0675: new AffineTransform(), true, true);
0676: return fontRenderContext;
0677: }
0678:
0679: /* (non-Javadoc)
0680: * @see java.awt.Graphics2D#drawGlyphVector(java.awt.font.GlyphVector,
0681: * float, float)
0682: */
0683: public void drawGlyphVector(GlyphVector g, float x, float y) {
0684: // TODO Auto-generated method stub
0685:
0686: }
0687:
0688: /* (non-Javadoc)
0689: * @see java.awt.Graphics#create()
0690: */
0691: public Graphics create() {
0692: // TODO Auto-generated method stub
0693: return null;
0694: }
0695:
0696: /* (non-Javadoc)
0697: * @see java.awt.Graphics#getColor()
0698: */
0699: public Color getColor() {
0700: return SWTUtils.toAwtColor(gc.getForeground());
0701: }
0702:
0703: /* (non-Javadoc)
0704: * @see java.awt.Graphics#setColor(java.awt.Color)
0705: */
0706: public void setColor(Color color) {
0707: org.eclipse.swt.graphics.Color swtColor = getSwtColorFromPool(color);
0708: gc.setForeground(swtColor);
0709: }
0710:
0711: /* (non-Javadoc)
0712: * @see java.awt.Graphics#setPaintMode()
0713: */
0714: public void setPaintMode() {
0715: // TODO Auto-generated method stub
0716: }
0717:
0718: /* (non-Javadoc)
0719: * @see java.awt.Graphics#setXORMode(java.awt.Color)
0720: */
0721: public void setXORMode(Color color) {
0722: // TODO Auto-generated method stub
0723:
0724: }
0725:
0726: /**
0727: * Returns the font in form of an awt font created
0728: * with the parameters of the font of the swt graphic
0729: * composite.
0730: * @see java.awt.Graphics#getFont()
0731: */
0732: public Font getFont() {
0733: // retrieve the swt font description in an os indept way
0734: FontData[] fontData = gc.getFont().getFontData();
0735: // create a new awt font with the appropiate data
0736: return SWTUtils.toAwtFont(gc.getDevice(), fontData[0], true);
0737: }
0738:
0739: /**
0740: * Set the font swt graphic composite from the specified
0741: * awt font. Be careful that the newly created swt font
0742: * must be disposed separately.
0743: * @see java.awt.Graphics#setFont(java.awt.Font)
0744: */
0745: public void setFont(Font font) {
0746: org.eclipse.swt.graphics.Font swtFont = getSwtFontFromPool(font);
0747: gc.setFont(swtFont);
0748: }
0749:
0750: /* (non-Javadoc)
0751: * @see java.awt.Graphics#getFontMetrics(java.awt.Font)
0752: */
0753: public FontMetrics getFontMetrics(Font font) {
0754: return SWTUtils.DUMMY_PANEL.getFontMetrics(font);
0755: }
0756:
0757: /* (non-Javadoc)
0758: * @see java.awt.Graphics#getClipBounds()
0759: */
0760: public Rectangle getClipBounds() {
0761: org.eclipse.swt.graphics.Rectangle clip = gc.getClipping();
0762: return new Rectangle(clip.x, clip.y, clip.width, clip.height);
0763: }
0764:
0765: /* (non-Javadoc)
0766: * @see java.awt.Graphics#clipRect(int, int, int, int)
0767: */
0768: public void clipRect(int x, int y, int width, int height) {
0769: org.eclipse.swt.graphics.Rectangle clip = gc.getClipping();
0770: clip.intersects(x, y, width, height);
0771: gc.setClipping(clip);
0772: }
0773:
0774: /* (non-Javadoc)
0775: * @see java.awt.Graphics#setClip(int, int, int, int)
0776: */
0777: public void setClip(int x, int y, int width, int height) {
0778: gc.setClipping(x, y, width, height);
0779: }
0780:
0781: /* (non-Javadoc)
0782: * @see java.awt.Graphics#getClip()
0783: */
0784: public Shape getClip() {
0785: // TODO Auto-generated method stub
0786: return null;
0787: }
0788:
0789: /* (non-Javadoc)
0790: * @see java.awt.Graphics#setClip(java.awt.Shape)
0791: */
0792: public void setClip(Shape clip) {
0793: if (clip == null)
0794: return;
0795: Path clipPath = toSwtPath(clip);
0796: gc.setClipping(clipPath);
0797: clipPath.dispose();
0798: }
0799:
0800: /* (non-Javadoc)
0801: * @see java.awt.Graphics#copyArea(int, int, int, int, int, int)
0802: */
0803: public void copyArea(int x, int y, int width, int height, int dx,
0804: int dy) {
0805: // TODO Auto-generated method stub
0806:
0807: }
0808:
0809: /**
0810: * Draws a line on the swt graphic composite.
0811: * @see java.awt.Graphics#drawLine(int, int, int, int)
0812: */
0813: public void drawLine(int x1, int y1, int x2, int y2) {
0814: gc.drawLine(x1, y1, x2, y2);
0815: }
0816:
0817: /**
0818: * Fill a rectangle area on the swt graphic composite.
0819: * The <code>fillRectangle</code> method of the <code>GC</code>
0820: * class uses the background color so we must switch colors.
0821: * @see java.awt.Graphics#fillRect(int, int, int, int)
0822: */
0823: public void fillRect(int x, int y, int width, int height) {
0824: this .switchColors();
0825: gc.fillRectangle(x, y, width, height);
0826: this .switchColors();
0827: }
0828:
0829: /* (non-Javadoc)
0830: * @see java.awt.Graphics#clearRect(int, int, int, int)
0831: */
0832: public void clearRect(int x, int y, int width, int height) {
0833: // TODO Auto-generated method stub
0834:
0835: }
0836:
0837: /* (non-Javadoc)
0838: * @see java.awt.Graphics#drawRoundRect(int, int, int, int, int, int)
0839: */
0840: public void drawRoundRect(int x, int y, int width, int height,
0841: int arcWidth, int arcHeight) {
0842: // TODO Auto-generated method stub
0843:
0844: }
0845:
0846: /* (non-Javadoc)
0847: * @see java.awt.Graphics#fillRoundRect(int, int, int, int, int, int)
0848: */
0849: public void fillRoundRect(int x, int y, int width, int height,
0850: int arcWidth, int arcHeight) {
0851: // TODO Auto-generated method stub
0852:
0853: }
0854:
0855: /* (non-Javadoc)
0856: * @see java.awt.Graphics#drawOval(int, int, int, int)
0857: */
0858: public void drawOval(int x, int y, int width, int height) {
0859: // TODO Auto-generated method stub
0860:
0861: }
0862:
0863: /* (non-Javadoc)
0864: * @see java.awt.Graphics#fillOval(int, int, int, int)
0865: */
0866: public void fillOval(int x, int y, int width, int height) {
0867: // TODO Auto-generated method stub
0868:
0869: }
0870:
0871: /* (non-Javadoc)
0872: * @see java.awt.Graphics#drawArc(int, int, int, int, int, int)
0873: */
0874: public void drawArc(int x, int y, int width, int height,
0875: int arcStart, int arcAngle) {
0876: // TODO Auto-generated method stub
0877:
0878: }
0879:
0880: /* (non-Javadoc)
0881: * @see java.awt.Graphics#fillArc(int, int, int, int, int, int)
0882: */
0883: public void fillArc(int x, int y, int width, int height,
0884: int arcStart, int arcAngle) {
0885: // TODO Auto-generated method stub
0886:
0887: }
0888:
0889: /* (non-Javadoc)
0890: * @see java.awt.Graphics#drawPolyline(int[], int[], int)
0891: */
0892: public void drawPolyline(int[] xPoints, int[] yPoints, int npoints) {
0893: // TODO Auto-generated method stub
0894:
0895: }
0896:
0897: /* (non-Javadoc)
0898: * @see java.awt.Graphics#drawPolygon(int[], int[], int)
0899: */
0900: public void drawPolygon(int[] xPoints, int[] yPoints, int npoints) {
0901: // TODO Auto-generated method stub
0902:
0903: }
0904:
0905: /* (non-Javadoc)
0906: * @see java.awt.Graphics#fillPolygon(int[], int[], int)
0907: */
0908: public void fillPolygon(int[] xPoints, int[] yPoints, int npoints) {
0909: // TODO Auto-generated method stub
0910:
0911: }
0912:
0913: /* (non-Javadoc)
0914: * @see java.awt.Graphics#drawImage(java.awt.Image, int, int,
0915: * java.awt.image.ImageObserver)
0916: */
0917: public boolean drawImage(Image image, int x, int y,
0918: ImageObserver observer) {
0919: // TODO Auto-generated method stub
0920: return false;
0921: }
0922:
0923: /* (non-Javadoc)
0924: * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int,
0925: * java.awt.image.ImageObserver)
0926: */
0927: public boolean drawImage(Image image, int x, int y, int width,
0928: int height, ImageObserver observer) {
0929: // TODO Auto-generated method stub
0930: return false;
0931: }
0932:
0933: /* (non-Javadoc)
0934: * @see java.awt.Graphics#drawImage(java.awt.Image, int, int,
0935: * java.awt.Color, java.awt.image.ImageObserver)
0936: */
0937: public boolean drawImage(Image image, int x, int y, Color bgcolor,
0938: ImageObserver observer) {
0939: // TODO Auto-generated method stub
0940: return false;
0941: }
0942:
0943: /* (non-Javadoc)
0944: * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int,
0945: * java.awt.Color, java.awt.image.ImageObserver)
0946: */
0947: public boolean drawImage(Image image, int x, int y, int width,
0948: int height, Color bgcolor, ImageObserver observer) {
0949: // TODO Auto-generated method stub
0950: return false;
0951: }
0952:
0953: /* (non-Javadoc)
0954: * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int,
0955: * int, int, int, int, java.awt.image.ImageObserver)
0956: */
0957: public boolean drawImage(Image image, int dx1, int dy1, int dx2,
0958: int dy2, int sx1, int sy1, int sx2, int sy2,
0959: ImageObserver observer) {
0960: // TODO Auto-generated method stub
0961: return false;
0962: }
0963:
0964: /* (non-Javadoc)
0965: * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int,
0966: * int, int, int, int, java.awt.Color, java.awt.image.ImageObserver)
0967: */
0968: public boolean drawImage(Image image, int dx1, int dy1, int dx2,
0969: int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor,
0970: ImageObserver observer) {
0971: // TODO Auto-generated method stub
0972: return false;
0973: }
0974:
0975: /* (non-Javadoc)
0976: * @see java.awt.Graphics#dispose()
0977: */
0978: public void dispose() {
0979: // we dispose resources we own but user must dispose gc
0980: disposeResourcePool();
0981: }
0982:
0983: static ImageData convertToSWT(BufferedImage bufferedImage) {
0984: if (bufferedImage.getColorModel() instanceof DirectColorModel) {
0985: DirectColorModel colorModel = (DirectColorModel) bufferedImage
0986: .getColorModel();
0987: PaletteData palette = new PaletteData(colorModel
0988: .getRedMask(), colorModel.getGreenMask(),
0989: colorModel.getBlueMask());
0990: ImageData data = new ImageData(bufferedImage.getWidth(),
0991: bufferedImage.getHeight(), colorModel
0992: .getPixelSize(), palette);
0993: WritableRaster raster = bufferedImage.getRaster();
0994: int[] pixelArray = new int[3];
0995: for (int y = 0; y < data.height; y++) {
0996: for (int x = 0; x < data.width; x++) {
0997: raster.getPixel(x, y, pixelArray);
0998: int pixel = palette.getPixel(new RGB(pixelArray[0],
0999: pixelArray[1], pixelArray[2]));
1000: data.setPixel(x, y, pixel);
1001: }
1002: }
1003: return data;
1004: } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
1005: IndexColorModel colorModel = (IndexColorModel) bufferedImage
1006: .getColorModel();
1007: int size = colorModel.getMapSize();
1008: byte[] reds = new byte[size];
1009: byte[] greens = new byte[size];
1010: byte[] blues = new byte[size];
1011: colorModel.getReds(reds);
1012: colorModel.getGreens(greens);
1013: colorModel.getBlues(blues);
1014: RGB[] rgbs = new RGB[size];
1015: for (int i = 0; i < rgbs.length; i++) {
1016: rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF,
1017: blues[i] & 0xFF);
1018: }
1019: PaletteData palette = new PaletteData(rgbs);
1020: ImageData data = new ImageData(bufferedImage.getWidth(),
1021: bufferedImage.getHeight(), colorModel
1022: .getPixelSize(), palette);
1023: data.transparentPixel = colorModel.getTransparentPixel();
1024: WritableRaster raster = bufferedImage.getRaster();
1025: int[] pixelArray = new int[1];
1026: for (int y = 0; y < data.height; y++) {
1027: for (int x = 0; x < data.width; x++) {
1028: raster.getPixel(x, y, pixelArray);
1029: data.setPixel(x, y, pixelArray[0]);
1030: }
1031: }
1032: return data;
1033: }
1034: return null;
1035: }
1036: }
|