Yes — two things: 1 - You load a new image each time your paintComponent method calls drawImage. You only need to load the image one time so this line
belongs in your class constructor or a method called from it.
2 - in the test app below I originally put this line
inside paintComponent here
and the (System.out.println()) marker showed that paintComponent was being called repeatedly, so much so that the app would/could not repaint itself after being partially covered/uncovered by another window. The marker output in the console was
suggesting that the second instance of ImageScalingPanel loaded first and the first one went spastic.
So I looked in the getScaledInstance method detail to find this line: The new Image object may be loaded asynchronously even if the original source image has already been loaded completely. Apparently the attempt to load the scaled instance inside paintComponent was causing a runaway loop.
The code below works okay. Note what happens to the image quality for scale factors over 1.0.
[ August 10, 2004: Message edited by: Craig Wood ]
Joined: Feb 17, 2004
Thanks for your reply, yet there's still the problem. I copied the complete code to compile and run, but it can't still show the scaled picture. There was nothing in the window, just as before. How in the world can I solve this problem? Is it because of my compiler?