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();
}
}
@Override
The method paintComponent(Graphics) of type Graphs must override or implement a supertype method.
1 quick fix available:
-> Remove '@Override' annotation
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);