Re: [問題] getGraphics() 和 createGraphics() 差異
※ 引述《germun (ger)》之銘言:
: 會有這個疑問是因為我要對某張 BufferedImage 做塗色
: a) 我可以用 image.createGraphics 取得一個 Graphics2D 來作畫
: b) 或是用 image.getGraphics 再強制轉成 Graphics2D 來作畫
: ps. getGraphics 回傳的不是 Graphics2D 是因為沒有 draw() 這個 method
: a, b畫出來的效果其實一樣的, 但我不太懂這兩者實際運作上的差異
: 因為感覺 a 比較偏向 new 一個 Graphics2D 出來(?)
: 會不會比較容易有延遲的問題? 在執行效率上是否比 b 來得差?
Java 1.2 開始,core classes 內所有的 Graphics implementation 皆是
Graphics2D 介面,任一個可取得的 Graphics instance 皆可當作 Graphics2D
來操作。
Image image = ...;
java.awt.Graphics2D g2d = (java.awt.Graphics2D) image.getGraphics();
至於你提到的其他問題,先看一下 sun JRE java.awt.image.BufferedImage
的原始碼,再自行判斷。
public java.awt.Graphics getGraphics() {
return createGraphics();
}
*/
public Graphics2D createGraphics() {
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
return env.createGraphics(this);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.173.133.140
※ 編輯: sbrhsieh 來自: 218.173.133.140 (05/20 15:19)
推
05/20 16:26, , 1F
05/20 16:26, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):