Pergunta

I just started coding again and I guess I forgot how to double buffer. This is the code I have now and I'm not sure what I am missing. When I start it there is just a white screen, no oval.

What is the mistake in rendering?

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;

public class Graphs extends JFrame {

private Image dbImage;
private Graphics dbg;

public static void main(String[] args) {
    new Graphs();
}

public Graphs() {
    setSize(1000, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setTitle("Graphs");
    setLocationRelativeTo(null);
    setVisible(true);
}

public void paint(Graphics g) {
    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    dbg.drawImage(dbImage, 0, 0, this);
}

public void paintComponent(Graphics g) {
    g.drawOval(200, 200, 200, 200);
    repaint();
}
}

Update: compilation error on @Override

The method  paintComponent(Graphics) of type Graphs must override or implement a supertype method.

1 quick fix available:
-> Remove '@Override' annotation
Foi útil?

Solução

The reason that you're not seeing the oval is that you're drawing the image onto its own Graphics object. Replace:

dbg.drawImage(dbImage, 0, 0, this);

with

g.drawImage(dbImage, 0, 0, this);

Also better not to override paint in a top-level container but rather override paintComponent in a sub-classed JComponent. Also remember to call

super.paintComponent(g);

Outras dicas

  1. You should avoid overriding the paint method of top level
    components (like JFrame), the main issue for you is that they are not double buffered, like components that extend from JComponent
  2. Failing to call super.paint is VERY, VERY bad. You've basically prevent the frame from painting any of its child components...
  3. You shouldn't be loading images within the paint method, it will slow down any future repaints
  4. You should never call repaint from any paintXxx method, it will cause an infinite loop of paints to be created, quickly devouring your CPU cycles
  5. JFrame does not have a paintComponent method.
  6. It is preferred that custom painting be performed an JComponent (such as a JPanel) via the paintComponent method.

Update with Example

enter image description here

public class BadPaint10 {

    public static void main(String[] args) {
        new BadPaint10();
    }

    public BadPaint10() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new PaintPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintPane extends JPanel {

        private BufferedImage background;

        public PaintPane() {
            try {
                background = ImageIO.read(new File("C:/Users/shane/Dropbox/pictures/436px-Work_with_exotic_creatures.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (background != null) {
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight() - background.getHeight()) / 2;
                g.drawImage(background, x, y, this);

                x = (getWidth() - 200) / 2;
                y = (getHeight() - 200) / 2;
                g.setColor(Color.RED);
                g.drawOval(x, y, 200, 200);

            }

        }
    }
}

As has already been noted, precedence is important. The order that elements are paint will effect the out come.

You might find

Useful.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top