更改 JPanel 默认图形
当我有JPanel
时,它有默认的图形,它被传递给paint(Graphics g)
和类似的函数。我可以以某种方式将默认图形
切换为我自己的吗?来自 JPanel 类
之外?我正在寻找类似 JPanel.setGraphics(Graphics g)
的内容。谢谢。
when I have JPanel
, it has it´s default Graphics which is passed to paint(Graphics g)
and similiar function. Can I somehow switch that default Graphics
for my own? From outside of the JPanel class
? I am looking for something like JPanel.setGraphics(Graphics g)
. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可以,但您可以重写其
paintComponent
方法,将作为参数传递给Graphics2D
的Graphics
对象进行强制转换,并在其上绘制您想要的任何内容。No, but you may override its
paintComponent
method, cast theGraphics
object passed as argument toGraphics2D
, and draw whatever you want on it.据我所知,控制传递哪个 Graphics 对象的唯一方法是启用调试图形选项。这是通过调用
JComponent.setDebugGraphicsOptions(int)
来完成的,它将用javax.swing.DebugGraphics
的实例替换原始 Graphics 对象。DebugGraphics
的实例化是硬编码在JComponent
类的getGraphics
方法中的,因此我认为这里没有办法使用您自己的实现(除了使用JVM 的工具来重写代码,就像模拟库所做的那样)。As far as I know, the only way to control which Graphics object is passed around is to enable the debug graphics option. This is done by calling
JComponent.setDebugGraphicsOptions(int)
, which will replace the original Graphics object by an instance ofjavax.swing.DebugGraphics
.The instanciation of
DebugGraphics
is hardcoded in the methodgetGraphics
of classJComponent
, so I see no way to use your own implementation here (other than using the instrumentation of the JVM to rewrite the code, as mock libraries do).