0

I've been working on some simple Java code with the intention of making images appear on-screen. I've figured it out, but strangely, the code only works on the second+ attempt of running it. The code is as follows:

import javax.swing.*;

public class A3011A {

    public static JFrame frame;
    public static int delay;

    public static void main(String[] args) {
        CreateWindow(800, 600, "Frame");

        ImageIcon img = new ImageIcon("H:/Intro to Programming/Semester Project/duck.png");
        AddImage(50, 0, img);

        AddLabel(0, 0, "top left");
        AddLabel(300, 300, "another label");
    }

    public static void CreateWindow(int x, int y, String name) {
        frame = new JFrame(name);
        frame.setSize(x, y);

        frame.setLayout(null);

        frame.setVisible(true);
    }

    public static void AddLabel(int x, int y, String text) {
        JLabel label = new JLabel(text);

        label.setSize(300, 13);
        label.setLocation(x, y);

        frame.add(label);
    }

    public static void AddImage(int x, int y, ImageIcon image) {
        JLabel label = new JLabel(image);

        label.setSize(image.getIconWidth(), image.getIconHeight());
        label.setLocation(x, y);

        frame.add(label);
    }

}

It is worth noting that the following line:

ImageIcon img = new ImageIcon("H:/Intro to Programming/Semester Project/duck.png");

seems to be a source of the problem. If this line is removed, the program works on the first run attempt. Is there anything obvious sticking out here?

There are no compiler errors/exceptions when the program is run. The only thing wrong is the window; nothing appears in it on the first run. On the second run or after, everything appears in the window.

EvilLemons
  • 13
  • 3
  • What happens when your code doesn't work, do you get an Exception ? – Arnaud Sep 28 '16 at 13:34
  • The window appears as expected with no errors, but nothing appears in the window. For instance, I have two text labels being placed and those lines are not problematic. On the first run, neither the text nor the image appears, just the JFrame. On the second run, it works as intended. – EvilLemons Sep 28 '16 at 13:36
  • Make sure that your source image doesn't get modified by anything, and also if `H` is a "network drive", try copying the image on your own computer, and change the path accordingly (maybe to a path without spaces in it, just in case). – Arnaud Sep 28 '16 at 13:39
  • Any exception in console? – talex Sep 28 '16 at 13:43
  • H: is a network drive, and that was a great thought. However, the same problem persists. This is a school project so if we can't find a solution, we might have to use Graphics and paint(). – EvilLemons Sep 28 '16 at 13:47
  • draw the sequence of events as they happen and you will know when setVisible occurs - why it happens with the icons and doesnt happen without? I don know and I dont care I consider icons second rate citizens – gpasch Sep 28 '16 at 17:11

0 Answers0