16

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.

Jonas
  • 109,920
  • 93
  • 295
  • 369
anon
  • 657
  • 3
  • 14
  • 19

4 Answers4

45

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
Dariusz
  • 20,650
  • 8
  • 71
  • 109
Ian Will
  • 962
  • 10
  • 13
  • This just makes the smallest window possible. –  Jan 30 '17 at 23:43
  • 1
    It's been a long time since I used swing, but i'd guess you didn't call `pack()` if you have a tiny window – Ian Will Feb 01 '17 at 00:22
  • 1
    exactly what i was looking for, quickly check whether bufferedImages are read correctly .. no need for all this JComponent extending and print overriding. Thanks! – philx_x Apr 17 '17 at 17:25
  • @IanWill you will also need to call `frame.setPreferredSize(new java.awt.Dimension(int width, int height));` before calling `frame.pack();` importing java.awt.Dimension also works too :) Also this should be the selected answer. +1 for code example. – Pranav A. Sep 27 '17 at 02:06
8

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

Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
ldog
  • 11,070
  • 9
  • 53
  • 69
4

I'm not really sure what you question is but if you have a BufferedImage then you simply create an ImageIcon using the image, then you add the icon to a JLabel and add the label to the GUI like any other component.

If you question is about how to create a gray scale, the I suggest you search the web using those terms as the search keywords, I'm sure you will find examples out there.

camickr
  • 316,400
  • 19
  • 155
  • 279
4

Just incase life's to short too read the official docs here's a dirty way to get it done multiple times over

private static JFrame frame;
private static JLabel label;
public static void display(BufferedImage image){
   if(frame==null){
       frame=new JFrame();
       frame.setTitle("stained_image");
       frame.setSize(image.getWidth(), image.getHeight());
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       label=new JLabel();
       label.setIcon(new ImageIcon(image));
       frame.getContentPane().add(label,BorderLayout.CENTER);
       frame.setLocationRelativeTo(null);
       frame.pack();
       frame.setVisible(true);
   }else label.setIcon(new ImageIcon(image));
}
LiNKeR
  • 827
  • 1
  • 6
  • 16