无法让 JFrame 刷新图像
嘿,所以我正在开发一个程序,出于调试目的,我试图让程序截取部分显示的屏幕截图。我想更新显示器,但我似乎无法让它工作。我确信这是一个简单的问题,但我对 Java Applet 的经验非常少。
这是我遇到问题的部分:
...
Thread.sleep(5000);
try {gb = new GameBoard(frame.getBounds());}
catch(Exception e){System.out.println("Error.");} // Make "gameboard" Object
while (true)
{
Thread.sleep(1000);
gb.grabImage(); // use java.awt.Robot's createScreenCapture()
ImageIcon icon = new ImageIcon(gb.image()); // wrap the image
JLabel label = new JLabel(icon, JLabel.CENTER);
frame.getContentPane().add(label,BorderLayout.EAST); //display the image (works)
//JOptionPane.showMessageDialog(null, label, "icon", -1);
label.repaint(); //update the display??
frame.repaint();
frame.getContentPane().repaint();
}
正如我所说,图像会出现,并且如果我更改小程序大小,则会创建新图像,但我需要不断变化的图像。
提前致谢!
Hey, so I'm working on a program and for debugging purposes, I'm trying to get the program to take a screenshot of a part of the display. I want to have the display updated, but I can't seem to get it to work. I'm sure it's a simple issue, but my experience with Java Applets is very small.
Here's the part that I'm having issues with:
...
Thread.sleep(5000);
try {gb = new GameBoard(frame.getBounds());}
catch(Exception e){System.out.println("Error.");} // Make "gameboard" Object
while (true)
{
Thread.sleep(1000);
gb.grabImage(); // use java.awt.Robot's createScreenCapture()
ImageIcon icon = new ImageIcon(gb.image()); // wrap the image
JLabel label = new JLabel(icon, JLabel.CENTER);
frame.getContentPane().add(label,BorderLayout.EAST); //display the image (works)
//JOptionPane.showMessageDialog(null, label, "icon", -1);
label.repaint(); //update the display??
frame.repaint();
frame.getContentPane().repaint();
}
As I said, the image appears, and will create new ones if I change the Applet size, but I need a constantly changing image.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您每次通过循环都会创建并添加一个新的
JLabel
。由于您要更改组件树的结构,因此需要在框架的内容窗格上调用revalidate
。更好的解决方案是仅更改单个 JLabel 上的图像。创建一个标签,添加它,然后在循环中使用
JLabel.setIcon
和repaint
。You are creating and adding a new
JLabel
each time through the loop. Because you are changing the structure of the component tree you'll need to callrevalidate
on the frame's content pane.A better solution would be to just change the image on a single JLabel. Create one label, add it, then in your loop use
JLabel.setIcon
andrepaint
.正如@RD所指出的,
createScreenCapture()
如果小程序未签名,将抛出SecurityException
。在事件调度线程上休眠可能会阻塞更新。此示例在拖动鼠标时捕获屏幕图像;它的 BufferedImage 会“记住”最后捕获的图像。As @RD notes,
createScreenCapture()
will throw aSecurityException
if the applet is not signed. Sleeping on the event dispatch thread may be blocking updates. This example captures a screen image as the mouse is dragged; itsBufferedImage
"remembers" the last image captured.据我所知,Java 中的
repaint()
确实应该被称为invalidate()
——它实际上并没有重新绘制窗口;它只是一个窗口。它只会使其无效,以便操作系统下次有机会重新绘制它。我没有仔细查看你的代码,但我认为这可能是问题所在。我不确定如何强制重绘,但一个想法是从函数返回,然后让一个计时器中断你并绘制它——这样,操作系统将有机会绘制窗口。From what I remember,
repaint()
in Java should really have been calledinvalidate()
-- it doesn't actually repaint the window; it only invalidates it so it can be repainted by the OS at the next opportunity. I didn't look carefully at your code, but I think this might be the issue. I'm not sure how to force a redraw, but an idea would be to return from the function, then have a timer interrupt you and paint it then -- that way, the OS will have a chance to paint the window.