// set size of the button (inherited from JButton =) ) // changes the size of the button, and resizes the image to fit it. public void setSize(int x, int y) { if (!mode.equals("String")) { width = x; height = x; image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH); BWimage = BWimage.getScaledInstance(width, height, Image.SCALE_SMOOTH); } }
/** * Returns a smoothly scaled image using getScaledInstance. This method has interesting behaviour. * The scaled image retains its type (indexed/rgb and bitmask/translucent), and the algorithm * tries to scale smoothly within these constraints. For indexed, interpolated pixels are rounded * to the existing indexed colours. For bitmask, the behaviour depends on the platform. On * WinXP/J1.2 I found that the colour _behind_ each transparent pixel is used to interpolate * between nontransparent and transparent pixels. On BSD/J1.4 I found that the colours of * transparent pixels are never used, and only the nontransparent pixels are used when * interpolating a region with mixed transparent/nontransparent pixels. */ public JGImage scale(int width, int height) { // BufferedImage dstimg = createCompatibleImage(width,height); // BufferedImage srcimg = toBuffered(img); Image scaledimg = img.getScaledInstance(width, height, Image.SCALE_SMOOTH); try { /* this is necessary for scaled images too */ ensureLoaded(scaledimg); } catch (Exception e) { System.err.println("Error scaling image."); } return new JREImage(scaledimg); }
public void setImage(Image img, int gsize) { pieceSize = gsize; image = img.getScaledInstance(pieceSize, pieceSize, Image.SCALE_SMOOTH); this.setSize(pieceSize, pieceSize); this.setIcon(new ImageIcon(image)); }