Question

I was wondering if there is a way to plot a point using a double value in the built in java graphics utility. I am making a simple clock but I want it to be as precise as possible. The method drawLine(int, int, int, int) in Graphics obviously won't take a double as a parameter. Is there a work-around to this?

P.S. The doubles in question are the change in x and y for each hand on the clock as 1 second passes.

Was it helpful?

Solution

Graphics2D has an internal AffineTransform Matrix that you can alter. I have not tested the following code, but i think this or a variant might get close to what you want (Although really, the pixels are all at integer positions...)

Graphics2D g; // get a Graphics2D from somewhere
g.rotate(Math.PI/4);
g.drawLine(0,0,1,0); //draw a line at 45°
// now you should probably rotate back...

OTHER TIPS

Pixels only exist at integer positions. think of it like a chess board, each square is only one colour and it means nothing to say half a square.

There are some things you can do (bi linear interpolation for example) to draw in between pixels but all they do is modify all the pixels around the point according to a suitable algorithm and the position you select.

If you want higher resolution then the simplest thing is just to increase the resolution of the image you are using. More pixels gives more possible precision, until you reach the limits of your display device...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top