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
2009-07-07 20:09:07
+2
A:
Two things that may help:
- Use
Graphics2D.draw(Shape)
with an instance ofjava.awt.geom.Ellipse2D
instead ofGraphics.draw.Oval
- 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
2009-07-07 20:09:32
+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
2009-07-07 20:09:33
You also need to set the position of the ellipse, and I think you want g2d.draw not g2d.stroke
finnw
2009-07-07 20:14:40
+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
2009-07-07 20:10:02