KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > SWTGraphics2DRenderer


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: SWTGraphics2DRenderer.java,v $
11    Revision 1.10 2004/05/05 13:24:30 bobintetley
12    Bugfixes and Laurent's patch for binary compatibility on Container.add()
13
14    Revision 1.9 2004/04/30 23:18:24 dannaab
15    List selection support, misc bug fixes
16
17    Revision 1.8 2004/04/20 14:14:19 bobintetley
18    Fix to drawImage() in SWTGraphics2DRenderer to scale images correctly
19
20    Revision 1.7 2004/03/30 10:42:44 bobintetley
21    Many minor bug fixes, event improvements by Dan Naab. Full swing.Icon support
22
23    Revision 1.6 2004/03/21 17:22:53 bobintetley
24    Compatibility methods for awt Graphics, List and Label. Dummy Applet implementation
25
26    Revision 1.5 2004/02/26 13:53:56 bobintetley
27    SWT GC can be grabbed externally now
28
29    Revision 1.4 2004/01/26 10:37:09 bobintetley
30    Better painting support
31
32    Revision 1.3 2004/01/26 09:09:54 bobintetley
33    JComboBox event fixes, fix to Java2D garbage collection and ComboBox demo
34
35    Revision 1.2 2004/01/15 15:58:41 bobintetley
36    Extra thread safety
37
38    Revision 1.1 2004/01/15 15:20:29 bobintetley
39    Java2D work
40
41
42 */

43
44 package swingwt.awt;
45
46 import org.eclipse.swt.graphics.GC;
47 import swingwt.awt.font.FontRenderContext;
48 import swingwt.awt.font.GlyphVector;
49 import swingwt.awt.geom.AffineTransform;
50
51 import java.util.Map JavaDoc;
52
53 /**
54  * This class is the final point for the mapped AWT graphics stuff - it
55  * provides the rendering capabilities to the Graphics/Graphics2D classes
56  * via mappings to SWT GC calls.
57  *
58  * @author Robin Rawson-Tetley
59  */

60 public class SWTGraphics2DRenderer extends Graphics2D {
61     
62     /** The SWT graphics context for painting */
63     protected GC gc = null;
64     /** The current font for the GC */
65     protected swingwt.awt.Font f = null;
66     /** The current colour for the GC */
67     protected swingwt.awt.Color c = null;
68     /** Whether this object should use a finalizer based destructor. This is
69      * set to false generally by Paint Events, as they will ultimately handle
70      * the GC's destruction.
71      */

72     protected boolean shouldDestroy = false;
73     
74     public SWTGraphics2DRenderer(final GC gcon) {
75         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
76             public void run() {
77                 gc = gcon;
78                 
79                 // Note - we don't to clean these up, since the GC owns and
80
// created them.
81
f = new swingwt.awt.Font(gc.getFont());
82                 c = new swingwt.awt.Color(gc.getForeground());
83             }
84         });
85     }
86     
87     /**
88      * @param needToCleanUp Set to true if you want a finalize destructor
89      */

90     public SWTGraphics2DRenderer(GC gc, boolean needToCleanUp) {
91         this(gc);
92         shouldDestroy = needToCleanUp;
93     }
94     
95     public Graphics create() {
96         //return new SWTGraphics2DRenderer(gc.clone(), true);
97
return this;
98     }
99
100     public void dispose() {
101         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
102             public void run() {
103                 gc.dispose();
104             }
105         });
106     }
107     
108     /** Returns the SWT GC used for direct manipulation */
109     public GC getSWTGC() { return gc; }
110     
111     public void clearRect(final int x, final int y, final int width, final int height) {
112         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
113             public void run() {
114                 gc.fillRectangle(x, y, width, height);
115             }
116         });
117     }
118     /** NOT IMPLEMENTED */
119     public void clipRect(int x, int y, int width, int height) { }
120           
121     public void copyArea(final int x, final int y, final int width, final int height, final int dx, final int dy) {
122         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
123             public void run() {
124                 gc.copyArea(x, y, width, height, dx, dy);
125             }
126         });
127     }
128
129     public void redrawUnder(final int x, final int y, final int width, final int height) {
130         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
131             public void run() {
132                 org.eclipse.swt.graphics.Image image = new org.eclipse.swt.graphics.Image(
133                         swingwtx.swing.SwingWTUtils.getDisplay(), width, height);
134                 gc.copyArea(image, x, y);
135                 gc.drawImage(image, x, y);
136             }
137         });
138     }
139
140     public void drawArc(final int x, final int y, final int width, final int height, final int startAngle,final int arcAngle) {
141         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
142             public void run() {
143                 gc.drawArc(x, y, width, height, startAngle, arcAngle);
144             }
145         });
146     }
147     
148     public void drawBytes(final byte[] data, final int offset, final int length, final int x, final int y) {
149         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
150             public void run() {
151                 gc.drawString(new String JavaDoc(data), x, y, true);
152             }
153         });
154     }
155     public void drawChars(final char[] data, final int offset, final int length, final int x, final int y) {
156         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
157             public void run() {
158                 gc.drawString(new String JavaDoc(data), x, y, true);
159             }
160         });
161     }
162     
163     public boolean drawImage(final swingwt.awt.Image img, final int x, final int y, final Color bgcolor, final ImageObserver observer) {
164         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
165             public void run() {
166                 gc.drawImage(img.image, x, y);
167             }
168         });
169         return true;
170     }
171     public boolean drawImage(final swingwt.awt.Image img, final int x, final int y, final ImageObserver observer) {
172         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
173             public void run() {
174                 gc.drawImage(img.image, x, y);
175             }
176         });
177         return true;
178     }
179     public boolean drawImage(final swingwt.awt.Image img, final int x, final int y, final int width, final int height, final Color bgcolor, final ImageObserver observer) {
180         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
181             public void run() {
182                 gc.drawImage(img.image, x, y);
183             }
184         });
185         return true;
186     }
187     public boolean drawImage(final swingwt.awt.Image img, final int x, final int y, final int width, final int height, final ImageObserver observer) {
188         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
189             public void run() {
190                 gc.drawImage(img.image, 0, 0, img.getWidth(null), img.getHeight(null), x, y, width, height);
191             }
192         });
193         return true;
194     }
195     public boolean drawImage(final swingwt.awt.Image img, final int dx1, final int dy1, final int dx2, final int dy2, final int sx1, final int sy1, final int sx2, final int sy2, final Color bgcolor,final ImageObserver observer) {
196         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
197             public void run() {
198                 gc.drawImage(img.image, sx1, sy1, sx2 - sx1, sy2 - sy1, dx1, dy1, dx2 - dx1, dy2 - dy1);
199             }
200         });
201         return true;
202     }
203     public boolean drawImage(final swingwt.awt.Image img, final int dx1, final int dy1, final int dx2, final int dy2, final int sx1, final int sy1, final int sx2, final int sy2, ImageObserver observer) {
204         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
205             public void run() {
206                 gc.drawImage(img.image, sx1, sy1, sx2 - sx1, sy2 - sy1, dx1, dy1, dx2 - dx1, dy2 - dy1);
207             }
208         });
209         return true;
210     }
211
212     public void drawLine(final int x1, final int y1, final int x2, final int y2) {
213         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
214             public void run() {
215                 gc.drawLine(x1, y1, x2, y2);
216             }
217         });
218     }
219           
220     public void drawOval(final int x, final int y, final int width, final int height) {
221         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
222             public void run() {
223                 gc.drawOval(x, y, width, height);
224             }
225         });
226     }
227         
228     /** NOT IMPLEMENTED */
229     public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
230             
231     }
232
233     /** NOT IMPLEMENTED */
234     public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
235         
236     }
237     
238     public void drawString(final String JavaDoc str, final int x, final int y) {
239         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
240             public void run() {
241                 gc.drawString(str, x, y, false);
242             }
243         });
244     }
245     
246     public void drawString(final String JavaDoc str, final int x, final int y, final boolean transparent) {
247         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
248             public void run() {
249                 gc.drawString(str, x, y, transparent);
250             }
251         });
252     }
253     
254     public void fillArc(final int x, final int y, final int width, final int height, final int startAngle, final int arcAngle) {
255         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
256             public void run() {
257                 gc.setBackground(c.getSWTColor());
258                 gc.fillArc(x, y, width, height, startAngle, arcAngle);
259             }
260         });
261     }
262           
263     public void fillOval(final int x, final int y, final int width, final int height) {
264         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
265             public void run() {
266                 gc.setBackground(c.getSWTColor());
267                 gc.fillOval(x, y, width, height);
268             }
269         });
270     }
271     
272     /** NOT IMPLEMENTED */
273     public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
274     }
275           
276     public void fillRect(final int x, final int y, final int width, final int height) {
277         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
278             public void run() {
279                 gc.setBackground(c.getSWTColor());
280                 gc.fillRectangle(x, y, width, height);
281             }
282         });
283     }
284     
285     /** Calls <code>fillRect</code> instead */
286     public void fillRoundRect(final int x, final int y, final int width, final int height, final int arcWidth, final int arcHeight) {
287         fillRect(x, y, width, height);
288     }
289     
290     public Font getFont() {
291         return f;
292     }
293     
294     public Color getColor() {
295         return c;
296     }
297     
298     public void setFont(swingwt.awt.Font f) {
299         if (f != this.f) {
300             this.f = f;
301             swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
302                 public void run() {
303                     gc.setFont(SWTGraphics2DRenderer.this.f.getSWTFont());
304                 }
305             });
306         }
307     }
308     
309     public void setColor(swingwt.awt.Color color) {
310         this.c = color;
311         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
312             public void run() {
313                 gc.setForeground(c.getSWTColor());
314             }
315         });
316     }
317     
318     protected void finalize() throws Throwable JavaDoc {
319         if (shouldDestroy) {
320             swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
321                 public void run() {
322                     gc.dispose();
323                 }
324             });
325         }
326     }
327     
328     /** NOT IMPLEMENTED */
329     public void addRenderingHints(Map JavaDoc hints) {
330     }
331     
332     /** NOT IMPLEMENTED */
333     public void clip(Shape s) {
334     }
335     
336     /** NOT IMPLEMENTED */
337     public void draw(Shape s) {
338     }
339     
340     /** NOT IMPLEMENTED */
341     public void drawGlyphVector(GlyphVector g, float x, float y) {
342     }
343     
344     /** NOT IMPLEMENTED */
345     public void fill(Shape s) {
346     }
347     
348     public Color getBackground() {
349         return new swingwt.awt.Color(gc.getBackground());
350     }
351     
352     /** NOT IMPLEMENTED */
353     public Composite getComposite() {
354         return null;
355     }
356     
357     /** NOT IMPLEMENTED */
358     public GraphicsConfiguration getDeviceConfiguration() {
359         return null;
360     }
361     
362     /** NOT IMPLEMENTED */
363     public FontRenderContext getFontRenderContext() {
364         return null;
365     }
366     
367     /** NOT IMPLEMENTED */
368     public Paint getPaint() {
369         return null;
370     }
371     
372     /** NOT IMPLEMENTED */
373     public Object JavaDoc getRenderingHint(Object JavaDoc hintKey) {
374         return null;
375     }
376     
377     /** NOT IMPLEMENTED */
378     public RenderingHints getRenderingHints() {
379         return null;
380     }
381     
382     /** NOT IMPLEMENTED */
383     public Stroke getStroke() {
384         return null;
385     }
386     
387     /** NOT IMPLEMENTED */
388     public AffineTransform getTransform() {
389         return null;
390     }
391     
392     /** NOT IMPLEMENTED */
393     public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
394         return false;
395     }
396     
397     /** NOT IMPLEMENTED */
398     public void rotate(double theta) {
399     }
400     
401     /** NOT IMPLEMENTED */
402     public void rotate(double theta, double x, double y) {
403     }
404     
405     /** NOT IMPLEMENTED */
406     public void scale(double sx, double sy) {
407     }
408     
409     public void setBackground(final swingwt.awt.Color color) {
410         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
411             public void run() {
412                 gc.setBackground(color.getSWTColor());
413             }
414         });
415     }
416     
417     /** NOT IMPLEMENTED */
418     public void setComposite(Composite comp) {
419     }
420     
421     /** NOT IMPLEMENTED */
422     public void setPaint(Paint paint) {
423     }
424     
425     /** NOT IMPLEMENTED */
426     public void setRenderingHint(Object JavaDoc hintKey, Object JavaDoc hintValue) {
427     }
428     
429     /** NOT IMPLEMENTED */
430     public void setRenderingHints(Map JavaDoc hints) {
431     }
432     
433     /** NOT IMPLEMENTED */
434     public void setStroke(Stroke s) {
435     }
436     
437     /** NOT IMPLEMENTED */
438     public void setTransform(AffineTransform Tx) {
439     }
440     
441     /** NOT IMPLEMENTED */
442     public void shear(double shx, double shy) {
443     }
444     
445     /** NOT IMPLEMENTED */
446     public void transform(AffineTransform Tx) {
447     }
448     
449     /** NOT IMPLEMENTED */
450     public void translate(double tx, double ty) {
451     }
452     
453     /** NOT IMPLEMENTED */
454     public void translate(int x, int y) {
455     }
456     
457     /** NOT IMPLEMENTED */
458     public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
459     }
460     
461     /** NOT IMPLEMENTED */
462     public Shape getClip() {
463         return null;
464     }
465     
466     public Rectangle getClipBounds() {
467         final swingwt.awt.Rectangle r = new swingwt.awt.Rectangle();
468         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
469             public void run() {
470                 org.eclipse.swt.graphics.Rectangle rc = gc.getClipping();
471                 r.height = rc.height;
472                 r.width = rc.width;
473                 r.x = rc.x;
474                 r.y = rc.y;
475             }
476         });
477         return r;
478     }
479     
480     public FontMetrics getFontMetrics(Font f) {
481         return new swingwt.awt.FontMetrics(f);
482     }
483     
484     /** NOT IMPLEMENTED */
485     public void setClip(Shape clip) {
486     }
487     
488     public void setClip(final int x, final int y, final int width, final int height) {
489         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
490             public void run() {
491                 gc.setClipping(x, y, width, height);
492             }
493         });
494     }
495     
496     /** NOT IMPLEMENTED */
497     public void setPaintMode() {
498     }
499     
500     /** NOT IMPLEMENTED */
501     public void setXORMode(Color c1) {
502     }
503     
504 }
505
Popular Tags