Graphics2D: Should I use int or float versions?

Some of the Graphics2D methods, such as drawString , have versions that accept coordinates as int or float . Are there any reasons for choosing one of them?

Also, should I use new Shape classes such as Rectangle2D (which use floating point coordinates), or use a Rectangle (which defines coordinates as int s)?

+4
source share
1 answer

I see int and float -

 drawString(AttributedCharacterIterator iterator, float x, float y) Renders the text of the specified iterator, using the Graphics2D context current Paint. drawString(AttributedCharacterIterator iterator, int x, int y) Renders the text of the specified iterator, using the Graphics2D context current Paint. 

Use float if you need higher precision, int if there are enough positions.

Regarding your second question, please see below, which was described / answered by the following, Rectangle and Rectangle2D Difference :

Rectangle uses int coordinates. Rectangle2D is an abstract class that does not care if you use int, double, or float coordinates.

If you need more precision double and float, you will need to use Rectangle2D.

Rectangle2D is a base class, so if you are writing code that works on rectangular shapes in an abstract way, go to Rectangle2D and assign it like this:

 Rectangle2D rect = new Rectangle2D.Double(double, double, double, double); 

or

 Rectangle2D rect = new Rectangle(int, int, int, int) 

If you know that you intend to work only with ints, you can use Rectangle fully.

We can say that the Rectangle should be called Rectangle2D.Integer. But this is not entirely true, because, for example, the Rectangle is the only one of the three that implements a serializable interface.

As skaffman commented, this is a bit dated issue.

+4
source

All Articles