How to manage files in file chooser
Hello everybody.
I am building an Image Viewer like the one the Windows has.
One of the choices I offer the user is to enter the directory he wants through a file chooser and display the images in it(one at a time via arrow buttons)and then choose the one he likes.The problem is that I keep every picture of the directory in arrays(one array for the icons,another for filepaths etc).Is there any smarter way to do that?This turns out to be a problem in another thing I want to achieve: resize the image that the jpanel shows whenever jframe size changes.I have managed to do that,but the program is very ,very slow.So I believe a reason for that is that I use 'for' loops in order to change all images' icons according to the new jpanel size.
Re: How to manage files in file chooser
Re: How to manage files in file chooser
Quote:
The problem is that I keep every picture of the directory in arrays
You are storing the actual binary image data in an array? I wouldn't do this, it's very memory hungry. A folder of 5000 images would probably cause it to explode
Quote:
Is there any smarter way to do that?
Yes, only store information about where the file is, not the file itself, and then load the file on demand
Quote:
resize the image that the jpanel shows whenever jframe size changes
That's an expensive operation and becomes exponentially more expensive as the size of the image increases. There will be some limits here without a lot of ram and hardware acceleration.
Quote:
So I believe a reason for that is that I use 'for' loops in order to change all images' icons according to the new jpanel size.
I doubt it. You can rip through a few billion iterations of a for loop in milliseconds or less. The thing that might be slowing you down is the image processing itself. If you are using pure java to do your image resizing, there are some limits on the draw performance you can get. Assuming your code is as optimized as possible and you aren't doing a lot of unnecessary work, then you probably have hit the java2d speed barrier. I would also check what you are using to do the actual resize, since some java2d methods are many times slower than others. The one I used last was:
Code:
def scaleImage(img){
float wr = maxWindowSize.width / img.width
float hr = maxWindowSize.height / img.height
float scaler = wr < hr ? wr : hr
BufferedImage after = new BufferedImage((int)(img.width*scaler), (int)(img.height*scaler), BufferedImage.TYPE_INT_ARGB)
AffineTransform at = new AffineTransform()
at.scale(scaler, scaler)
AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR)
after = scaleOp.filter(img, after)
after
}
Thats groovy code, not java, but the classes and techniques are the same, just add semi colons and a proper method header/return
I've read that using the AffineTransform in this way is quite a bit faster than other techniques.
I've also had success offloading the image processing to imageMagick, which for very large images (20 meg jpgs or 50 meg pngs, that kind of thing) is substantially faster. This will require that you install imageMagick though, and that breaks your app's portability to some extent.
If you profile your app, I'm guessing the two slowest parts are:
1. Resizing the image for display
2. Drawing the collection of images to the screen
Figure out which is the worst, and then see what you can do to minimize the calls to that.
Re: How to manage files in file chooser
Thank you very much for the reply.I have managed to limit the problem a little bit by changing a few things.Now I read the label and its icon currently attached to it and display it without scanning the arrays.These seems to work faster.I will check out your advice and try to implement the whole thing without using arrays for the images.Hopefully this will make thing much better :)
Re: How to manage files in file chooser
Quote:
Originally Posted by
vaggos
a problem in another thing I want to achieve: resize the image that the jpanel shows whenever jframe size changes.I have managed to do that,but the program is very ,very slow.So I believe a reason for that is that I use 'for' loops in order to change all images' icons according to the new jpanel size.
You could try my Stretch Icon « Java Tips Weblog (or ShrinkIcon, linked from that page) class.
What does the subject line of this thread have to do with the question posted?
db