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

发布于 2024-07-13 14:56:02 字数 252 浏览 9 评论 0原文

在 Paint 组件内。 它以g为参数,g可以是graphics或graphics2d。 该类扩展了 jpanel。 那么:

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

如果 g 是graphics,它可以工作,但如果它是graphics2d,它就不行。 它可以同时编译两者,但graphics2d不会改变背景颜色。 怎么会?

inside a paintcomponent. it takes g as parameter, and g can be graphics or graphics2d. the class extends jpanel. then:

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

if g is graphics it works, but if it's graphics2d it doesn't. it compiles with both, but graphics2d doesn't change the background color. how come?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2024-07-20 14:56:02

JPanel (它是 JComponent)只有一个 paintComponent(Graphics) 方法。 它没有带有签名 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.
}

JComponent(它是 JPanel 的超类)的 Java API 规范)有一个方法摘要,其中列出了属于该类的所有方法。

有关 Swing 中绘画的更多信息;

JPanel (which is a subclass of JComponent) only has a paintComponent(Graphics) method. It does not have a method with the signature paintComponent(Graphics2D).

Overriding the paintComponent(Graphics) method can be accomplished by the following:

public void paintComponent(Graphics g)
{
    // Do things.
}

However, defining a method with the signature with paintComponent(Graphics2D) like the following is legal, but it won't ever be called, as it is not overriding any method that is defined in 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.
}

The Java API specifications for the JComponent class (which is the superclass of JPanel) has a method summary which lists all the methods that are part of the class.

More information about painting in Swing;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文