I have a JPanel
with a painted background image and a layout manager holding other smaller images, all of this inside a JFrame
. The background image is pretty big and I want to be able to have it maintain its aspect ratio whether its on a big or small monitor.
Eventually, I want to be able to have my LayoutManager
and the smaller images in its cells "glued" to the background picture.
I looked around for resources and it seems that many examples use a BufferedImage
but I am not; will this pose a problem? I'll post my code below for painting the image, If I lack any information please let me know.
public class MonitorPanel extends JPanel {
Image img;
public MonitorPanel() throws MalformedURLException {
//add components
try {
img = ImageIO.read(new File("src/customer_vlans.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void paintComponent(Graphics g)
{
//paint background image
super.paintComponent(g);
//g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
g.drawImage(img, 0, 0, this);
}
}
EDIT: I should mention that I know the aspect ratio formula: original height / original width x new width = new height However, I do not know how to use that correctly to my advantage.
For anyone interested ammending the PaintComponent method by MadProgrammer as follows allows much quicker display update
Try something like this:
which will ultimately scale the image to the
JPanel
's size by usinggetScaledInstance(int width, int height, ImageObserver io)
Well, the quickest and easiest solution is to use
Image.getScaledInstance
If your wondering about the negative number, the java docs say:
UPDATE
Just as a side note (my Google was playing up).
getScaledInstance
is neither the fastest or highest quality approach, but it is the easiest.Take a read through The Perils of Image.getScaledInstance for some more ideas
UPDATE
Scaling an image to fit an area is slightly more complicated then simply scaling the aspect ratio. You have to make a choice over if you want the image to "fit" within the area (possibly leaving blank areas around it) or over "fill" the area (so that it's smallest dimension fits the largest dimension of the area).
Fit & Fill
Basically, I work with scale factors
This returns the scaling factor for a particular size. I use this to make decisions about which factor I want to use based which algorithm I need
It's used by these two methods. They simply take two
Dimension
s. The original and the target.It's relatively simple to pass an image into (either directly or via a support method). So for example, you could call this from within your
paint
methodThis will automatically take care of the aspect ratio for you ;)
UPDATED with expanded example
I came up with this solution: