Reputation: 81
Basically, I want to draw a circle using Graphics, but instead of using integers to position it, I would like to use double values instead.
Ideally:
g.drawOval(0.5, 0.5, 50, 50);
Any help is greatly appreciated! Thanks in advance!
Thanks for all the help guys, but ive figured a way out, my dy variable was set at 1, so if i wanted dx to half of that of dy, it would be impossible, instead i just changed dy to 2 and dx to 1! Foolish me!
Upvotes: 3
Views: 12347
Reputation: 68877
The only valid way of doing this is to use an Ellipse2D.Double
shape and pass it to the draw(Shape)
method of a Graphics2D
instance. For the best results, enable anti-aliasing:
public void yourDrawingMethod(Graphics gg)
{
/* Cast it to Graphics2D */
Graphics2D g = (Graphics2D) gg;
/* Enable anti-aliasing and pure stroke */
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
/* Construct a shape and draw it */
Ellipse2D.Double shape = new Ellipse2D.Double(0.5, 0.5, 50, 50);
g.draw(shape);
}
You can use Graphics2D.fill(Shape)
as well.
Upvotes: 5
Reputation: 8874
If you want continous movement, then the best way is to keep x and y in double
variables. As the object moves, the variables will increment at different rates (depending on the angle), and then you can just cast to ints...
And you will get positions:
<0, 0.999> : 0
<1, 1.999> : 1
<2, 2.999> : 2
<3, 3.999> : 3
etc..
For sufficiently long movement it will seem fluent. But the positions must be integer values, because you are bound by technology. You cannot display something at 0.3 pixels. You either have the pixel or not, you cannot have only 0.3 of it.
Upvotes: -1