1 19 20 package com.sshtools.ui.awt; 21 22 import java.awt.Canvas ; 23 import java.awt.Color ; 24 import java.awt.Dimension ; 25 import java.awt.Graphics ; 26 import java.awt.Image ; 27 import java.awt.MediaTracker ; 28 29 35 36 public class ImageCanvas extends Canvas { 37 38 41 public final static int CENTERED = 0; 42 43 46 public final static int STRETCH = 1; 47 48 private MediaTracker tracker; 50 private Image image; 51 private int scale; 52 private int border; 53 private Color borderColor; 54 private float valign = CENTER_ALIGNMENT; 55 private boolean paintBackground; 56 private float halign = CENTER_ALIGNMENT; 57 private boolean doubleBuffered; 58 private int bufferWidth; 59 private int bufferHeight; 60 private Image bufferImage; 61 private Graphics bufferGraphics; 62 63 69 public ImageCanvas() { 70 super(); 71 } 72 73 81 public ImageCanvas(Image image) { 82 super(); 83 setImage(image); 84 repaint(); 85 } 86 87 98 public ImageCanvas(Class cls, String image) { 99 super(); 100 setImage(UIUtil.loadImage(cls, image)); 101 } 102 103 108 public void setValign(float valign) { 109 this.valign = valign; 110 } 111 112 117 public void setHalign(float halign) { 118 this.halign = halign; 119 } 120 121 126 public void setBorder(int border) { 127 this.border = border; 128 } 129 130 135 public int getBorder() { 136 return border; 137 } 138 139 144 public Color getBorderColor() { 145 return borderColor; 146 } 147 148 153 public void setBorderColor(Color borderColor) { 154 this.borderColor = borderColor; 155 } 156 157 169 public void setScale(int scale) { 170 this.scale = scale; 171 repaint(); 172 } 173 174 186 public int getScale() { 187 return scale; 188 } 189 190 195 public void setImage(Image image) { 196 if ((image == null && this.image != null) || (image != null && this.image == null) 198 || (image != null && this.image != null && (image.getWidth(this) != this.image.getWidth(this) || image.getHeight(this) != this.image.getHeight(this)))) { 199 this.image = image; 200 doLayout(); 201 } else { 202 this.image = image; 203 } 204 paintBackground = true; 205 repaint(); 206 } 207 208 213 public Image getImage() { 214 return image; 215 } 216 217 222 public void update(Graphics g) { 223 paint(g); 224 } 225 226 public boolean isDoubleBuffered() { 227 return doubleBuffered; 228 } 229 230 public void setDoubleBuffered(boolean doubleBuffered) { 231 this.doubleBuffered = doubleBuffered; 232 } 233 234 public void paint(Graphics g) { 235 if (!doubleBuffered) { 236 paintBuffer(g); 237 } else { 238 if (bufferWidth != getSize().width || bufferHeight != getSize().height || bufferImage == null || bufferGraphics == null) 241 resetBuffer(); 242 243 if (bufferGraphics != null) { 244 bufferGraphics.clearRect(0, 0, bufferWidth, bufferHeight); 246 247 paintBuffer(bufferGraphics); 250 251 g.drawImage(bufferImage, 0, 0, this); 253 } 254 } 255 } 256 257 private void paintBuffer(Graphics g) { 258 259 Dimension d = getSize(); 260 if (paintBackground) { 261 g.setColor(getBackground()); 262 g.fillRect(0, 0, d.width, d.height); 263 } 264 Dimension f = new Dimension (d.width - (border * 2), d.height - (border * 2)); 265 if (image != null) { 266 if (scale == STRETCH) { 267 g.drawImage(image, border, border, f.width, f.height, this); 268 } else { 269 int x = Math.max((f.width - image.getWidth(this)) / 2, 0); 270 if (halign == Canvas.LEFT_ALIGNMENT) { 271 x = 0; 272 } else if (halign == Canvas.RIGHT_ALIGNMENT) { 273 x = f.width - image.getWidth(this); 274 } 275 int y = Math.max((f.height - image.getHeight(this)) / 2, 0); 276 if (valign == Canvas.TOP_ALIGNMENT) { 277 y = 0; 278 } else if (valign == Canvas.BOTTOM_ALIGNMENT) { 279 y = f.height - image.getHeight(this); 280 } 281 g.drawImage(image, x + border, y + border, this); 282 } 283 } 284 285 } 286 287 private void resetBuffer() { 288 bufferWidth = getSize().width; 290 bufferHeight = getSize().height; 291 292 if (bufferGraphics != null) { 294 bufferGraphics.dispose(); 295 bufferGraphics = null; 296 } 297 if (bufferImage != null) { 298 bufferImage.flush(); 299 bufferImage = null; 300 } 301 System.gc(); 302 303 bufferImage = createImage(bufferWidth, bufferHeight); 305 bufferGraphics = bufferImage.getGraphics(); 306 } 307 308 public Dimension getPreferredSize() { 309 if (image == null) { 310 return new Dimension (border * 2, border * 2); 311 } else { 312 return new Dimension (image.getWidth(this) + (border * 2), image.getHeight(this) + (border * 2)); 313 } 314 } 315 316 public Dimension getMinimumSize() { 317 return getPreferredSize(); 318 } 319 } 320 | Popular Tags |