Problems scaling an image


Image scaledImage = image.getScaledInstance(width,height,height);
                  g.drawImage(scaledImage,x,y,null);

This is how I try to scaled my picture and show it right after. But it doesnt work. I dont see the picture on the screen.

Does anyone know what I do wrong?

The third value isn’t height again, its hints to the scaling process and should be one of:

SCALE_DEFAULT, SCALE_FAST, SCALE_SMOOTH, SCALE_REPLICATE, SCALE_AREA_AVERAGING

(defined in Image)

Kev

PS. Could you start posting these sorts of questions in the right forums so other people searching for answers might find them easier after the fact.

And what would you suggest would be the right place to post this one?

I’d go for Java 2D.

Kev

Well I have tried your suggestion but it still doesnt work.

I see nothing but a blank screen :frowning:

Does it draw if you just use drawImage() on the original image?

Kev

yes it does. g.drawImage(image,x,y,null) works just fine. But i have a zoom tool that requires scaling.

Do you have any ideas what I fuck up in the code? :slight_smile:

I have tried .jpg, .png and .bmp

Could you post a bit more of the code? Or a reference to the file or something…?

Kev

Has the scalled image finished loading? Stick some MediaTracker magic in?


//This is where you define the picture
      public void setPicture(String path){
            this.path = path;
            image = new ImageIcon(path).getImage();
      }

//from the paint-method these lines are executed:
Image scaledImage = image.getScaledInstance(width,height,Image.SCALE_DEFAULT);
g.drawImage(scaledImage,x,y,null);


If you write
g.drawImage(image,x,y,null);

you get to see the picture. but the scaled edition doesnt show. Has is something to do with the ImageObserver which is null in this case?


 Image scaledImage = image.getScaledInstance(width,height,Image.SCALE_DEFAULT);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage( scaledImage, 0);
tracker.waitForAll();
g.drawImage(scaledImage,x,y,null); 

MediaTracker tracker = new MediaTracker(this);

what ya mean by “this” ?

It complains about that constructor.

To quote the apis:
MediaTracker(java.awt.Component comp)