1 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 } 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 ("You must supply width or height"); 75 76 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 org.eclipse.swt.graphics.Image destImage = 89 new org.eclipse.swt.graphics.Image(swingwtx.swing.SwingWTUtils.getDisplay(), width, height); 90 91 GC gc = new GC(destImage); 93 gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, width, height); 94 95 Image retImage = new Image(); 97 retImage.image = destImage; 98 99 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 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 |