Bufferedimage does not appear on the screen

Asked

Viewed 134 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


    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();
                }   
            });
        }

    }



    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;

    import javax.swing.JPanel;

    public class Pane extends JPanel {

        private BufferedImage img;
        private Graphics2D g2d;

        public Pane() {
            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() {
            g2d.setColor(Color.BLACK);
            g2d.drawRect(10, 10, 200, 200);
        }

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

    }

inserir a descrição da imagem aqui When I run the program it just appears the Frame, I figured it is some problem when calling the Paint This is all the code

  • 2

    Right.. And what is the question? Where is the error? An important tip here: make life easier for those who are willing to help you.. Of ALL possible details, otherwise, hardly anyone will give up their time to perform analyses of others.

  • Thanks for the tip The Error is that the drawing does not appear in the panel, I followed several tutorials but still not right, I do not know what the problem may be

  • And on the console, no error?

  • Nothing, the console is empty

1 answer

2


You are creating a new instance of the Graphics2d type, when you should actually get it through one provided by the implementation. How? Through the paintComponent behavior();

Note, however, that the paintCompoment() method does not override the Jpanel base class method (which it inherits from Jcomponent), because in order for this to happen, it needs this superscript method in the sub class, to contain the same signature and return type.

See how it is correct signature:

protected void paintComponent(Graphics g)

and see how your

public void paintComponent(Graphics2D g)

To avoid these misconceptions (write wrong name, different signatures and returns), try to use the @Override annotation in the method you want to overwrite, as this way you will get error at compile time, if the method you want to overwrite, does not match in inherited classes.

That said, change your class, so that it uses the reference controlled by Swing, injected in the paintComponent method. Briefly changing your code would look something like this:

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();
                }   
            });
        }

    }

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;

    import javax.swing.JPanel;

    public class Pane extends JPanel {

        private BufferedImage img;

        public Pane() {
            img = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
        }


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

        public void draw(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.BLACK);
            g2d.drawImage(img, 0, 0, null);
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            // Referencia compartilhada.
            draw(g);
        }

    }

See, I didn’t go into other details (height, image width), I just used your example to demonstrate how to use the shared reference.

  • Thanks M, that’s just what I needed, really good explanation

Browser other questions tagged

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