KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > Image


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: Image.java,v $
11    Revision 1.12 2004/05/06 12:35:21 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.11 2004/04/23 00:52:31 dannaab
15    Handle borders in a Swing-like way. Implement EmptyBorder & TitledBorder
16
17    Revision 1.10 2004/03/01 12:25:46 bobintetley
18    Better HTML conversion, custom JFileChooser support, JLabel, Window and
19    Image fixes to improve compatibility
20
21    Revision 1.9 2004/01/15 15:20:29 bobintetley
22    Java2D work
23
24    Revision 1.8 2003/12/22 13:58:50 bobintetley
25    Image scaling support
26
27    Revision 1.7 2003/12/15 17:44:22 bobintetley
28    Image getHeight/Width support
29
30    Revision 1.6 2003/12/14 09:13:38 bobintetley
31    Added CVS log to source headers
32
33 */

34
35
36 package swingwt.awt;
37
38 import org.eclipse.swt.graphics.*;
39
40 public class Image {
41     
42     public final static int SCALE_DEFAULT = 1;
43     public final static int SCALE_FAST = 2;
44     public final static int SCALE_SMOOTH = 4;
45     public final static int SCALE_REPLICATE = 8;
46     public final static int SCALE_AREA_AVERAGING = 16;
47     
48     public org.eclipse.swt.graphics.Image image = null;
49     protected swingwt.awt.Graphics2D gc = null;
50     
51     public Image() {
52         //image = new org.eclipse.swt.graphics.Image();
53
}
54
55     public int getHeight() { return getHeight(null); }
56     public int getHeight(ImageObserver img) {
57         if (image != null)
58             return image.getBounds().height;
59         else
60             return 0;
61     }
62
63     public int getWidth() { return getWidth(null); }
64     public int getWidth(ImageObserver img) {
65         if (image != null)
66             return image.getBounds().width;
67         else
68             return 0;
69     }
70     
71     public Image getScaledInstance(int width, int height, int hints) {
72         
73         if (image == null) return null;
74         if (width < 0 && height < 0) throw new IllegalArgumentException JavaDoc("You must supply width or height");
75         
76         // If width or height is negative, we need to maintain the
77
// aspect ratio for the one that's missing
78
if (height < 0) {
79             double aspectRatio = ((double) image.getBounds().width) / ((double) image.getBounds().height);
80             height = (int) ((double) width * (double) aspectRatio);
81         }
82         if (width < 0) {
83             double aspectRatio = ((double) image.getBounds().width) / ((double) image.getBounds().height);
84             width = (int) ((double) height * (double) aspectRatio);
85         }
86         
87         // Create our new output image
88
org.eclipse.swt.graphics.Image destImage =
89             new org.eclipse.swt.graphics.Image(swingwtx.swing.SwingWTUtils.getDisplay(), width, height);
90         
91         // Paint our image on it
92
GC gc = new GC(destImage);
93         gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, width, height);
94         
95         // Construct our return image
96
Image retImage = new Image();
97         retImage.image = destImage;
98         
99         // Dispose of the GC
100
gc.dispose();
101         
102         return retImage;
103         
104     }
105     
106     public Graphics getGraphics() { return createGraphics(); }
107     public Graphics2D createGraphics() {
108         if (gc != null)
109             return gc;
110         // We have an image and no GC now - create it
111
else if (image != null) {
112             gc = new SWTGraphics2DRenderer( new GC(image), true);
113             return gc;
114         }
115         else
116             return null;
117     }
118 }
119
Popular Tags