Results 1 to 6 of 6
  1. #1
    Steelsmasher is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Display a buffered image in a window

    Can someone show me how to load a bufferedimage and display it on JFrame.

    I've already searched the internet but unfortunately the solutions given come in bits and pieces so I'm not really sure what fits where.
    Therefore I'd prefer if someone shown me the full code including the main method please.
    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Display a buffered image in a window

    Quote Originally Posted by Steelsmasher View Post
    Can someone show me how to load a bufferedimage and display it on JFrame.

    I've already searched the internet but unfortunately the solutions given come in bits and pieces so I'm not really sure what fits where.
    Therefore I'd prefer if someone shown me the full code including the main method please.
    Thanks.
    This has already been given many many times before. Rather than ask us to re-write that which has been already beaten to death, why not give it a try based on what you've found, and then if you get stuck, show us your code and ask a specific question.
    Last edited by Fubarable; 07-29-2012 at 03:15 AM.

  3. #3
    Steelsmasher is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Display a buffered image in a window

    Well that's the thing. I don't even know where to start with bufferedimages.

    I've searched extremely hard.. honestly. But what I get are bits of code that I don't know how to put together.

    Like I get told how to load a bufferedimage :
    BufferedImage img = null;
    try {
    img = ImageIO.read(new File("strawberry.jpg"));
    } catch (IOException e) {
    }


    Then I'm told how to draw it:
    public void paint(Graphics g){
    super.paint(g);
    g.drawImage(img, 20,20,this);
    }


    (not even sure if those examples are compatible)
    But then I don't even know how to put them together in a way that actually works. To be specific I just want a window that displays a buffered image on it.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Display a buffered image in a window

    Quote Originally Posted by Steelsmasher View Post
    Well that's the thing. I don't even know where to start with bufferedimages.
    I've searched extremely hard.. honestly. But what I get are bits of code that I don't know how to put together.
    As with most complex problems, the way to solve them is to divide and conquer -- that means try to solve each single little problem one thing at a time and in isolation.

    Like I get told how to load a bufferedimage :
    Java Code:
    BufferedImage img = null;
    try {
        img = ImageIO.read(new File("strawberry.jpg"));
    } catch (IOException e) {
    }
    OK, this is a good place to start: try creating a small program that does nothing but load in a BufferedImage using ImageIO. You could use the code above as a starting point, but you will not want to ignore exceptions as that code above is doing. In the very least print out a stacktrace:
    Java Code:
    BufferedImage img = null;
    try {
        img = ImageIO.read(new File("strawberry.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    Also and of course, your jpg file name will be different. Key to get this to work is to make sure you know where to look for the file. Putting this in your active code:
    Java Code:
    System.out.println(System.getProperty("user.dir"));
    will tell you where Java will start to look for your image file. Then make sure that your image file path name is relative to the user directory.

    Then if you can do this without error , simply try to display the image in a JOptionPane. You would create an ImageIcon object with the BufferedImage, then create a JLabel with the ImageIcon object, then display the JLabel in a JOptionPane.showMessageDialog(null, myJLabel).

    Once you've succeeded at doing that then next work on displaying the image in a JPanel's paintComponent method (not its paint method). Again look for examples including those at the Java tutorials.

    Also, please display your code using [code] [/code] tags, not color tags.

  5. #5
    Steelsmasher is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Display a buffered image in a window

    Thanks for the advice, much appreciated.
    Guess I was looking for a quick fix, but I wouldn't mind going the long way and learning things bit by bit.

    Also do you know any tutorials particularly about JPanel's paintComponent. I just don't want to end up in the same trap with different codes from different sources.
    As I've said I already searched but it seems like noone explains it all in one.

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Display a buffered image in a window

    I personally haven't gotten to painting, but I think this tutorial might help out: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Similar Threads

  1. Buffered Image and DataBuffer
    By ErdyMurphy in forum Java 2D
    Replies: 1
    Last Post: 04-19-2011, 05:55 AM
  2. Volatile image vs buffered image
    By trishtren in forum Java 2D
    Replies: 0
    Last Post: 04-18-2011, 12:31 AM
  3. Drawing onto a buffered image
    By trishtren in forum Java 2D
    Replies: 3
    Last Post: 04-09-2011, 10:23 PM
  4. Rotating Buffered Image distorts image
    By VortexSpin in forum Java 2D
    Replies: 1
    Last Post: 02-13-2011, 06:54 AM
  5. Unable to draw buffered image
    By pedjasmek in forum Java 2D
    Replies: 7
    Last Post: 08-08-2008, 04:49 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
  •