How to draw a decent looking Circle in Java

前端 未结 7 1392
我在风中等你
我在风中等你 2020-11-27 06:20

I have tried using the method drawOval with equal height and width but as the diameter increases the circle becomes worse looking. What can I do to have a decent looking cir

7条回答
  •  无人及你
    2020-11-27 06:45

    Two things that may help:

    1. Use Graphics2D.draw(Shape) with an instance of java.awt.geom.Ellipse2D instead of Graphics.drawOval
    2. If the result is still not satisfactory, try using Graphics2D.setRenderingHint to enable antialiasing

    Example

    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        Shape theCircle = new Ellipse2D.Double(centerX - radius, centerY - radius, 2.0 * radius, 2.0 * radius);
        g2d.draw(theCircle);
    }
    

    See Josef's answer for an example of setRenderingHint

提交回复
热议问题