如何在paintComponent之外进行绘画?
我有一个画布,它在 paintComponent()
方法中绘制所有内容。
是否可以在 paintComponent
外部进行绘制(无需调用 paintComponent
内的方法?)
如果可以,您将如何执行此操作?
I have a canvas that is drawing everything in a paintComponent()
method.
Is it possible to draw outside of paintComponent
(without calling a method within paintComponent
?)
If so how do you go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您的意思以及为什么需要它。例如,可以创建一个 BufferedImage、获取 Graphics2D 对象、Graphics.paint() 一切应该位于图像上,然后将图像放入
JLabel
中。但由于我不知道您想要实现什么(而不是您想要做),我不知道这个答案是否解决了未说明的问题。
It depends what you mean and why you need it. For example, it is possible to create a
BufferedImage
, get theGraphics2D
object,Graphics.paint()
everything that should be on the image, then drop the image into aJLabel
.But since I do not know what you are trying to achieve (as opposed to what you are trying to do) I cannot know if that answer solves the unstated problem.
我找到了如何解决这个问题。
我所做的就是使 JPanel 成为我的 JFrame 类的内部类。
在 JPanels PaintComponent 中,我让它从外部类调用一个方法,该方法通过传递 PaintComponents Graphics2D 对象来对图形进行一些更新。
这使我能够根据需要在 PaintComponent 的“外部”进行绘制。
I found out how to solve this issue.
What I did was make JPanel an inner class to my JFrame class.
In JPanels paintComponent I had it calling a method from the outer class which did some updating of the graphics, by passing paintComponents Graphics2D object.
This has allowed me to paint "outside" of paintComponent, just as I needed.