代码适用于java图形,但不适用于graphics2d
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JPanel
(它是JComponent
)只有一个
paintComponent(Graphics)
方法。 它没有带有签名paintComponent(Graphics2D)
的方法。重写
paintComponent(Graphics)
方法可以通过以下方式完成:但是,像下面这样使用
paintComponent(Graphics2D)
签名定义一个方法是合法的,但是它永远不会被调用,因为它不会覆盖JComponent
中定义的任何方法:JComponent
类(它是JPanel 的超类)的 Java API 规范
)有一个方法摘要,其中列出了属于该类的所有方法。有关 Swing 中绘画的更多信息;
JPanel
(which is a subclass ofJComponent
) only has apaintComponent(Graphics)
method. It does not have a method with the signaturepaintComponent(Graphics2D)
.Overriding the
paintComponent(Graphics)
method can be accomplished by the following: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 inJComponent
:The Java API specifications for the
JComponent
class (which is the superclass ofJPanel
) has a method summary which lists all the methods that are part of the class.More information about painting in Swing;