views:

1384

answers:

4

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 circle no matter the size. How would I implement anti-aliasing in java or some other method.

+2  A: 

you can set rendering hints:

Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
Josef
+2  A: 

Two things that may help:

  1. Use Graphics2D.draw(Shape) with an instance of java.awt.geom.Ellipse2D instead of Graphics.draw.Oval
  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

finnw
+1  A: 

Of course you set your radius to what ever you need:

@Override
public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    Ellipse2D.Double hole = new Ellipse2D.Double();
    hole.width = 28;
    hole.height = 28;

hole.x = 14; hole.y = 14; g2d.draw(hole); }

Clint
You also need to set the position of the ellipse, and I think you want g2d.draw not g2d.stroke
finnw
+5  A: 

As it turns out, Java2D (which I'm assuming is what you're using) is already pretty good at this! There's a decent tutorial here: http://www.javaworld.com/javaworld/jw-08-1998/jw-08-media.html

The important line is:

graphics.setRenderingHints(Graphics2D.ANTIALIASING,Graphics2D.ANTIALIAS_ON);

CaptainAwesomePants
You gotta love links 11 years old, hope they will never die.
Mercer Traieste