Results 1 to 9 of 9

Thread: Java image load

  1. #1
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Java image load

    I am trying to learn how load an image file in java. I've done this in an applet but cannot load it in a application.

    Here is the code

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;

    import javax.imageio.ImageIO;
    import javax.swing.JFrame;


    public class Main extends JFrame {
    public static void main(String[] args)

    {
    ImageLoad();

    }
    public static void ImageLoad()
    {
    BufferedImage img = null;


    try {
    img = ImageIO.read(new File("C:/slime.jpg"));

    }
    catch (IOException e)
    { ;
    }

    System.out.print(img);


    }// Image load method end


    }//End of Main class


    I am returning the data as console, but how do i return the actual image?

    Thanks for the help

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java image load

    how do i return the actual image
    If the image's bytes are in an object, return a reference to that object.
    I'm not sure what you mean by "return" for an image. Can you explain?

    BTW You shouldn't leave catch blocks empty and ignore any exceptions. Add a call to printStackTrace() to the catch block.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Java image load

    Quote Originally Posted by Norm View Post
    If the image's bytes are in an object, return a reference to that object.
    I'm not sure what you mean by "return" for an image. Can you explain?

    BTW You shouldn't leave catch blocks empty and ignore any exceptions. Add a call to printStackTrace() to the catch block.

    Oh by return i mean render from memory visually

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java image load

    One way is make the image an Icon and add it to a JLabel
    or create a JPanel, override its paintComponent() method and draw the image.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Java image load

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

  6. #6
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Java image load

    Can you paste some code or be more specific pls

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Java image load

    Quote Originally Posted by dd2308 View Post
    Can you paste some code
    This is a forum, not a code factory.

    Quote Originally Posted by dd2308 View Post
    or be more specific pls
    How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

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

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java image load

    There are lots of code samples on the forum. Try doing a Search here for code: ImageIcon and drawImage
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

Similar Threads

  1. Load image from jar
    By sublixt in forum Java 2D
    Replies: 2
    Last Post: 10-17-2011, 01:15 AM
  2. How to load an image using Toolkit
    By pappyj77 in forum AWT / Swing
    Replies: 7
    Last Post: 07-25-2011, 02:16 PM
  3. Load percentage of an image
    By trishtren in forum Java 2D
    Replies: 0
    Last Post: 04-15-2011, 03:02 PM
  4. Can not load image in SWT/Eclipse
    By janice in forum SWT / JFace
    Replies: 4
    Last Post: 10-03-2008, 05:53 PM
  5. Help with load image
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:16 AM

Tags for this Thread

Posting Permissions

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