作者sbrhsieh (偶尔想摆烂一下)
看板java
标题Re: [问题] getGraphics() 和 createGraphics() 差异
时间Thu May 20 15:18:11 2010
※ 引述《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)
1F:推 germun:了解了, 原来只有差在(Graphics2D)上, 感谢 05/20 16:26