Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By JosAH

Thread: Importing Images

  1. #1
    e.cummins95 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Importing Images

    Can someone give me an example of how to import a saved image into Java? I've looked all over the internet and need a quick and easy way to do it. I already know how to draw it (with g.drawImage).

    Thanks!

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

    Default Re: Importing Images

    Read the API for Image and click the "Use" link at the top of the page. Go through the sections for methods that return Image.

    If you don't find exactly what you want, you may need to consider whether a particular Image subclass serves the purpose. Go through any subclasses of Image and their subclasses and check out the "Use" of each, till you find a method that returns an Image that serves your purpose.

    Hint: since you want to load a saved image, you will need to focus on methods that take a File or a URL/URI (or a String representation of any of those) as a parameter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Importing Images

    I use the ImageIO class to create BufferedImages - these work well for general java2d stuff! You can also save images with ImageIO!

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

    Default Re: Importing Images

    Quote Originally Posted by quad64bit View Post
    I use the ImageIO class to create BufferedImages - these work well for general java2d stuff!
    Try loading an animated image with ImageIO.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default Re: Importing Images

    Quote Originally Posted by DarrylBurke View Post
    Try loading an animated image with ImageIO.
    Try to get a BufferedImage from an ImageIcon.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Importing Images

    Quote Originally Posted by JosAH View Post
    Try to get a BufferedImage from an ImageIcon.
    If you put one in, you can get one out. ImageIcon isn't immutable, though by all design considerations it should be.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default Re: Importing Images

    Quote Originally Posted by DarrylBurke View Post
    If you put one in, you can get one out. ImageIcon isn't immutable, though by all design considerations it should be.
    That'd be cheating; if you put an animated .gif in you can't get a BufferedImage containing all frames of the orginal .gif out of it (only the first frame).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Importing Images

    Quote Originally Posted by JosAH View Post
    That'd be cheating; if you put an animated .gif in you can't get a BufferedImage containing all frames of the orginal .gif out of it (only the first frame).
    getImage() will return an Image object that models the animated image. On my system, that Image is of type sun.awt.image.ToolkitImage

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default Re: Importing Images

    Quote Originally Posted by DarrylBurke View Post
    getImage() will return an Image object that models the animated image. On my system, that Image is of type sun.awt.image.ToolkitImage
    Same here; but that class isn't documented anywhere nor is it part of a public API donated to us poor plebeians; I think it has something to do with the days when animated .gif files were patented; only the Sun gods were allowed to ingfringe that pattern but us ignorant sods still have to fiddle diddle with Timers and image redraws and all that crappy stuff ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. importing classes
    By javajames in forum New To Java
    Replies: 6
    Last Post: 05-24-2011, 09:21 AM
  2. ...................... Importing images.
    By falkon114 in forum New To Java
    Replies: 3
    Last Post: 02-17-2011, 08:38 PM
  3. Replies: 0
    Last Post: 12-16-2010, 05:14 PM
  4. Replies: 4
    Last Post: 12-16-2010, 04:32 PM
  5. Importing JXL API
    By ryanlbowen in forum New To Java
    Replies: 0
    Last Post: 07-17-2009, 07:17 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
  •