public void write(BufferedImage image) throws IOException {
   writeByte(image.getWidth());
   writeByte(image.getHeight());
   for (int pixel : Images.imageToRGB(image)) {
     writeInt(pixel);
   }
 }
  protected Icon[] makeIcons(String location, int count, boolean horizontal) {
    Icon[] icons = new Icon[count];

    BufferedImage[] images = Images.split(createImage(location), count, horizontal);

    for (int i = 0; i < count; i++) {
      icons[i] = new IconUIResource(new ImageIcon(images[i]));
    }
    return icons;
  }
  public static Images getImage(String filename) {
    try {
      if (toolkit == null) toolkit = Toolkit.getDefaultToolkit();

      Image img = toolkit.getImage(filename);

      if (img == null) return null;

      Images image = new Images();

      img.getSource().startProduction(image);

      synchronized (image) {
        image.wait(100);
      }

      if (image.width > 0 && image.height > 0) return image;
      else return null;
    } catch (Throwable e) {
      return null;
    }
  }
Example #4
0
  private synchronized void loadImage(Object imageData) {
    if (DEBUG.IMAGE) out("loadImage " + imageData);

    // test of synchronous loading:
    // out("***GOT IMAGE " + Images.getImage(imageData));

    // TODO: refactor ResourceIcon to use the new ImageRef --
    // will be much simpler.

    if (Images.getImage(imageData, this) == null) {
      // will make callback to gotImage when we have it
      isLoading = true;
      showLoadingStatus();
    } else {
      // gotImage has already been called
      isLoading = false;
    }
  }
  protected void paintToImage(Component c, Graphics gr, int width, int height, Object args) {
    // Cast Graphics to Graphics2D
    // Workaround for Java 1.4 and 1.4 on Mac OS X 10.4. We create a new
    // Graphics object instead of just casting the provided one. This is
    // because drawing texture paints appears to confuse the Graphics object.
    Graphics2D g = (Graphics2D) gr.create();

    // Convert image to buffered image (and keep the buffered image).
    image = Images.toBufferedImage(image);
    BufferedImage img = (BufferedImage) image;

    // Set some variables for easy access of insets and image size
    int top = imageInsets.top;
    int left = imageInsets.left;
    int bottom = imageInsets.bottom;
    int right = imageInsets.right;
    int imgWidth = img.getWidth();
    int imgHeight = img.getHeight();

    // Optimisation: Draw image directly if it fits into the component
    if (fillContentArea) {
      if (width == imgWidth && height == imgHeight) {
        g.drawImage(img, 0, 0, c);
        g.dispose();
        return;
      }
    }

    // Optimisation: Remove insets, if image width or image height fits
    if (width == imgWidth) {
      left = imgWidth;
      right = 0;
    }
    if (height == imgHeight) {
      top = imgHeight;
      bottom = 0;
    }

    // Adjust insets if component is too small
    if (width < left + right) {
      left = Math.min(left, width / 2); // Math.max(0, left + (width - left - right) / 2);
      right = width - left;
    }
    if (height < top + bottom) {
      top = Math.min(top, height / 2); // Math.max(0, top + (height - top - bottom) / 2);
      bottom = height - top;
    }

    // Draw the Corners
    if (top > 0 && left > 0) {
      g.drawImage(img, 0, 0, left, top, 0, 0, left, top, c);
    }
    if (top > 0 && right > 0) {
      // g.fillRect(x+width-right, y, x+width, y+top);
      g.drawImage(img, width - right, 0, width, top, imgWidth - right, 0, imgWidth, top, c);
    }
    if (bottom > 0 && left > 0) {
      g.drawImage(img, 0, height - bottom, left, height, 0, imgHeight - bottom, left, imgHeight, c);
    }
    if (bottom > 0 && right > 0) {
      g.drawImage(
          img,
          width - right,
          height - bottom,
          width,
          height,
          imgWidth - right,
          imgHeight - bottom,
          imgWidth,
          imgHeight,
          c);
    }

    // Draw the edges
    BufferedImage subImg = null;
    TexturePaint paint;

    // North
    if (top > 0 && left + right < width) {
      if (imgWidth > right + left) {
        subImg = img.getSubimage(left, 0, imgWidth - right - left, top);
        paint = new TexturePaint(subImg, new Rectangle(left, 0, imgWidth - left - right, top));
        g.setPaint(paint);
        g.fillRect(left, 0, width - left - right, top);
      }
    }
    // South
    if (bottom > 0 && left + right < width) {
      if (imgHeight > bottom && imgWidth > right + left) {
        subImg = img.getSubimage(left, imgHeight - bottom, imgWidth - right - left, bottom);
        paint =
            new TexturePaint(
                subImg, new Rectangle(left, height - bottom, imgWidth - left - right, bottom));
        g.setPaint(paint);
        g.fillRect(left, height - bottom, width - left - right, bottom);
      }
    }
    // West
    if (left > 0 && top + bottom < height) {
      if (imgHeight > top + bottom) {
        subImg = img.getSubimage(0, top, left, imgHeight - top - bottom);
        paint = new TexturePaint(subImg, new Rectangle(0, top, left, imgHeight - top - bottom));
        g.setPaint(paint);
        g.fillRect(0, top, left, height - top - bottom);
      }
    }
    // East
    if (right > 0 && top + bottom < height) {
      if (imgWidth > right && imgHeight > top + bottom) {
        subImg = img.getSubimage(imgWidth - right, top, right, imgHeight - top - bottom);
        paint =
            new TexturePaint(
                subImg, new Rectangle(width - right, top, right, imgHeight - top - bottom));
        g.setPaint(paint);
        g.fillRect(width - right, top, right, height - top - bottom);
      }
    }

    // Fill the center
    if (fillContentArea) {
      if (left + right < imgWidth && top + bottom < imgHeight) {
        subImg = img.getSubimage(left, top, imgWidth - right - left, imgHeight - top - bottom);
        paint =
            new TexturePaint(
                subImg,
                new Rectangle(left, top, imgWidth - right - left, imgHeight - top - bottom));
        g.setPaint(paint);
        g.fillRect(left, top, width - right - left, height - top - bottom);
      }
    }
    g.dispose();
  }