With the code as it is (with the debug lines), it actually works fine, and the output is:
-1 -1
550 220
When I comment out the *first* println, I get this output, and it goes into an infinite loop because of these values:
-1 -1
So it looks like somehow what's happening is that the first call to getWidth() and getHeight() on the newly-scaled image will always return -1, but will change subsequent calls to the correct values.
What on earth is going on here? Why is it exhibiting this behaviour?
bg = new ImageIcon(OpenScreen.class.getResource("/res/images/HomepageHeader.png")).getImage();
bg = bg.getScaledInstance(TOP_HEIGHT*bg.getWidth(null)/bg.getHeight(null), TOP_HEIGHT, Image.SCALE_FAST);
System.out.println(bg.getWidth(null) + "\t" + bg.getHeight(null));
JPanel topPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension d = getSize();
int width = bg.getWidth(null);
int height = bg.getHeight(null);
System.out.println(width + "\t" + height);
for (int x = 0; x < d.width; x += width) {
for (int y = 0; y < d.height; y += height) {
g.drawImage(bg, x, y, null, null);
//System.out.println(x + "\t" + y);
}
}
}
};

New Topic/Question
Reply



MultiQuote





|