/ / Tracer un simple cercle avec Java Swing ne fonctionne pas - java, swing, awt, java-2d

Dessiner un simple cercle avec Java Swing ne fonctionne pas - java, swing, awt, java-2d

Je pense que je manque quelque chose de vraiment évident mais de toute façon ce code me donne une fenêtre vide mais il ne peint pas l'ovale rouge. Qu'est-ce que je rate?

public class Test extends JPanel {

@Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
g = this.getGraphics();
Graphics2D g2 = (Graphics2D) g;

// Anti-aliasing
g2.setColor(new Color(255, 0, 0));
g2.fillOval(0, 0, 20, 20);
}

public static void main(String[] args) {
JFrame frame = new JFrame("Ball");
Test panel = new Test();

frame.getContentPane().add(panel);
frame.setPreferredSize(new Dimension(250, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();
frame.setVisible(true);
}

}

Réponses:

4 pour la réponse № 1

la paintComponent n'est pas correct, supprimez ceci g = this.getGraphics();

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;

Ellipse2D.Double circle = new Ellipse2D.Double(xR, yR, diameter, diameter);
g2d.fill(circle);
...
}