0

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.

  • what do you expect such a method should do when you tell it to draw at a position between two pixels? – kutschkem Dec 05 '13 at 18:33
  • agree because helicopter view over Graphics/2D APIs showing a few methods with double – mKorbel Dec 05 '13 at 18:45
  • It makes sense to me now because a pixel is an integer essentially. I just have to figure out how to apply the change in x and y. For example, I have changeX = radius*Math.cos(angle). The angle changes every second for the hand to move. Even if I make changeX an integer, the only values available will be 0 through the radius as integers... – lilscarecrow Dec 05 '13 at 18:55

2 Answers2

0

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...

Tim B
  • 40,716
  • 16
  • 83
  • 128
0

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...
kutschkem
  • 7,826
  • 3
  • 21
  • 56