Re: [問題] getGraphics() 和 createGraphics() 差異

看板java作者 (偶爾想擺爛一下)時間15年前 (2010/05/20 15:18), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《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
瞭解了, 原來只有差在(Graphics2D)上, 感謝
05/20 16:26, 1F
文章代碼(AID): #1BzE8t1H (java)
文章代碼(AID): #1BzE8t1H (java)