问题描述
假设我们总是使用Sun JVM(比如1.5+),那么将Graphics引用转换为Graphics2D总是安全的吗?
Assuming we always use a Sun JVM (say, 1.5+), is it always safe to cast a Graphics reference to Graphics2D?
我还没有看到它导致任何问题,据我所知,Graphics类是遗留代码,但Java设计者不想更改接口Swing和AWT类以保持向后兼容性。
I haven't seen it cause any problems yet and, to my understanding, the Graphics class is legacy code but the Java designers didn't want to change the interfaces for Swing and AWT classes in order to preserver backwards compatibility.
推荐答案
根据讨论,从 Graphics
投射总是安全的到 Graphics2D
。但是我无法快速找到关于此的正式Sun声明。
According to the discussion here, it is always safe to cast from Graphics
to Graphics2D
. However I am not able to quickly find the official Sun statement on this.
另一个提示有相同的结论。
Another hint here with the same conclusion.
这篇关于Java2D:将Graphics转换为Graphics2D总是安全的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!