| Author |
Why can't I use getScaledInstance to scale images?
|
Howard Ting
Greenhorn
Joined: Feb 17, 2004
Posts: 19
|
|
The statement I tried is as follows: g.drawImage(new ImageIcon("chi_wei.jpg").getImage().getScaledInstance(100,100,Image.SCALE_DEFAULT),0,0,Color.WHITE,this); But it ended up drawing nothing out. Could somebody tell me if there's something wrong with the statement above or something I forget?
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24045
|
|
|
This sounds like a Swing/AWT question to me, so I'm going to move this to the Swing/AWT forum.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
|
|
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 ]
|
 |
Howard Ting
Greenhorn
Joined: Feb 17, 2004
Posts: 19
|
|
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?
|
 |
 |
|
|
subject: Why can't I use getScaledInstance to scale images?
|
|
|