Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Currently, I have a JFrame for my game to render in, and I'm using Graphics2D for drawing (The games graphics are fairly simple 2D sprites). However, my delta variable is a double, and all of the Graphics 2D methods (And Grpahics) use int. I tried to type cast the delta to an int, but it just rounds down to 0. So my question is, how can I render graphics using Graphics2D in Java with coordinates that are doubles. Can I convert it to work with Graphics2D if there is no built in way? Or, is there a graphics library that can support doubles for coordinates?

share|improve this question
    
How do you intend to light a fraction of a pixel on your computer monitor? –  ClassicThunder Dec 6 '12 at 4:25
2  
It can be done with sub-pixel accurate rendering and antialiasing. –  msell Dec 6 '12 at 8:06

1 Answer 1

up vote 1 down vote accepted

Graphics class uses integer coordinates for all operations, but Graphics2D provides API with float and double accuracy. You can use Graphics2D.draw(Shape) and Graphics2D.fill(Shape) to get sub-pixel accuracy in rendering. You should also probably enable antialiasing for the Graphics2D object.

public void paintComponent(Graphics graphics) {
    Graphics2D g = (Graphics2D)graphics;
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.draw(new Line2D.Double(10.5, 10.5, 20.5, 20.5));
}

Alternatively you can scale, rotate and translate Graphics2D object or manipulate the AffineTransform directly. This might be needed if you want to draw images to double coordinates.

share|improve this answer
    
I was actually thinking about checking out Shape & using that. Thanks the for the answer! –  thedeadlybutter Dec 6 '12 at 16:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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