wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes swing paint performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "swing paint performance" Watch "swing paint performance" New topic
Author

swing paint performance

Brandon Broschinsky
Ranch Hand

Joined: Aug 21, 2004
Posts: 41
I have an application that i wrote, it has two panels. on the panel i allow the user to add photo's i then scale them down to thumbnails as the user adds them and store them in and arraylist. I then paint all the thumbnails in the list into the panel. But the repainting of these images is very slow, is there any way to either:
a)speed up the painting of the images
this is my paintComp
I store the thumbnail images i have sized in the thumbnailImages arraylist
public void paintComponent(Graphics g) {
//System.out.println("h "+this.getHeight()+" w "+this.getWidth());
super.paintComponent(g); // paints background
if(!thumbnailImages.isEmpty()){ //only paint if have images
int x=1;
int y=1;
for(int i=0;i<thumbnailImages.size();i++){
thumbnail = (ImageIcon)thumbnailImages.get(i);
thumbnail.paintIcon(this,g,x,y);
y=y+thumbnail.getIconHeight()+5;
size=new Dimension(this.getWidth(),y);
setSize(size);
}
}
}

This is how i am scaling the pictures
public void addImage(String url){
Image tempImage = null;
ImageIcon tempIcon = null;
images.add(url);
tempImage=(java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url));
tempIcon = new ImageIcon(tempImage.
getScaledInstance(this.getWidth(), -1,
Image.SCALE_FAST));
thumbnailImages.add(tempIcon);
}
b)speed up the scaling(if that is the cause)

Thanks in advance
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
You can experiment with these two ways of scaling. The BufferedImage/AffineTransform is advertised as faster. If the time is still excessive you might consider doing the scaling on a background thread. My computer takes about half a second to scale both sets of images (sum of image sizes = 348 kb).
I would avoid handling ImageIcons if possible. You can always set an image in an ImageIcon before display. But this is more for simplicity and may not affect the timing.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: swing paint performance