Search code examples
javanetbeansbitmapjava-2d

How to show pictures in jFrame, using java2d?


I'm new on working with Java and Netbeans. In many others languages, it's a simple stuff to do. But after broke my brain thinking, I couldn't. My doubt is simple to explain. How can I show bitmaps (stored on Hard Drive) in runtime, in a commom JFrame, using java2D? What I need to edit, and or create? Is it simple to do?

Thanks in advance...


Solution

  • The basic process is use Graphics#drawImage to render an image you have previously loaded.

    There are a number of things you need in order to achieve this...

    • An image to paint
    • A surface to paint on

    Luckily, both these are relatively easy to achieve in Swing

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class ShowMyImage {
    
        public static void main(String[] args) {
            new ShowMyImage();
        }
    
        public ShowMyImage() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    ImagePane pane = new ImagePane();
                    try {
                        pane.setImg(ImageIO.read(new File("C:\\hold\\thumbnails\\_MTCGAC__Pulling_Cords_by_Dispozition.png")));
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(pane);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class ImagePane extends JPanel {
    
            private BufferedImage img;
    
            public ImagePane() {
            }
    
            public void setImg(BufferedImage value) {
                if (img != value) {
                    img = value;
                    repaint();
                }
            }
    
            public BufferedImage getImg() {
                return img;
            }
    
            @Override
            public Dimension getPreferredSize() {
                BufferedImage img = getImg();
                return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                BufferedImage img = getImg();
                if (img != null) {
                    int x = (getWidth() - img.getWidth()) / 2;
                    int y = (getHeight()- img.getHeight()) / 2;
    
                    g2d.drawImage(img, x, y, this);
                }
                g2d.dispose();
            }
        }
    }