Пример #1
0
 /**
  * Prepare a buffer for the image, return if the canvas is ready Only need to call this when the
  * size of the canvas is changed, Since we automatically detect the size change in paint() only
  * call this on start
  */
 public boolean newImgBuf() {
   Dimension sz = getSize();
   if (sz.width == 0 || sz.height == 0) return false;
   // quit if the current image already has the right size
   if (img != null && imgG != null && sz.equals(imgSize)) return true;
   img = createImage(sz.width, sz.height);
   if (imgG != null) imgG.dispose();
   imgG = img.getGraphics();
   imgSize = sz;
   return true;
 }
Пример #2
0
  static Icon reescala(Icon ic, int maxW, int maxH) {
    if (ic == null) {
      return null;
    }
    if (ic.getIconHeight() == maxH && ic.getIconWidth() == maxW) {
      return ic;
    }

    BufferedImage bi =
        new BufferedImage(ic.getIconHeight(), ic.getIconWidth(), BufferedImage.TYPE_INT_ARGB);

    Graphics g = bi.createGraphics();
    ic.paintIcon(null, g, 0, 0);
    g.dispose();

    Image bf = bi.getScaledInstance(maxW, maxH, Image.SCALE_SMOOTH);

    return new ImageIcon(bf);
  }