某些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