KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > experimental > swt > SWTGraphics2D


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ------------------
28  * SWTGraphics2D.java
29  * ------------------
30  * (C) Copyright 2006, by Henry Proudhon and Contributors.
31  *
32  * Original Author: Henry Proudhon (henry.proudhon AT insa-lyon.fr);
33  * Contributor(s):
34  *
35  * Changes
36  * -------
37  * 14 Jun 2006 : New class (HP);
38  *
39  */

40
41 package org.jfree.experimental.swt;
42
43 import java.awt.BasicStroke JavaDoc;
44 import java.awt.Color JavaDoc;
45 import java.awt.Composite JavaDoc;
46 import java.awt.Font JavaDoc;
47 import java.awt.FontMetrics JavaDoc;
48 import java.awt.Graphics JavaDoc;
49 import java.awt.Graphics2D JavaDoc;
50 import java.awt.GraphicsConfiguration JavaDoc;
51 import java.awt.Image JavaDoc;
52 import java.awt.Paint JavaDoc;
53 import java.awt.Rectangle JavaDoc;
54 import java.awt.RenderingHints JavaDoc;
55 import java.awt.Shape JavaDoc;
56 import java.awt.Stroke JavaDoc;
57 import java.awt.RenderingHints.Key;
58 import java.awt.font.FontRenderContext JavaDoc;
59 import java.awt.font.GlyphVector JavaDoc;
60 import java.awt.geom.AffineTransform JavaDoc;
61 import java.awt.geom.PathIterator JavaDoc;
62 import java.awt.image.BufferedImage JavaDoc;
63 import java.awt.image.BufferedImageOp JavaDoc;
64 import java.awt.image.DirectColorModel JavaDoc;
65 import java.awt.image.ImageObserver JavaDoc;
66 import java.awt.image.IndexColorModel JavaDoc;
67 import java.awt.image.RenderedImage JavaDoc;
68 import java.awt.image.WritableRaster JavaDoc;
69 import java.awt.image.renderable.RenderableImage JavaDoc;
70 import java.text.AttributedCharacterIterator JavaDoc;
71 import java.util.Map JavaDoc;
72
73 import javax.swing.JPanel JavaDoc;
74
75 import org.eclipse.swt.SWT;
76 import org.eclipse.swt.graphics.FontData;
77 import org.eclipse.swt.graphics.GC;
78 import org.eclipse.swt.graphics.ImageData;
79 import org.eclipse.swt.graphics.PaletteData;
80 import org.eclipse.swt.graphics.Path;
81 import org.eclipse.swt.graphics.RGB;
82 import org.eclipse.swt.graphics.Transform;
83
84 /**
85  * This is a class utility to draw Graphics2D stuff on a swt composite.
86  * It is presently developed to use JFreeChart with the Standard
87  * Widget Toolkit but may be of a wider use later.
88  */

89 public class SWTGraphics2D extends Graphics2D JavaDoc {
90     
91     /** The swt graphic composite */
92     private GC gc;
93     
94     /** A dummy JPanel used to provide font metrics. */
95     private static final JPanel JavaDoc DUMMY_PANEL = new JPanel JavaDoc();
96
97     /**
98      * Creates a new instance.
99      *
100      * @param gc the graphics context.
101      */

102     public SWTGraphics2D(GC gc) {
103         super();
104         this.gc = gc;
105     }
106
107     /**
108      * Perform a switch between foreground and background
109      * color of gc. This is needed for consistency with
110      * the awt behaviour, and is required notably for the
111      * filling methods.
112      */

113     private void switchColors() {
114         org.eclipse.swt.graphics.Color bg = gc.getBackground();
115         org.eclipse.swt.graphics.Color fg = gc.getForeground();
116         gc.setBackground(fg);
117         gc.setForeground(bg);
118     }
119     
120     /**
121      * Converts an AWT <code>Shape</code> into a SWT <code>Path</code>.
122      *
123      * @param shape the shape.
124      *
125      * @return The path.
126      */

127     private Path toSwtPath(Shape JavaDoc shape) {
128         int type;
129         float[] coords = new float[6];
130         Path path = new Path(this.gc.getDevice());
131         PathIterator JavaDoc pit = shape.getPathIterator(null);
132         while (!pit.isDone()) {
133             type = pit.currentSegment(coords);
134             switch (type) {
135                 case (PathIterator.SEG_MOVETO):
136                     path.moveTo(coords[0], coords[1]);
137                     break;
138                 case (PathIterator.SEG_LINETO):
139                     path.lineTo(coords[0], coords[1]);
140                     break;
141                 case (PathIterator.SEG_QUADTO):
142                     path.quadTo(coords[0], coords[1], coords[2], coords[3]);
143                     break;
144                 case (PathIterator.SEG_CUBICTO):
145                     path.cubicTo(coords[0], coords[1], coords[2],
146                             coords[3], coords[4], coords[5]);
147                     break;
148                 case (PathIterator.SEG_CLOSE):
149                     path.close();
150                     break;
151                 default:
152                     break;
153             }
154             pit.next();
155         }
156         return path;
157     }
158     
159     /**
160      * Converts an AWT transform into the equivalent SWT transform.
161      *
162      * @param awtTransform the AWT transform.
163      *
164      * @return The SWT transform.
165      */

166     private Transform JavaDoc toSwtTransform(AffineTransform JavaDoc awtTransform) {
167         Transform JavaDoc t = new Transform JavaDoc(gc.getDevice());
168         double[] matrix = new double[6];
169         awtTransform.getMatrix(matrix);
170         t.setElements((float) matrix[0], (float) matrix[1],
171                 (float) matrix[2], (float) matrix[3],
172                 (float) matrix[4], (float) matrix[5]);
173         return t;
174     }
175     
176     /**
177      * Converts an SWT transform into the equivalent AWT transform.
178      *
179      * @param swtTransform the SWT transform.
180      *
181      * @return The AWT transform.
182      */

183     private AffineTransform JavaDoc toAwtTransform(Transform JavaDoc swtTransform) {
184         float[] elements = new float[6];
185         swtTransform.getElements(elements);
186         AffineTransform JavaDoc awtTransform = new AffineTransform JavaDoc(elements);
187         return awtTransform;
188     }
189     
190     /* (non-Javadoc)
191      * @see java.awt.Graphics2D#draw(java.awt.Shape)
192      */

193     public void draw(Shape JavaDoc shape) {
194         Path path = toSwtPath(shape);
195         gc.drawPath(path);
196         path.dispose();
197     }
198
199     /* (non-Javadoc)
200      * @see java.awt.Graphics2D#drawImage(java.awt.Image,
201      * java.awt.geom.AffineTransform, java.awt.image.ImageObserver)
202      */

203     public boolean drawImage(Image JavaDoc image, AffineTransform JavaDoc xform,
204             ImageObserver JavaDoc obs) {
205         // TODO Auto-generated method stub
206
return false;
207     }
208
209     /* (non-Javadoc)
210      * @see java.awt.Graphics2D#drawImage(java.awt.image.BufferedImage,
211      * java.awt.image.BufferedImageOp, int, int)
212      */

213     public void drawImage(BufferedImage JavaDoc image, BufferedImageOp JavaDoc op, int x,
214             int y) {
215         org.eclipse.swt.graphics.Image im
216             = new org.eclipse.swt.graphics.Image(gc.getDevice(),
217                     convertToSWT(image));
218         gc.drawImage(im, x, y);
219         im.dispose();
220     }
221
222     /**
223      * Draws an image at (x, y).
224      *
225      * @param image the image.
226      * @param x the x-coordinate.
227      * @param y the y-coordinate.
228      */

229     public void drawImage(org.eclipse.swt.graphics.Image image, int x, int y) {
230         gc.drawImage(image, x, y);
231     }
232
233     /* (non-Javadoc)
234      * @see java.awt.Graphics2D#drawRenderedImage(java.awt.image.RenderedImage,
235      * java.awt.geom.AffineTransform)
236      */

237     public void drawRenderedImage(RenderedImage JavaDoc image, AffineTransform JavaDoc xform) {
238         // TODO Auto-generated method stub
239
}
240
241     /* (non-Javadoc)
242      * @see java.awt.Graphics2D#drawRenderableImage(
243      * java.awt.image.renderable.RenderableImage, java.awt.geom.AffineTransform)
244      */

245     public void drawRenderableImage(RenderableImage JavaDoc image,
246             AffineTransform JavaDoc xform) {
247         // TODO Auto-generated method stub
248

249     }
250
251     /**
252      * Draws a string on the receiver. note that
253      * to be consistent with the awt method,
254      * the y has to be modified with the ascent of the font.
255      *
256      * @see java.awt.Graphics#drawString(java.lang.String, int, int)
257      */

258     public void drawString(String JavaDoc text, int x, int y) {
259         float fm = gc.getFontMetrics().getAscent();
260         gc.drawString(text, x, (int) (y - fm), true);
261     }
262
263     /* (non-Javadoc)
264      * @see java.awt.Graphics2D#drawString(java.lang.String, float, float)
265      */

266     public void drawString(String JavaDoc text, float x, float y) {
267         float fm = gc.getFontMetrics().getAscent();
268         gc.drawString(text, (int) x, (int) ( y - fm ), true);
269     }
270
271     /* (non-Javadoc)
272      * @see java.awt.Graphics2D#drawString(
273      * java.text.AttributedCharacterIterator, int, int)
274      */

275     public void drawString(AttributedCharacterIterator JavaDoc iterator, int x, int y) {
276         // TODO Auto-generated method stub
277

278     }
279
280     /* (non-Javadoc)
281      * @see java.awt.Graphics2D#drawString(
282      * java.text.AttributedCharacterIterator, float, float)
283      */

284     public void drawString(AttributedCharacterIterator JavaDoc iterator, float x,
285             float y) {
286         // TODO Auto-generated method stub
287

288     }
289
290     /** fill an arbitrary shape on the swt graphic composite
291      * with the current stroke and paint.
292      * note that for consistency with the awt method, it is needed
293      * to switch temporarily the foreground and background colors.
294      * @see java.awt.Graphics2D#fill(java.awt.Shape)
295      */

296     public void fill(Shape JavaDoc shape) {
297         Path path = toSwtPath(shape);
298         switchColors();
299         this.gc.fillPath(path);
300         switchColors();
301         path.dispose();
302     }
303
304     /* (non-Javadoc)
305      * @see java.awt.Graphics2D#hit(java.awt.Rectangle, java.awt.Shape, boolean)
306      */

307     public boolean hit(Rectangle JavaDoc rect, Shape JavaDoc text, boolean onStroke) {
308         // TODO Auto-generated method stub
309
return false;
310     }
311
312     /* (non-Javadoc)
313      * @see java.awt.Graphics2D#getDeviceConfiguration()
314      */

315     public GraphicsConfiguration JavaDoc getDeviceConfiguration() {
316         // TODO Auto-generated method stub
317
return null;
318     }
319
320     /* (non-Javadoc)
321      * @see java.awt.Graphics2D#setComposite(java.awt.Composite)
322      */

323     public void setComposite(Composite JavaDoc comp) {
324         // TODO Auto-generated method stub
325

326     }
327
328     /**
329      * Set the paint associated with the swt graphic composite.
330      * @see java.awt.Graphics2D#setPaint(java.awt.Paint)
331      */

332     public void setPaint(Paint JavaDoc paint) {
333         if (paint instanceof Color JavaDoc) {
334             setColor((Color JavaDoc) paint);
335         }
336         else {
337             throw new RuntimeException JavaDoc("Can only handle 'Color' at present.");
338         }
339     }
340
341     /* (non-Javadoc)
342      * @see java.awt.Graphics2D#setStroke(java.awt.Stroke)
343      */

344     public void setStroke(Stroke JavaDoc stroke) {
345         if (stroke instanceof BasicStroke JavaDoc) {
346             BasicStroke JavaDoc bs = (BasicStroke JavaDoc) stroke;
347             // linewidth
348
gc.setLineWidth((int) bs.getLineWidth());
349
350             // line join
351
switch (bs.getLineJoin()) {
352                 case BasicStroke.JOIN_BEVEL :
353                     gc.setLineJoin(SWT.JOIN_BEVEL);
354                     break;
355                 case BasicStroke.JOIN_MITER :
356                     gc.setLineJoin(SWT.JOIN_MITER);
357                     break;
358                 case BasicStroke.JOIN_ROUND :
359                     gc.setLineJoin(SWT.JOIN_ROUND);
360                     break;
361             }
362
363             // line cap
364
switch (bs.getEndCap()) {
365                 case BasicStroke.CAP_BUTT :
366                     gc.setLineCap(SWT.CAP_FLAT);
367                     break;
368                 case BasicStroke.CAP_ROUND :
369                     gc.setLineCap(SWT.CAP_ROUND);
370                     break;
371                 case BasicStroke.CAP_SQUARE :
372                     gc.setLineCap(SWT.CAP_SQUARE);
373                     break;
374             }
375
376             // set the line style to solid by default
377
gc.setLineStyle(SWT.LINE_SOLID);
378
379             // apply dash style if any
380
float[] dashes = bs.getDashArray();
381             if (dashes != null) {
382                 int[] swtDashes = new int[dashes.length];
383                 for (int i = 0; i < swtDashes.length; i++) {
384                     swtDashes[i] = (int) dashes[i];
385                 }
386                 gc.setLineDash(swtDashes);
387             }
388         }
389         else {
390             throw new RuntimeException JavaDoc(
391                     "Can only handle 'Basic Stroke' at present.");
392         }
393     }
394
395     /* (non-Javadoc)
396      * @see java.awt.Graphics2D#setRenderingHint(java.awt.RenderingHints.Key,
397      * java.lang.Object)
398      */

399     public void setRenderingHint(Key hintKey, Object JavaDoc hintValue) {
400         // TODO Auto-generated method stub
401

402     }
403
404     /* (non-Javadoc)
405      * @see java.awt.Graphics2D#getRenderingHint(java.awt.RenderingHints.Key)
406      */

407     public Object JavaDoc getRenderingHint(Key hintKey) {
408         // TODO Auto-generated method stub
409
return null;
410     }
411
412     /* (non-Javadoc)
413      * @see java.awt.Graphics2D#setRenderingHints(java.util.Map)
414      */

415     public void setRenderingHints(Map JavaDoc hints) {
416         // TODO Auto-generated method stub
417

418     }
419
420     /* (non-Javadoc)
421      * @see java.awt.Graphics2D#addRenderingHints(java.util.Map)
422      */

423     public void addRenderingHints(Map JavaDoc hints) {
424         // TODO Auto-generated method stub
425

426     }
427
428     /* (non-Javadoc)
429      * @see java.awt.Graphics2D#getRenderingHints()
430      */

431     public RenderingHints JavaDoc getRenderingHints() {
432         // TODO Auto-generated method stub
433
return null;
434     }
435
436     /* (non-Javadoc)
437      * @see java.awt.Graphics2D#translate(int, int)
438      */

439     public void translate(int x, int y) {
440         Transform JavaDoc swtTransform = new Transform JavaDoc(gc.getDevice());
441         gc.getTransform(swtTransform);
442         swtTransform.translate(x, y);
443         gc.setTransform(swtTransform);
444         swtTransform.dispose();
445     }
446
447     /* (non-Javadoc)
448      * @see java.awt.Graphics2D#translate(double, double)
449      */

450     public void translate(double tx, double ty) {
451         translate((int) tx, (int) ty);
452     }
453
454     /* (non-Javadoc)
455      * @see java.awt.Graphics2D#rotate(double)
456      */

457     public void rotate(double theta) {
458         Transform JavaDoc swtTransform = new Transform JavaDoc(gc.getDevice());
459         gc.getTransform(swtTransform);
460         swtTransform.rotate( (float) (theta * 180 / Math.PI));
461         gc.setTransform(swtTransform);
462         swtTransform.dispose();
463     }
464
465     /* (non-Javadoc)
466      * @see java.awt.Graphics2D#rotate(double, double, double)
467      */

468     public void rotate(double theta, double x, double y) {
469         // TODO Auto-generated method stub
470

471     }
472
473     /* (non-Javadoc)
474      * @see java.awt.Graphics2D#scale(double, double)
475      */

476     public void scale(double scaleX, double scaleY) {
477         Transform JavaDoc swtTransform = new Transform JavaDoc(gc.getDevice());
478         gc.getTransform(swtTransform);
479         swtTransform.scale((float) scaleX, (float) scaleY);
480         gc.setTransform(swtTransform);
481         swtTransform.dispose();
482     }
483
484     /* (non-Javadoc)
485      * @see java.awt.Graphics2D#shear(double, double)
486      */

487     public void shear(double shearX, double shearY) {
488         Transform JavaDoc swtTransform = new Transform JavaDoc(gc.getDevice());
489         gc.getTransform(swtTransform);
490         Transform JavaDoc shear = new Transform JavaDoc(gc.getDevice(), 1f, (float) shearX,
491                 (float) shearY, 1f, 0, 0);
492         swtTransform.multiply(shear);
493         gc.setTransform(swtTransform);
494         swtTransform.dispose();
495     }
496
497     /* (non-Javadoc)
498      * @see java.awt.Graphics2D#transform(java.awt.geom.AffineTransform)
499      */

500     public void transform(AffineTransform JavaDoc Tx) {
501         Transform JavaDoc swtTransform = new Transform JavaDoc(gc.getDevice());
502         gc.getTransform(swtTransform);
503         swtTransform.multiply(toSwtTransform(Tx));
504         gc.setTransform(swtTransform);
505         swtTransform.dispose();
506     }
507
508     /* (non-Javadoc)
509      * @see java.awt.Graphics2D#setTransform(java.awt.geom.AffineTransform)
510      */

511     public void setTransform(AffineTransform JavaDoc Tx) {
512         gc.setTransform(toSwtTransform(Tx));
513     }
514
515     /* (non-Javadoc)
516      * @see java.awt.Graphics2D#getTransform()
517      */

518     public AffineTransform JavaDoc getTransform() {
519         Transform JavaDoc swtTransform = new Transform JavaDoc(gc.getDevice());
520         gc.getTransform(swtTransform);
521         return toAwtTransform(swtTransform);
522     }
523
524     /* (non-Javadoc)
525      * @see java.awt.Graphics2D#getPaint()
526      */

527     public Paint JavaDoc getPaint() {
528         return SWTUtils.toAwtColor(gc.getForeground());
529     }
530
531     /* (non-Javadoc)
532      * @see java.awt.Graphics2D#getComposite()
533      */

534     public Composite JavaDoc getComposite() {
535         // TODO Auto-generated method stub
536
return null;
537     }
538
539     /* (non-Javadoc)
540      * @see java.awt.Graphics2D#setBackground(java.awt.Color)
541      */

542     public void setBackground(Color JavaDoc color) {
543         //TODO do we need this? gc.getBackground().dispose();
544
gc.setBackground(SWTUtils.toSwtColor(gc.getDevice(), color));
545     }
546
547     /* (non-Javadoc)
548      * @see java.awt.Graphics2D#getBackground()
549      */

550     public Color JavaDoc getBackground() {
551         return SWTUtils.toAwtColor(gc.getBackground());
552     }
553
554     /* (non-Javadoc)
555      * @see java.awt.Graphics2D#getStroke()
556      */

557     public Stroke JavaDoc getStroke() {
558         return new BasicStroke JavaDoc(gc.getLineWidth(), gc.getLineCap(),
559                 gc.getLineJoin());
560     }
561
562     /* (non-Javadoc)
563      * @see java.awt.Graphics2D#clip(java.awt.Shape)
564      */

565     public void clip(Shape JavaDoc s) {
566         Path path = toSwtPath(s);
567         gc.setClipping(path);
568         path.dispose();
569     }
570
571     /* (non-Javadoc)
572      * @see java.awt.Graphics2D#getFontRenderContext()
573      */

574     public FontRenderContext JavaDoc getFontRenderContext() {
575         FontRenderContext JavaDoc fontRenderContext
576             = new FontRenderContext JavaDoc(new AffineTransform JavaDoc(), true, true);
577         return fontRenderContext;
578     }
579
580     /* (non-Javadoc)
581      * @see java.awt.Graphics2D#drawGlyphVector(java.awt.font.GlyphVector,
582      * float, float)
583      */

584     public void drawGlyphVector(GlyphVector JavaDoc g, float x, float y) {
585         // TODO Auto-generated method stub
586

587     }
588
589     /* (non-Javadoc)
590      * @see java.awt.Graphics#create()
591      */

592     public Graphics JavaDoc create() {
593         // TODO Auto-generated method stub
594
return null;
595     }
596
597     /* (non-Javadoc)
598      * @see java.awt.Graphics#getColor()
599      */

600     public Color JavaDoc getColor() {
601         return SWTUtils.toAwtColor(gc.getForeground());
602     }
603
604     /* (non-Javadoc)
605      * @see java.awt.Graphics#setColor(java.awt.Color)
606      */

607     public void setColor(Color JavaDoc color) {
608         //TODO do we need this? gc.getForeground().dispose();
609
gc.setForeground(SWTUtils.toSwtColor( gc.getDevice(), color));
610     }
611
612     /* (non-Javadoc)
613      * @see java.awt.Graphics#setPaintMode()
614      */

615     public void setPaintMode() {
616         // TODO Auto-generated method stub
617
}
618
619     /* (non-Javadoc)
620      * @see java.awt.Graphics#setXORMode(java.awt.Color)
621      */

622     public void setXORMode(Color JavaDoc color) {
623         // TODO Auto-generated method stub
624

625     }
626
627     /**
628      * Returns the font in form of an awt font created
629      * with the parameters of the font of the swt graphic
630      * composite.
631      * @see java.awt.Graphics#getFont()
632      */

633     public Font JavaDoc getFont() {
634         // retrieve the swt font description in an os indept way
635
FontData[] fontData = gc.getFont().getFontData();
636         // create a new awt font with the appropiate data
637
return SWTUtils.toAwtFont(gc.getDevice(), fontData[0], true);
638     }
639
640     /**
641      * Set the font swt graphic composite from the specified
642      * awt font. Be careful that the newly created swt font
643      * must be disposed separately.
644      * @see java.awt.Graphics#setFont(java.awt.Font)
645      */

646     public void setFont(Font JavaDoc font) {
647         //TODO do we need this? gc.getFont().dispose();
648
org.eclipse.swt.graphics.Font swtFont
649             = new org.eclipse.swt.graphics.Font(
650                     gc.getDevice(),
651                     SWTUtils.toSwtFontData(gc.getDevice(), font, true));
652         gc.setFont( swtFont );
653     }
654
655     /* (non-Javadoc)
656      * @see java.awt.Graphics#getFontMetrics(java.awt.Font)
657      */

658     public FontMetrics JavaDoc getFontMetrics(Font JavaDoc font) {
659         return DUMMY_PANEL.getFontMetrics(font);
660     }
661
662     /* (non-Javadoc)
663      * @see java.awt.Graphics#getClipBounds()
664      */

665     public Rectangle JavaDoc getClipBounds() {
666         org.eclipse.swt.graphics.Rectangle clip = gc.getClipping();
667         return new Rectangle JavaDoc(clip.x, clip.y, clip.width, clip.height);
668     }
669
670     /* (non-Javadoc)
671      * @see java.awt.Graphics#clipRect(int, int, int, int)
672      */

673     public void clipRect(int x, int y, int width, int height) {
674         org.eclipse.swt.graphics.Rectangle clip = gc.getClipping();
675         clip.intersects(x, y, width, height);
676         gc.setClipping(clip);
677     }
678
679     /* (non-Javadoc)
680      * @see java.awt.Graphics#setClip(int, int, int, int)
681      */

682     public void setClip(int x, int y, int width, int height) {
683         gc.setClipping(x, y, width, height);
684     }
685
686     /* (non-Javadoc)
687      * @see java.awt.Graphics#getClip()
688      */

689     public Shape JavaDoc getClip() {
690         // TODO Auto-generated method stub
691
return null;
692     }
693
694     /* (non-Javadoc)
695      * @see java.awt.Graphics#setClip(java.awt.Shape)
696      */

697     public void setClip(Shape JavaDoc clip) {
698         if (clip == null)
699             return;
700         Path clipPath = toSwtPath(clip);
701         gc.setClipping(clipPath);
702         clipPath.dispose();
703     }
704
705     /* (non-Javadoc)
706      * @see java.awt.Graphics#copyArea(int, int, int, int, int, int)
707      */

708     public void copyArea(int x, int y, int width, int height, int dx, int dy) {
709         // TODO Auto-generated method stub
710

711     }
712
713     /**
714      * Draws a line on the swt graphic composite.
715      * @see java.awt.Graphics#drawLine(int, int, int, int)
716      */

717     public void drawLine(int x1, int y1, int x2, int y2) {
718         gc.drawLine(x1, y1, x2, y2);
719     }
720
721     /**
722      * Fill a rectangle area on the swt graphic composite.
723      * @see java.awt.Graphics#fillRect(int, int, int, int)
724      */

725     public void fillRect(int x, int y, int width, int height) {
726         gc.fillRectangle(x, y, width, height);
727     }
728
729     /* (non-Javadoc)
730      * @see java.awt.Graphics#clearRect(int, int, int, int)
731      */

732     public void clearRect(int x, int y, int width, int height) {
733         // TODO Auto-generated method stub
734

735     }
736
737     /* (non-Javadoc)
738      * @see java.awt.Graphics#drawRoundRect(int, int, int, int, int, int)
739      */

740     public void drawRoundRect(int x, int y, int width, int height,
741             int arcWidth, int arcHeight) {
742         // TODO Auto-generated method stub
743

744     }
745
746     /* (non-Javadoc)
747      * @see java.awt.Graphics#fillRoundRect(int, int, int, int, int, int)
748      */

749     public void fillRoundRect(int x, int y, int width, int height,
750             int arcWidth, int arcHeight) {
751         // TODO Auto-generated method stub
752

753     }
754
755     /* (non-Javadoc)
756      * @see java.awt.Graphics#drawOval(int, int, int, int)
757      */

758     public void drawOval(int x, int y, int width, int height) {
759         // TODO Auto-generated method stub
760

761     }
762
763     /* (non-Javadoc)
764      * @see java.awt.Graphics#fillOval(int, int, int, int)
765      */

766     public void fillOval(int x, int y, int width, int height) {
767         // TODO Auto-generated method stub
768

769     }
770
771     /* (non-Javadoc)
772      * @see java.awt.Graphics#drawArc(int, int, int, int, int, int)
773      */

774     public void drawArc(int x, int y, int width, int height, int arcStart,
775             int arcAngle) {
776         // TODO Auto-generated method stub
777

778     }
779
780     /* (non-Javadoc)
781      * @see java.awt.Graphics#fillArc(int, int, int, int, int, int)
782      */

783     public void fillArc(int x, int y, int width, int height, int arcStart,
784             int arcAngle) {
785         // TODO Auto-generated method stub
786

787     }
788
789     /* (non-Javadoc)
790      * @see java.awt.Graphics#drawPolyline(int[], int[], int)
791      */

792     public void drawPolyline(int [] xPoints, int [] yPoints, int npoints) {
793         // TODO Auto-generated method stub
794

795     }
796
797     /* (non-Javadoc)
798      * @see java.awt.Graphics#drawPolygon(int[], int[], int)
799      */

800     public void drawPolygon(int [] xPoints, int [] yPoints, int npoints) {
801         // TODO Auto-generated method stub
802

803     }
804
805     /* (non-Javadoc)
806      * @see java.awt.Graphics#fillPolygon(int[], int[], int)
807      */

808     public void fillPolygon(int [] xPoints, int [] yPoints, int npoints) {
809         // TODO Auto-generated method stub
810

811     }
812
813     /* (non-Javadoc)
814      * @see java.awt.Graphics#drawImage(java.awt.Image, int, int,
815      * java.awt.image.ImageObserver)
816      */

817     public boolean drawImage(Image JavaDoc image, int x, int y,
818             ImageObserver JavaDoc observer) {
819         // TODO Auto-generated method stub
820
return false;
821     }
822
823     /* (non-Javadoc)
824      * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int,
825      * java.awt.image.ImageObserver)
826      */

827     public boolean drawImage(Image JavaDoc image, int x, int y, int width, int height,
828             ImageObserver JavaDoc observer) {
829         // TODO Auto-generated method stub
830
return false;
831     }
832
833     /* (non-Javadoc)
834      * @see java.awt.Graphics#drawImage(java.awt.Image, int, int,
835      * java.awt.Color, java.awt.image.ImageObserver)
836      */

837     public boolean drawImage(Image JavaDoc image, int x, int y, Color JavaDoc bgcolor,
838             ImageObserver JavaDoc observer) {
839         // TODO Auto-generated method stub
840
return false;
841     }
842
843     /* (non-Javadoc)
844      * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int,
845      * java.awt.Color, java.awt.image.ImageObserver)
846      */

847     public boolean drawImage(Image JavaDoc image, int x, int y, int width, int height,
848             Color JavaDoc bgcolor, ImageObserver JavaDoc observer) {
849         // TODO Auto-generated method stub
850
return false;
851     }
852
853     /* (non-Javadoc)
854      * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int,
855      * int, int, int, int, java.awt.image.ImageObserver)
856      */

857     public boolean drawImage(Image JavaDoc image, int dx1, int dy1, int dx2, int dy2,
858             int sx1, int sy1, int sx2, int sy2, ImageObserver JavaDoc observer) {
859         // TODO Auto-generated method stub
860
return false;
861     }
862
863     /* (non-Javadoc)
864      * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int,
865      * int, int, int, int, java.awt.Color, java.awt.image.ImageObserver)
866      */

867     public boolean drawImage(Image JavaDoc image, int dx1, int dy1, int dx2, int dy2,
868             int sx1, int sy1, int sx2, int sy2, Color JavaDoc bgcolor,
869             ImageObserver JavaDoc observer) {
870         // TODO Auto-generated method stub
871
return false;
872     }
873
874     /* (non-Javadoc)
875      * @see java.awt.Graphics#dispose()
876      */

877     public void dispose() {
878         gc.dispose();
879     }
880
881     static ImageData convertToSWT(BufferedImage JavaDoc bufferedImage) {
882         if (bufferedImage.getColorModel() instanceof DirectColorModel JavaDoc) {
883             DirectColorModel JavaDoc colorModel
884                     = (DirectColorModel JavaDoc) bufferedImage.getColorModel();
885             PaletteData palette = new PaletteData(colorModel.getRedMask(),
886                     colorModel.getGreenMask(), colorModel.getBlueMask());
887             ImageData data = new ImageData(bufferedImage.getWidth(),
888                     bufferedImage.getHeight(), colorModel.getPixelSize(),
889                     palette);
890             WritableRaster JavaDoc raster = bufferedImage.getRaster();
891             int[] pixelArray = new int[3];
892             for (int y = 0; y < data.height; y++) {
893                 for (int x = 0; x < data.width; x++) {
894                   raster.getPixel(x, y, pixelArray);
895                   int pixel = palette.getPixel(new RGB(pixelArray[0],
896                           pixelArray[1], pixelArray[2]));
897                   data.setPixel(x, y, pixel);
898                 }
899             }
900             return data;
901         }
902         else if (bufferedImage.getColorModel() instanceof IndexColorModel JavaDoc) {
903             IndexColorModel JavaDoc colorModel
904                     = (IndexColorModel JavaDoc) bufferedImage.getColorModel();
905             int size = colorModel.getMapSize();
906             byte[] reds = new byte[size];
907             byte[] greens = new byte[size];
908             byte[] blues = new byte[size];
909             colorModel.getReds(reds);
910             colorModel.getGreens(greens);
911             colorModel.getBlues(blues);
912             RGB[] rgbs = new RGB[size];
913             for (int i = 0; i < rgbs.length; i++) {
914                 rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF,
915                         blues[i] & 0xFF);
916             }
917             PaletteData palette = new PaletteData(rgbs);
918             ImageData data = new ImageData(bufferedImage.getWidth(),
919                     bufferedImage.getHeight(), colorModel.getPixelSize(),
920                     palette);
921             data.transparentPixel = colorModel.getTransparentPixel();
922             WritableRaster JavaDoc raster = bufferedImage.getRaster();
923             int[] pixelArray = new int[1];
924             for (int y = 0; y < data.height; y++) {
925                 for (int x = 0; x < data.width; x++) {
926                     raster.getPixel(x, y, pixelArray);
927                     data.setPixel(x, y, pixelArray[0]);
928                 }
929             }
930             return data;
931         }
932         return null;
933     }
934 }
935
Popular Tags