Java2D:将Graphics转换为Graphics2D总是安全的

时间:2021-12-04 21:38:59

Assuming we always use a Sun JVM (say, 1.5+), is it always safe to cast a Graphics reference to Graphics2D?

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

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类是遗留代码,但Java设计者不想更改Swing和AWT类的接口以保持向后兼容性。

3 个解决方案

#1


25  

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.

根据这里的讨论,从Graphics转换为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.

从Graphics转换为Graphics2D有效的原因是因为Sun已经说过Java 1.2或更高版本中API返回的所有Graphics对象都是Graphics2D的子类。

Another hint here with the same conclusion.

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

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

Graphics Object总是可以转换为Graphics2D g2d =(Graphics2D)g;

#2


10  

In the book Filthy Rich Client by Chet Haase and Romain Guy they are saying that Swing almost always uses a Graphics2D object. Exceptions from this are printing and Swing's DebugGraphics object. So as long as none of these situations apply to your code it is safe to cast to Graphics2D.
Both of the authors worked at Sun, so I would assume that they know what they are talking about.

在Chet Haase和Romain Guy的书Filthy Rich Client中,他们说Swing几乎总是使用Graphics2D对象。例外情况是打印和Swing的DebugGraphics对象。因此,只要这些情况都不适用于您的代码,就可以安全地转换为Graphics2D。两位作者都在Sun工作,所以我认为他们知道他们在谈论什么。

#3


6  

The 2D Graphics Trail says:

2D Graphics Trail说:

To employ Java 2D API features in the application, cast the Graphics object passed into a component’s rendering method to a Graphics2D object. For example:

要在应用程序中使用Java 2D API功能,请将传递到组件的呈现方法中的Graphics对象转换为Graphics2D对象。例如:

public void paint (Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    ...
}

This is the most "official" source I could find. Coming straight from Sun's Java Tutorials, I'd say that this is the officially sanctioned way of doing it. I wouldn't have exactly minded if the JavaDocs spelled this out, though...

这是我能找到的最“官方”的来源。直接来自Sun的Java教程,我会说这是官方认可的方式。如果JavaDocs拼写出来,我不会完全明白,不过......

#1


25  

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.

根据这里的讨论,从Graphics转换为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.

从Graphics转换为Graphics2D有效的原因是因为Sun已经说过Java 1.2或更高版本中API返回的所有Graphics对象都是Graphics2D的子类。

Another hint here with the same conclusion.

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

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

Graphics Object总是可以转换为Graphics2D g2d =(Graphics2D)g;

#2


10  

In the book Filthy Rich Client by Chet Haase and Romain Guy they are saying that Swing almost always uses a Graphics2D object. Exceptions from this are printing and Swing's DebugGraphics object. So as long as none of these situations apply to your code it is safe to cast to Graphics2D.
Both of the authors worked at Sun, so I would assume that they know what they are talking about.

在Chet Haase和Romain Guy的书Filthy Rich Client中,他们说Swing几乎总是使用Graphics2D对象。例外情况是打印和Swing的DebugGraphics对象。因此,只要这些情况都不适用于您的代码,就可以安全地转换为Graphics2D。两位作者都在Sun工作,所以我认为他们知道他们在谈论什么。

#3


6  

The 2D Graphics Trail says:

2D Graphics Trail说:

To employ Java 2D API features in the application, cast the Graphics object passed into a component’s rendering method to a Graphics2D object. For example:

要在应用程序中使用Java 2D API功能,请将传递到组件的呈现方法中的Graphics对象转换为Graphics2D对象。例如:

public void paint (Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    ...
}

This is the most "official" source I could find. Coming straight from Sun's Java Tutorials, I'd say that this is the officially sanctioned way of doing it. I wouldn't have exactly minded if the JavaDocs spelled this out, though...

这是我能找到的最“官方”的来源。直接来自Sun的Java教程,我会说这是官方认可的方式。如果JavaDocs拼写出来,我不会完全明白,不过......