Results 1 to 5 of 5
  1. #1
    vaggos is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default 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.

  2. #2
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: How to manage files in file chooser

    what about lists??

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to manage files in file chooser

    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

    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

    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.

    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:
    Java 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.

  4. #4
    vaggos is offline Member
    Join Date
    Aug 2012
    Posts
    12
    Rep Power
    0

    Default 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 :)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: How to manage files in file chooser

    Quote Originally Posted by vaggos View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 11
    Last Post: 12-04-2011, 10:10 PM
  2. JFile Chooser
    By karno in forum NetBeans
    Replies: 4
    Last Post: 03-17-2010, 12:47 PM
  3. File chooser internationalization problem
    By Astghik in forum AWT / Swing
    Replies: 10
    Last Post: 01-14-2010, 06:44 PM
  4. [SOLVED] File chooser selecting file from directory...?
    By prabhurangan in forum AWT / Swing
    Replies: 12
    Last Post: 06-18-2008, 04:08 AM
  5. Using File Chooser
    By shaungoater in forum New To Java
    Replies: 0
    Last Post: 03-20-2008, 12:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •