Java2D:将Graphics转换为Graphics2D总是安全的 [英] Java2D: Is it always safe to cast Graphics into Graphics2D

查看:184
本文介绍了Java2D:将Graphics转换为Graphics2D总是安全的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们总是使用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.



图形投射有效的原因到Graphics2D,是因为Sun
已经说过Java 1.2中的API返回的所有Graphics对象
或者上面的
都是
Graphics2D的子类。

The reason it is valid to cast from Graphics to Graphics2D, is because Sun have said that all Graphics objects returned by the API in Java 1.2 or above will be a subclass of Graphics2D.

另一个提示这里有相同的结论。

Another hint here with the same conclusion.


图形对象总是可以投射
Graphics2D g2d =(Graphics2D )g;

Graphics Object can always be cast Graphics2D g2d = (Graphics2D)g;

这篇关于Java2D:将Graphics转换为Graphics2D总是安全的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆