For anti-aliased circles (smoother edges), use Graphics2D:
import javax.swing.*;
import java.awt.*;
public class SmoothCircleDrawer extends JPanel {
private int centerX = 200;
private int centerY = 200;
private int radius = 100;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.RED);
g2d.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
SmoothCircleDrawer panel = new SmoothCircleDrawer();
frame.add(panel);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
- Graphics2D improves rendering quality using anti-aliasing.
- Ideal for high-resolution or scientific visualizations.