gpt4 book ai didi

Java2D : Is it always safe to cast Graphics into Graphics2D

转载 作者:IT老高 更新时间:2023-10-28 20:51:58 25 4
gpt4 key购买 nike

假设我们总是使用 Sun JVM(比如 1.5+),将 Graphics 引用转换为 Graphics2D 总是安全的吗?

我还没有看到它导致任何问题,据我了解,Graphics 类是遗留代码,但 Java 设计人员不想更改 Swing 和 AWT 类的接口(interface)以保持向后兼容性。

最佳答案

根据讨论hereGraphics 转换为 Graphics2D 总是安全的。但是,我无法快速找到 Sun 的官方声明。

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.

另一个提示here得出同样的结论。

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

关于Java2D : Is it always safe to cast Graphics into Graphics2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/179415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com