某些Graphics2D
方法(如drawString
)的版本以坐标为int
或float
。有没有什么理由选择一个呢?Graphics2D:我应该使用int还是float版本?
同样地,应该使用较新的Shape
类,如Rectangle2D
(其使用浮点坐标),或使用Rectangle
(其坐标定义为int
S)?
某些Graphics2D
方法(如drawString
)的版本以坐标为int
或float
。有没有什么理由选择一个呢?Graphics2D:我应该使用int还是float版本?
同样地,应该使用较新的Shape
类,如Rectangle2D
(其使用浮点坐标),或使用Rectangle
(其坐标定义为int
S)?
我看到int和float -
drawString(AttributedCharacterIterator iterator, float x, float y)
Renders the text of the specified iterator, using the Graphics2D context's current Paint.
drawString(AttributedCharacterIterator iterator, int x, int y)
Renders the text of the specified iterator, using the Graphics2D context's current Paint.
使用float如果你需要更精确,INT如果位置不够的。
关于你提到的第二个问题,请参见下面进行了说明/回答以下,Rectangle and Rectangle2D Difference:
矩形使用INT坐标。 Rectangle2D是一个抽象类,不关心你是否使用int,double或float坐标。
如果您需要double和float的更高精度,则必须使用Rectangle2D。
矩形的基类,所以如果你正在写在一个抽象的方式长方形运行的代码,去为矩形,并指定像这样:
Rectangle2D rect = new Rectangle2D.Double(double, double, double, double);
或
Rectangle2D rect = new Rectangle(int, int, int, int)
如果你知道你只是要处理整数,你可以一直使用矩形。
你可能会说Rectangle应该被称为Rectangle2D.Integer。但是这也不完全是因为例如Rectangle是实现可序列化接口的三个中唯一的一个。
就像skaffman评论的那样,这有点遗留问题。
太好了,谢谢。 (我刚纠正后,*浮动*不是双倍) – 2011-04-27 23:43:57