/ / Dibujar un círculo simple con Java swing no funciona - java, swing, awt, java-2d

Dibujar un círculo simple con Java swing no funciona - java, swing, awt, java-2d

Creo que me falta algo realmente obvio, pero de alguna manera este código me da una ventana vacía pero no pinta el óvalo rojo. ¿Qué me estoy perdiendo?

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

}

Respuestas

4 para la respuesta № 1

el paintComponent no es correcto, quita esto 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);
...
}