001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.ui.graphics;
018:
019: import java.awt.BasicStroke;
020: import java.awt.Color;
021: import java.awt.Graphics2D;
022: import java.awt.Image;
023: import java.awt.Point;
024: import java.awt.Rectangle;
025: import java.awt.Shape;
026: import java.awt.geom.AffineTransform;
027: import java.awt.geom.Rectangle2D;
028: import java.awt.image.BufferedImage;
029: import java.awt.image.RenderedImage;
030:
031: import org.eclipse.swt.graphics.ImageData;
032: import org.eclipse.swt.graphics.PaletteData;
033: import org.eclipse.swt.graphics.Path;
034:
035: public class AWTGraphics implements ViewportGraphics {
036:
037: public Graphics2D g;
038:
039: public AWTGraphics(Graphics2D g) {
040: this .g = g;
041: g.setBackground(Color.WHITE);
042: }
043:
044: /**
045: * @see net.refractions.udig.ui.graphics.ViewportGraphics#draw(java.awt.Shape)
046: */
047: public void draw(Shape s) {
048: g.draw(s);
049: }
050:
051: /**
052: * @see net.refractions.udig.ui.graphics.ViewportGraphics#draw(java.awt.Shape)
053: */
054: public void fill(Shape s) {
055: g.fill(s);
056: }
057:
058: /**
059: * @see net.refractions.udig.ui.graphics.ViewportGraphics#setColor(java.awt.Color)
060: */
061: public void setColor(Color c) {
062: g.setColor(c);
063: }
064:
065: /**
066: * @see net.refractions.udig.ui.graphics.ViewportGraphics#setBackground(java.awt.Color)
067: */
068: public void setBackground(Color c) {
069: g.setBackground(c);
070: }
071:
072: /**
073: * @see net.refractions.udig.ui.graphics.ViewportGraphics#setStroke(int, int)
074: */
075: public void setStroke(int style, int width) {
076: switch (style) {
077: case LINE_DASH: {
078: g.setStroke(new BasicStroke(width, BasicStroke.CAP_SQUARE,
079: BasicStroke.JOIN_MITER, 10.0f, new float[] {
080: width * 2.0f, width * 2.0f }, 0.0f));
081: break;
082: }
083: case LINE_DASHDOT: {
084: g.setStroke(new BasicStroke(width, BasicStroke.CAP_SQUARE,
085: BasicStroke.JOIN_MITER, 10.0f, new float[] {
086: width * 2.0f, width * 2.0f, width * 1.0f,
087: width * 2.0f }, 0.0f));
088: break;
089: }
090: case LINE_DASHDOTDOT: {
091: g.setStroke(new BasicStroke(width, BasicStroke.CAP_SQUARE,
092: BasicStroke.JOIN_MITER, 10.0f, new float[] {
093: width * 2.0f, width * 2.0f, width * 1.0f,
094: width * 2.0f, width * 1.0f, width * 2.0f },
095: 0.0f));
096: break;
097: }
098: case LINE_DOT: {
099: g.setStroke(new BasicStroke(width, BasicStroke.CAP_SQUARE,
100: BasicStroke.JOIN_MITER, 10.0f, new float[] {
101: width * 1.0f, width * 2.0f }, 0.0f));
102: break;
103: }
104: case LINE_SOLID: {
105: g.setStroke(new BasicStroke(width));
106: break;
107: }
108: }
109: }
110:
111: /**
112: * @see net.refractions.udig.ui.graphics.ViewportGraphics#setClip(java.awt.Rectangle)
113: */
114: public void setClip(Rectangle r) {
115: g.setClip(r);
116: }
117:
118: /**
119: * @see net.refractions.udig.ui.graphics.ViewportGraphics#fillRect(int, int, int, int)
120: */
121: public void fillRect(int x, int y, int width, int height) {
122: g.fillRect(x, y, width, height);
123: }
124:
125: /**
126: * @see net.refractions.udig.ui.graphics.ViewportGraphics#translate(java.awt.Point)
127: */
128: public void translate(Point offset) {
129: g.setTransform(AffineTransform.getTranslateInstance(offset.x,
130: offset.y));
131: }
132:
133: /**
134: * @see net.refractions.udig.ui.graphics.ViewportGraphics#clearRect(int, int, int, int)
135: */
136: public void clearRect(int x, int y, int width, int height) {
137: g.clearRect(x, y, width, height);
138: }
139:
140: /**
141: * @see net.refractions.udig.ui.graphics.ViewportGraphics#drawImage(javax.media.jai.PlanarImage, int, int)
142: */
143: public void drawImage(RenderedImage image, int x, int y) {
144: g.drawRenderedImage(image, AffineTransform
145: .getTranslateInstance(x, y));
146: }
147:
148: /**
149: * @see net.refractions.udig.ui.graphics.ViewportGraphics#drawString(String, int, int)
150: */
151: public void drawString(String string, int x, int y, int alignx,
152: int aligny) {
153: Rectangle2D text = g.getFontMetrics()
154: .getStringBounds(string, g);
155: int w = (int) text.getWidth();
156: int h = (int) text.getHeight();
157:
158: int x2 = (alignx == 0) ? x - w / 2 : (alignx > 0) ? x - w : x;
159: int y2 = (aligny == 0) ? y + h / 2 : (aligny > 0) ? y + h : y;
160: g.drawString(string, x2, y2);
161: }
162:
163: /**
164: * @see net.refractions.udig.ui.graphics.ViewportGraphics#setTransform(java.awt.geom.AffineTransform)
165: */
166: public void setTransform(AffineTransform transform) {
167: g.setTransform(transform);
168:
169: }
170:
171: /**
172: * @see net.refractions.udig.ui.graphics.ViewportGraphics#drawImage(java.awt.Image, int, int)
173: */
174: public void drawImage(Image image, int x, int y) {
175: g.drawImage(image, x, y, null);
176: }
177:
178: public void drawImage(Image image, int dx1, int dy1, int dx2,
179: int dy2, int sx1, int sy1, int sx2, int sy2) {
180: g
181: .drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2,
182: sy2, null);
183: }
184:
185: public int getFontHeight() {
186: return g.getFontMetrics().getHeight();
187: }
188:
189: public int stringWidth(String str) {
190: return g.getFontMetrics().stringWidth(str);
191: }
192:
193: public int getFontAscent() {
194: return g.getFontMetrics().getAscent();
195: }
196:
197: public Rectangle2D getStringBounds(String str) {
198: return g.getFontMetrics().getStringBounds(str, g);
199: }
200:
201: /**
202: * Converts an SWT image to an AWT BufferedImage
203: *
204: * @param swtImageData
205: * @return
206: */
207: public static BufferedImage toAwtImage(ImageData swtImageData) {
208: int width = swtImageData.width;
209: int height = swtImageData.height;
210: PaletteData palette = swtImageData.palette;
211: BufferedImage bufferedImage = new BufferedImage(width, height,
212: BufferedImage.TYPE_INT_RGB);
213: for (int y = 0; y < height; y++) {
214: for (int x = 0; x < width; x++) {
215: int rgb = swtImageData.getPixel(x, y);
216:
217: int red = (rgb & palette.redMask) >> Math
218: .abs(palette.redShift);
219: int green = (rgb & palette.greenMask) >> Math
220: .abs(palette.greenShift);
221: int blue = (rgb & palette.blueMask) >>> Math
222: .abs(palette.blueShift);
223:
224: Color color = new Color(red, green, blue);
225: bufferedImage.setRGB(x, y, color.getRGB());
226: }
227: }
228: return bufferedImage;
229: }
230:
231: public void drawLine(int x1, int y1, int x2, int y2) {
232: g.drawLine(x1, y1, x2, y2);
233: }
234:
235: public void drawImage(org.eclipse.swt.graphics.Image image,
236: int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
237: int sx2, int sy2) {
238: BufferedImage awtImage = toAwtImage(image.getImageData());
239: drawImage(awtImage, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
240: }
241:
242: public AffineTransform getTransform() {
243: return g.getTransform();
244: }
245:
246: public void dispose() {
247: g.dispose();
248: }
249:
250: public void drawPath(Path path) {
251: throw new UnsupportedOperationException(
252: "We have not had time to implement this method yet"); //$NON-NLS-1$
253: }
254:
255: public void fillPath(Path path) {
256: throw new UnsupportedOperationException(
257: "We have not had time to implement this method yet"); //$NON-NLS-1$
258: }
259:
260: public void drawRect(int x, int y, int width, int height) {
261: g.drawRect(x, y, width, height);
262: }
263:
264: public void drawOval(int x, int y, int width, int height) {
265: g.drawOval(x, y, width, height);
266: }
267:
268: public void fillOval(int x, int y, int width, int height) {
269: g.fillOval(x, y, width, height);
270: }
271:
272: public void drawImage(org.eclipse.swt.graphics.Image swtImage,
273: int x, int y) {
274: BufferedImage awtImage = toAwtImage(swtImage.getImageData());
275: drawImage((Image) awtImage, x, y);
276:
277: }
278:
279: public Shape getClip() {
280: return g.getClip();
281: }
282:
283: public void setClipBounds(Rectangle newBounds) {
284: g.setClip(newBounds);
285: }
286:
287: public Color getBackgroundColor() {
288: return g.getBackground();
289: }
290:
291: public Color getColor() {
292: return g.getColor();
293: }
294:
295: public void drawRoundRect(int x, int y, int width, int height,
296: int arcWidth, int arcHeight) {
297: g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
298: }
299:
300: public void fillRoundRect(int x, int y, int width, int height,
301: int arcWidth, int arcHeight) {
302: g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
303: }
304: }
|