Sam*_*hdi 9 java graphics swing graphics2d
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
}
Run Code Online (Sandbox Code Playgroud)
lib*_*bik 16
图形本身就是一个abstract class,因此你无法创建它的实例.它只定义了一些接口和一些功能,因此可以通过其他类扩展.
因此,即使这个Graphics g用作参数paintComponent,也不仅如此Graphics.标准的java库只有两个扩展类:DebugGraphics, Graphics2D所以Graphics g你使用的是Graphics2D存储的实例Graphics g.
如果不是,该行Graphics2D g2 =(Graphics2D)g;将以错误结束.