简体   繁体   English

如何让JWindow立即绘制

[英]How to get JWindow to immediately draw

How do I get a JWindow to immediately draw? 如何获得JWindow立即绘制? In my example below there is a FULL 2 seconds between the window displaying and drawing as measured from the log output "done" to "painting jwindow". 在下面的示例中,从显示日志输出“完成”到“绘制jwindow”,在窗口显示和绘制之间有整整2秒钟的时间。 When the window is first displayed it is empty. 第一次显示窗口时,它为空。 Two seconds later, the image "splash-screen.png" is displayed. 两秒钟后,显示图像“ splash-screen.png”。

I do not want to use the built-in splash screen support in Java. 我不想在Java中使用内置的初始屏幕支持。 It has problems. 它有问题。 Please, let's not get into that. 拜托,让我们不要陷入困境。 I already spent days researching it... it doesn't work for my case (not fully described here). 我已经花了几天时间研究它……它不适用于我的情况(此处未完整描述)。

I have run this code many times in succession to see if the delay is reduced... it remains at 2 seconds... very consistently. 我已经连续多次运行此代码,以查看延迟是否减少了……它保持在2秒……非常一致。 I'm running on macOS 10.14.1 and JDK 10.0.2. 我在macOS 10.14.1和JDK 10.0.2上运行。

Here is my SSCCE: 这是我的SSCCE:

import java.util.logging.Logger;
import javax.swing.JWindow;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.BorderLayout;

public class MyApp {
    private final static Logger LOG = Logger.getLogger(MyApp.class.getName());
    public static void main(String args[]) throws Exception {
        final JWindow[] startupScreen = {null};
        SwingUtilities.invokeAndWait(() -> {
            try {
                Thread.currentThread().setPriority(10);
                LOG.info("edt thread priority: "+Thread.currentThread().getPriority());
                startupScreen[0] = new JWindow() {
                    @Override
                    public void paint(Graphics g) {
                        super.paint(g);
                        LOG.info("painting jwindow");
                    }
                };
                /*
                LOG.info("set layout");
                startupScreen[0].setLayout(new BorderLayout());
                */
                LOG.info("add splash image");
                startupScreen[0].add(
                    new JLabel(new ImageIcon(
                        MyApp.class.getResource("splash-screen.png")
                    )), BorderLayout.CENTER);
                LOG.info("pack");
                startupScreen[0].pack();
                LOG.info("set relative location");
                startupScreen[0].setLocationRelativeTo(null);
                LOG.info("set visible");
                startupScreen[0].setVisible(true);
                startupScreen[0].repaint();
                startupScreen[0].revalidate();
                LOG.info("done");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
    }
}

Likely bottleneck: 可能的瓶颈:

startupScreen[0].add(new JLabel(new ImageIcon(
        MyApp.class.getResource("splash-screen.png"))), 
        BorderLayout.CENTER);

or more specifically, here: 或更具体地说,在这里:

MyApp.class.getResource("splash-screen.png")

as you're reading in an uncompressed and possibly large .png image resource. 当您在读取未压缩的可能很大的.png图像资源时。 Solutions include not requiring the image or reading in a smaller compressed .jpg image. 解决方案包括不需要图像或读取较小的压缩的.jpg图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM