Java 在 JPanel 类之外绘制图像
我有一个扩展 JPanel 的 WorldManager 类,并具有:
public void Paint(Graphics g) {}
我想做的是拥有单独的类,例如具有自己的绘制方法的世界类,并且是能够像这样调用该类的 Paint 方法:
public void Paint(Graphics g) { world1.paint();英雄.paint(); }
I have a WorldManager class that extends a JPanel and has:
public void paint(Graphics g) {}
What I would like to do is have separate classes, like a world class with its own paint method and be able to just call that classes paint method like so:
public void paint(Graphics g) { world1.paint(); hero.paint(); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原则上,你的做法没有任何问题。
正如trashgod所指出的,您应该覆盖paintComponent方法而不是paint方法。
其原因在链接的文章中指出by trashgod:这样,
paintBorder()
和paintChildren()
方法就可以绘制边框和子元素组件,并且您可以自由地只考虑真实内容。这是一个例子:
那么,你的问题实际上是什么?
In principle, there is nothing wrong with your approach.
As trashgod noted, you should overwrite the paintComponent method instead of the paint method.
The reason for this is noted in the article linked by trashgod: this way, the
paintBorder()
andpaintChildren()
method can do their painting of the border and the child components, and you are free to think only about the real content.Here is an example:
So, what was your question, actually?