Scalable images in Java

2019-08-15 02:46发布

问题:

I anticipate using some images in my Java application. These images will be drawn onto my JPanel using g.drawImage(). The JPanel is resizable and the images should scale as the JPanel increases/decreases in size

Two questions:

  1. What image format is best for this type of desired scalable image? For instance, in this application, an image of size 100x100 may be scaled into an image of size 30x30 or 10x10 or 300x300.
  2. How can I write code to do this scaling?

回答1:

Take a look at Java: maintaining aspect ratio of JPanel background image

I'd also take a read through The Perils of Image.getScaledInstance() and I'd also have a look at java-image-scaling (I've not used it personally, but I've heard good things about it).

As to the image format. I doubt it will really make that much of difference, the quality will be determined by the quality of the original image. Try and use a large image and scale it down, rather then trying to scale a small image up.



回答2:

  1. It doesn't matter, I don't think. (If you wanted images not to get blurry when you scale them up, look into vector images).

  2. You might want to use Image.getScaledImage(int, int, int), in a listener that is called on window resize.