无法让 JFrame 刷新图像

发布于 2024-10-09 22:11:53 字数 895 浏览 3 评论 0原文

嘿,所以我正在开发一个程序,出于调试目的,我试图让程序截取部分显示的屏幕截图。我想更新显示器,但我似乎无法让它工作。我确信这是一个简单的问题,但我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乙白 2024-10-16 22:11:53

您每次通过循环都会创建并添加一个新的JLabel。由于您要更改组件树的结构,因此需要在框架的内容窗格上调用revalidate

更好的解决方案是仅更改单个 JLabel 上的图像。创建一个标签,添加它,然后在循环中使用 JLabel.setIconrepaint

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 call revalidate 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 and repaint.

述情 2024-10-16 22:11:53

正如@RD所指出的, createScreenCapture() 如果小程序未签名,将抛出 SecurityException。在事件调度线程上休眠可能会阻塞更新。此示例在拖动鼠标时捕获屏幕图像;它的 BufferedImage 会“记住”最后捕获的图​​像。

As @RD notes, createScreenCapture() will throw a SecurityException 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; its BufferedImage "remembers" the last image captured.

倒数 2024-10-16 22:11:53

据我所知,Java 中的 repaint() 确实应该被称为 invalidate() ——它实际上并没有重新绘制窗口;它只是一个窗口。它只会使其无效,以便操作系统下次有机会重新绘制它。我没有仔细查看你的代码,但我认为这可能是问题所在。我不确定如何强制重绘,但一个想法是从函数返回,然后让一个计时器中断你并绘制它——这样,操作系统将有机会绘制窗口。

From what I remember, repaint() in Java should really have been called invalidate() -- 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文