Results 1 to 6 of 6
- 07-29-2012, 01:13 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 8
- Rep Power
- 0
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.
-
Re: Display a buffered image in a window
Last edited by Fubarable; 07-29-2012 at 02:15 AM.
- 07-29-2012, 03:00 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 8
- Rep Power
- 0
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.
-
Re: Display a buffered image in a window
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.
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: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) { }
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:BufferedImage img = null; try { img = ImageIO.read(new File("strawberry.jpg")); } catch (IOException e) { e.printStackTrace(); }
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.Java Code:System.out.println(System.getProperty("user.dir"));
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.
- 07-29-2012, 03:58 AM #5
Member
- Join Date
- Jul 2012
- Posts
- 8
- Rep Power
- 0
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.
- 07-29-2012, 04:15 AM #6
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
-
Buffered Image and DataBuffer
By ErdyMurphy in forum Java 2DReplies: 1Last Post: 04-19-2011, 04:55 AM -
Volatile image vs buffered image
By trishtren in forum Java 2DReplies: 0Last Post: 04-17-2011, 11:31 PM -
Drawing onto a buffered image
By trishtren in forum Java 2DReplies: 3Last Post: 04-09-2011, 09:23 PM -
Rotating Buffered Image distorts image
By VortexSpin in forum Java 2DReplies: 1Last Post: 02-13-2011, 05:54 AM -
Unable to draw buffered image
By pedjasmek in forum Java 2DReplies: 7Last Post: 08-08-2008, 03:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks