File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

swing paint performance

 
Brandon Broschinsky
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1535
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic