【问题标题】:Difference between Graphics and Graphics2D?Graphics 和 Graphics2D 的区别?
【发布时间】:2013-10-21 02:41:26
【问题描述】:

GraphicsGraphics2D 有什么区别?
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
}

【问题讨论】:

  • 它实际上是带有AffineTransform类方法的Graphics类。

标签: java swing graphics graphics2d


【解决方案1】:

Graphics 本身是一个abstract class,因此您不能创建它的实例。它只定义了一些接口和一些功能,因此可以被其他类扩展。

因此,即使是paintComponent 中用作参数的Graphics g,也不仅仅是Graphics。标准的java库只有两个扩展类:DebugGraphics, Graphics2D,所以你使用的Graphics gGraphics2D的实例存储在Graphics g中。

如果不是,Graphics2D g2 =(Graphics2D)g; 行将以错误结尾。

【讨论】:

    猜你喜欢
    • 2020-07-21
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 2012-05-27
    • 2010-12-25
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多