Java Graphics 和 Graphics2D 之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19344878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Graphics and Graphics2D?
提问by Samiey Mehdi
What is difference between Graphicsand Graphics2D?
Whether Graphics2D is extend of Graphics?
Graphics和Graphics2D 有什么区别?
Graphics2D 是否是 Graphics 的扩展?
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawRect(25, 25, 20, 20); //use Graphics to paint rectangle
Graphics2D g2 =(Graphics2D)g;
g2.drawRect(0, 0, 20, 20); // use Graphics2D to paint rectangle
}
采纳答案by libik
Graphics itself is an abstract class
, therefore you cant create its instance. It only defines some interface and some functionality, so it can be extended by other class.
图形本身是一个abstract class
,因此您不能创建它的实例。它只定义了一些接口和一些功能,所以它可以被其他类扩展。
So even this Graphics g
, which is used as parameter in paintComponent
, is not only Graphics
. The standard java library has only two extended class : DebugGraphics, Graphics2D
, so the Graphics g
you are using is Graphics2D
instance stored in Graphics g
.
因此,即使Graphics g
用作参数 in 的this也paintComponent
不仅仅是Graphics
. 标准 Java 库只有两个扩展类:DebugGraphics, Graphics2D
,因此Graphics g
您使用的是Graphics2D
存储在Graphics g
.
If it is not, the line Graphics2D g2 =(Graphics2D)g;
would end with an error.
如果不是,该行Graphics2D g2 =(Graphics2D)g;
将以错误结束。