I'm working on a basic program that creates a JFrame and adds a JPanel to it that contains an image. It works, however for some reason there is a slight delay (just a couple of seconds) where the screen is blank before the image is added. Why is this? and how can I fix it? Here is the code:
Frame Class:
public class Frame extends JFrame {
public Frame() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JFrame loader = new JFrame();
loader.setSize(screenSize.width / 3, screenSize.height / 3);
loader.add(new LaunchPanel(screenSize.width, screenSize.height));
loader.setLocationRelativeTo(null);
loader.setResizable(false);
loader.setUndecorated(true);
loader.repaint();
loader.setVisible(true);
}
}
Launch Panel Class:
public class LaunchPanel extends JPanel{
public LaunchPanel(int w, int h) {
BufferedImage image = null;
try {
image = ImageIO.read(new File("myImagePath.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
this.setSize(w, h);
Image scaledImage = image.getScaledInstance(this.getWidth() / 3, this.getHeight() / 3,Image.SCALE_SMOOTH);
JLabel label = new JLabel(new ImageIcon(scaledImage));
this.add(label);
label.setLocation(0, 0);
}
}