File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "swing paint performance" Watch "swing paint performance" New topic

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);
size=new Dimension(this.getWidth(),y);

This is how i am scaling the pictures
public void addImage(String url){
Image tempImage = null;
ImageIcon tempIcon = null;
tempIcon = new ImageIcon(tempImage.
getScaledInstance(this.getWidth(), -1,
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.
subject: swing paint performance
It's not a secret anymore!