3

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!

2
  • You cannot do that. The x and y values are pixel positions and you cannot have something like half-pixel. If you supply 0.5 to this function, it will get truncated to 0 (possibly requiring a cast to int). Mar 10, 2012 at 20:44
  • Thanks for the quick reply! Ok then, basically i want an oval to move at down at an angle that isnt 0,45, or -45 degrees. is there any way to do this?
    – hazard1994
    Mar 10, 2012 at 20:49

2 Answers 2

5

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.

2
-1

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.

4
  • hmm, slighty confused, but its given me an idea! Is there a way to move the oval one pixel left for ever 3 down
    – hazard1994
    Mar 10, 2012 at 20:58
  • 2
    You cannot using Java, you mean. Sub-pixel accuracy has been around for quite a while, actually...
    – 6502
    Mar 10, 2012 at 20:58
  • No, in Swing it is not possible. Mar 10, 2012 at 21:01
  • This is misleading, because it's possible, and not hard. See my answer with before+after picture: stackoverflow.com/a/31221395/1143274 Specifically, you can't have 0.3 pixels, but you can blend the pixel's colour to 30% foreground colour, and 70% background. This principle is called antialiasing (this name is misleading too, heh), and you can see many examples if you have a look at your screen with a magnifying glass. Jul 4, 2015 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.