Graphics2D:我应该使用int还是float版本? [英] Graphics2D: Should I use the int or float versions?

查看:88
本文介绍了Graphics2D:我应该使用int还是float版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些 Graphics2D 方法,例如 drawString ,其版本的坐标为 int float 。有什么理由选择一个吗?

Some of the Graphics2D methods, such as drawString, have versions that take coordinates as int or float. Are there any reasons to choose one over the other?

同样,我应该使用较新的 Shape 类,例如作为 Rectangle2D (使用浮点坐标),或使用 Rectangle (将坐标定义为 int s)?

Likewise, should I use the newer Shape classes, such as Rectangle2D (which use floating-point coordinates), or use Rectangle (which defines coordinates as ints)?

推荐答案

我看到了int和float-

I see int and 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。

Use float if you need more precision, int if the positions suffice.

关于您的第二个问题,请参见下面的内容,由以下内容矩形和Rectangle2D差异

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

矩形使用int坐标。 Rectangle2D是一个抽象类,不管您使用的是int,double还是float坐标。

Rectangle uses int coordinates. Rectangle2D is an abstract class that doesn't care if you're using int, double or float coordinates.

如果您需要double和float更高的精度,则可以

If you need the greater precision of double and float, you'll have to go with Rectangle2D.

Rectangle2D是基类,因此,如果您要编写以抽象方式对矩形进行操作的代码,请使用Rectangle2D,然后像这样分配它:

Rectangle2D is the base class, so if you're writing code that operates on rectangular shapes in an abstract way, go for Rectangle2D, and assign it like so:

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

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

如果您知道您只需要处理整数,就可以一直使用Rectangle。

If you know you're only going to deal with ints, you can use Rectangle all the way.

您可能会说Rectangle应该称为Rectangle2D.Integer。但这也不尽然,因为例如矩形是三个实现可序列化接口的唯一之一。

You might say that Rectangle should be called Rectangle2D.Integer. But that's not quite it either, because e.g. Rectangle is the only one of the three that implements the serializable interface.

就像skaffman所说,这是一个遗留问题。

Like skaffman commented, it's a bit of a legacy issue.

这篇关于Graphics2D:我应该使用int还是float版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆