Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display a BufferedImage in a JFrame?

I want to display variations of the same image in the same JFrame, for example display an image in JFrame, then replace it with gray scale of the same image.

like image 484
anon Avatar asked Oct 26 '09 19:10

anon


People also ask

How do I add an image to a JFrame?

If you want to add an image, choose the JPictureBox, after that go to Properties and find "icon" property and select an image.

What is the difference between BufferedImage and image?

List, the difference between Image and BufferedImage is the same as the difference between List and LinkedList. Image is a generic concept and BufferedImage is the concrete implementation of the generic concept; kind of like BMW is a make of a Car. Show activity on this post. Image is an abstract class.


2 Answers

To build on camickr's solution (for the lazy like me who want quick code to copy/paste) here's a code illustration:

JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.getContentPane().add(new JLabel(new ImageIcon(img2)));
frame.getContentPane().add(new JLabel(new ImageIcon(img3)));
frame.pack();
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // if you want the X button to close the app
like image 114
Ian Will Avatar answered Oct 07 '22 05:10

Ian Will


You will have to repaint the JFrame whenever you update the image.

Here is what a simple google on the topic brings up: (I use those tutorials for all my Java coding)

Java Tutorial: Drawing an Image

like image 37
ldog Avatar answered Oct 07 '22 06:10

ldog