View Jframe image

Asked

Viewed 30 times

1

When trying to display an image in JFrame, it is always displayed with height size, a few pixels less. How do I solve?

public class Janela extends JFrame {

    private JPanel painel;
    private BufferedImage image;
    private static int largura;
    private static int altura;

    public Janela() throws IOException {

        image = ImageIO.read(new File(getClass().getResource("Imagem/cao.png").getFile()));
        largura = image.getWidth();
        altura = image.getHeight();

        painel = new JPanel() {

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, this);
            }

        };
        setSize(largura, altura);
        getContentPane().add(painel);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) throws IOException {
        new Janela().setVisible(true);
    }
}
  • I just answered a question identical to this, there is using a Jdesktoppane, just change to a Jpanel that perfectly meets this doubt too.

  • I don’t understand: //draw the image inside the Buffer & #Xa; G2.drawImage(image, 0, 0, dWidth, dHeight, null); , Voce does not pass the bi parameter

  • Luke, in the third paragraph I explain this, see there again.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.