image.getScaledInstance() bug?

A

ak

this example works fine with SCALE_REPLICATE or without PixelGrabber.
With PixelGrabber and SCALE_AREA_AVERAGING it produces really strange
results.
Is it a known bug?

public static void main(String[] args) throws IOException {

//change to your favorite jpeg image
String s = "Portrait.jpg";

final Image image = Toolkit.getDefaultToolkit().createImage(s);
//ensure image loaded
/*ImageIcon icon = */new ImageIcon(image);

final int width = image.getWidth(null);
final int height = image.getHeight(null);

//****<grab pixels>*****************************
int[] pixels = new int[width * height];
PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height,
pixels, 0, width);
try {
pg.grabPixels();
}
catch (InterruptedException e) {
return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
throw new RuntimeException("can't fetch pixels");
}
//***</grab pixels>*****************************

//with SCALE_REPLICATE everithing work fine
final Image img = image.getScaledInstance(w, h,
Image.SCALE_AREA_AVERAGING);
//ensure image loaded
new ImageIcon(img);


JFrame frame = new JFrame();
JPanel panel = new JPanel() {
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, this);
g.drawImage(img, 0, 0, this);
}
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
};

frame.getContentPane().add(new JScrollPane(panel));
frame.pack();
frame.show();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Java Animation 0
Mirror an image 5
getting pixel data from image as single value 1
swings 9
getting pixel from image as single value 3
PixelGrabber 2
Help with my responsive home page 2
Create png image file 2

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top