This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Canvas precision

 
Iason Dimitrakopoulos
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to have double precision for the x,y coords of a canvas? And how would that be possible? So far i havent found anything on this subject.
thanks
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use double precision numbers to keep track of values and then cast them to appropriate integer coordinates to use to draw them on the canvas. Multiply them by a scale value and then use Math.round(), Math.rint(), or a simple cast to get an integer value. A lot of the Java2D Shape classes actually do use real numbers (both doubles and floats), so you could also "upgrade" to using Graphics2D.
 
Iason Dimitrakopoulos
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply..
let me get this straight:
as i understand it graphics2d will enable me to draw lines, points etc on an area with double precision
is that right?
(i dont HAVE to use a canvas but i really want precise positioning of the things i draw.. it's for a mapview)
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can still use canvas, but you just cast the Graphics object you get passed to your paint() method to a Graphics2D object.


Take a look at the Line2D and Point2D classes...

and a Java2D Tutorial.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!