【发布时间】:2011-08-14 07:00:41
【问题描述】:
某些Graphics2D 方法(例如drawString)具有将坐标作为int 或float 的版本。有什么理由选择其中一个吗?
同样,我应该使用较新的Shape 类,例如Rectangle2D(使用浮点坐标),还是使用Rectangle(将坐标定义为ints)?
【问题讨论】:
标签: java coordinates shape graphics2d
某些Graphics2D 方法(例如drawString)具有将坐标作为int 或float 的版本。有什么理由选择其中一个吗?
同样,我应该使用较新的Shape 类,例如Rectangle2D(使用浮点坐标),还是使用Rectangle(将坐标定义为ints)?
【问题讨论】:
标签: java coordinates shape graphics2d
我看到 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 是基类,因此,如果您正在编写以抽象方式对矩形进行操作的代码,请选择 Rectangle2D,并像这样分配它:
Rectangle2D rect = new Rectangle2D.Double(double, double, double, double);
或
Rectangle2D rect = new Rectangle(int, int, int, int)
如果你知道你只会处理整数,你可以一直使用 Rectangle。
您可能会说 Rectangle 应该称为 Rectangle2D.Integer。但这也不完全是,因为例如Rectangle 是三个实现可序列化接口的唯一一个。
就像 skaffman 评论的那样,这是一个遗留问题。
【讨论】: