有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

super.paintComponent(g);
this.setBackground( Color.BLACK );

如果g是graphics,它就可以工作,但是如果它是graphics2d,它就不能工作。它可以同时使用这两种图形进行编译,但是graphics2d不会改变背景色。为什么


共 (1) 个答案

  1. # 1 楼答案

    ^{}(是^{}的子类)只有一个^{}方法。它没有签名为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.
    }
    

    Java API specifications for the ^{} class(是JPanel的超类)有一个方法摘要,其中列出了属于该类的所有方法

    更多关于秋千绘画的信息