代码适用于java图形,但不适用于graphics2d

代码适用于java图形,但不适用于graphics2d,java,graphics,java-2d,Java,Graphics,Java 2d,在一个组件内部。它以g为参数,g可以是图形,也可以是图形2D。该类扩展了jpanel。然后: super.paintComponent(g); this.setBackground( Color.BLACK ); 如果g是graphics,它就工作,但如果是graphics2d,它就不工作。它可以同时使用这两种颜色进行编译,但graphics2d不会更改背景颜色。为什么?(是的子类)只有一个方法。它没有签名为paintComponent(Graphics2D)的方法 覆盖paintCompon

在一个组件内部。它以g为参数,g可以是图形,也可以是图形2D。该类扩展了jpanel。然后:

super.paintComponent(g);
this.setBackground( Color.BLACK );
如果g是graphics,它就工作,但如果是graphics2d,它就不工作。它可以同时使用这两种颜色进行编译,但graphics2d不会更改背景颜色。为什么?

(是的子类)只有一个方法。它没有签名为
paintComponent(Graphics2D)
的方法

覆盖
paintComponent(Graphics)
方法可以通过以下步骤完成:

public void paintComponent(Graphics g)
{
    // Do things.
}
但是,使用
paintComponent(Graphics2D)
定义具有签名的方法(如以下所示)是合法的,但它永远不会被调用,因为它不会覆盖
JComponent
中定义的任何方法:

public void paintComponent(Graphics2D g)
{
    // Do things.
    // However, this method will never be called, as it is not overriding any
    // method of JComponent, but is a method of the class this is defined in.
}
(是
JPanel
的超类)有一个方法摘要,其中列出了属于该类的所有方法

更多关于秋千绘画的信息

    • (是的子类)只有一个方法。它没有签名为
      paintComponent(Graphics2D)
      的方法

      覆盖
      paintComponent(Graphics)
      方法可以通过以下步骤完成:

      public void paintComponent(Graphics g)
      {
          // Do things.
      }
      
      但是,使用
      paintComponent(Graphics2D)
      定义具有签名的方法(如以下所示)是合法的,但它永远不会被调用,因为它不会覆盖
      JComponent
      中定义的任何方法:

      public void paintComponent(Graphics2D g)
      {
          // Do things.
          // However, this method will never be called, as it is not overriding any
          // method of JComponent, but is a method of the class this is defined in.
      }
      
      (是
      JPanel
      的超类)有一个方法摘要,其中列出了属于该类的所有方法

      更多关于秋千绘画的信息