heber gentilin
heber gentilin

Reputation: 155

Image inside JFrame - Swing/AWT

Some days ago, I wasted a lot of time searching some way to show a image in a JFrame. And here is my final solution:

jPanel1 = new javax.swing.JPanel(){
        @Override
        public void paintComponent(Graphics g) {
            BufferedImage image = null;
            try {
                BufferedImage in = ImageIO.read(Startup.class.getResource("imagem.jpg"));
                image = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);
                g.drawImage(in, 0, 0, null);
            } catch (Exception ex) {}
            super.paintComponents(g);
        }
    };

I just want to know, if it is the one way to do that, or exists another solutions like a image component on Swing o AWT that can be easily used?

Upvotes: 0

Views: 323

Answers (1)

Nam
Nam

Reputation: 166

You can show an image using JLabel, which is much simpler than your solution. For example:

label.setIcon(new ImageIcon("Path/to/your/image.jpg")); 

Upvotes: 1

Related Questions