Problem with showing Bufferedimage in Java Jpanel

Asked

Viewed 110 times

-2

I’m learning Java and I needed to use Bufferedimage, but for some reason this doesn’t seem to work

Error is that the drawing does not appear in the panel, I followed several tutorials but still not sure, I do not know what the problem may be

I used Borderfactory to see if the panel was working, and yes the panel is correct, but the drawing inside the panel does not appear

Can anyone figure out why the drawing doesn’t appear? It was to appear a square on the screen


    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;

    public class App extends JFrame{



        public App() {
            super("Buffered Test");
            add(new Pane());
            pack();
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);    
            setVisible(true);       
        }



        public static void main(String[] args) {

            SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    new App();
                }   
            });
        }

    }


//Classe do painel
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;

    import javax.swing.BorderFactory;
    import javax.swing.JPanel;

    public class Pane extends JPanel {

        private BufferedImage img;
        private Graphics2D g2d;

        public Pane() {
            setBorder(BorderFactory.createLineBorder(Color.BLACK));

            img = new BufferedImage(App.WIDTH, App.HEIGHT, BufferedImage.TYPE_INT_RGB);
            g2d = (Graphics2D) img.getGraphics();


            draw();
        }


        public Dimension getPreferredSize() {
            return new Dimension(600, 400);
        }

        public void draw() { //Metodo para fazer o desenho
            g2d.setColor(Color.BLACK);
            g2d.drawRect(10, 10, 200, 200);
        }

        public void paintComponent(Graphics2D g) {
            super.paintComponent(g);
            g.drawImage(img, 0, 0, null);
        }

    }

Este é o resultado do código, a borda aparece, então o painel esta funcionando

1 answer

0


The first thing you need to change is the method paintComponent, you are not overwriting the superclass method because the parameter is wrong, the correct is Graphics, nay Graphics2D.

Second, App.WIDTH is returning 1 and App.HEIGHT is returning 2. Even with the above method corrected, the image will be drawn with width of 1 and height of 2 pixels. As you placed the border, even then the image would not appear. Change these two constructor parameters to the correct size.

Delete your method draw(). If your intention was to change the color of the image just by setting the black color in the Graphics2D, not quite how it is done. Actually, in its implementation, would be drawn a black rectangle in BufferedImage, but since she’d already be black, you wouldn’t notice.

In place of your draw() place repaint(). This is the method of JPanel who calls the painting on canvas, in other words, he calls the paintComponent(Graphics g). So you should keep in mind that whatever you want to have as a visual result needs to be implemented in paintComponent.

If you are starting now in Java, do not recommend Swing right away.

  • Thanks, I tidied up here and it worked, agr I understood how it works, it was really worth

Browser other questions tagged

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