0

I am trying to make a game that calls a thread that has a while loop in it. I am using a JFrame that basically switches screens when you press the button. What I'm trying to get, is when you press the button, it draws the tiles and then calls the thread. But when I press the button, it just freezes. It stays on the screen where the button is pressed and doesn't change. Note that it DOES change if I don't call the thread. Also I have tried changing the position of the while loop and that did not work either.

The draw script:

void loadLevel(String level) {
    panel.remove(searchTA);
    panel.remove(titleL);
    panel.remove(search2B);
    panel.repaint();

    this.level = level;

    int x = 160;
    int y = 140;

    int tile = 0;
    int line = 0;

    for (int i = 0; i < 13; i++) {
        for (int j = 0; j < 13; j++) {
            String formula = (String
                    .valueOf(readFile("resources/Base Levels/Back Layer/" + level, line).charAt(tile))
                    + String.valueOf(readFile("resources/Base Levels/Front Layer/" + level, line).charAt(tile)));
            if (formula.equals("00")) {
                try {
                    BufferedImage img;
                    BufferedImage c;
                    img = ImageIO.read(clear);
                    c = resize(img, 50, 50);
                    JLabel lb = new JLabel(new ImageIcon(c));
                    lb.setLocation(x, y);
                    lb.setSize(50, 50);
                    panel.add(lb);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (formula.equals("01")) {
                try {
                    BufferedImage img;
                    BufferedImage c;
                    img = ImageIO.read(goal);
                    c = resize(img, 50, 50);
                    JLabel lb = new JLabel(new ImageIcon(c));
                    lb.setLocation(x, y);
                    lb.setSize(50, 50);
                    panel.add(lb);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (formula.equals("02")) {
                try {
                    BufferedImage img;
                    BufferedImage c;
                    img = ImageIO.read(wall);
                    c = resize(img, 50, 50);
                    JLabel lb = new JLabel(new ImageIcon(c));
                    lb.setLocation(x, y);
                    lb.setSize(50, 50);
                    panel.add(lb);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            x = x + 50;
            tile++;
        }
        tile = 0;
        line++;
        x = 160;
        y = y + 50;
    }

    play.run();
}

The Thread:

public Thread play = new Thread(new Runnable() {
    public void run() {

        panel.revalidate();
        panel.repaint();

        String tileOn = new String();

        String[] tiles = new String[169];

        int tile = 0;

        for (int i = 1; i < 14; i++) {
            for (int b = 1; b < 14; b++) {

                tiles[tile] = String
                        .valueOf(readFile("resources/Base Levels/Back Layer/" + level, i - 1).charAt(b - 1))
                        + String.valueOf(readFile("resources/Base Levels/Front Layer/" + level, i - 1).charAt(b - 1));

                tile++;
            }
        }

        while (!tileOn.equals("01")) {

        }

    }
});

And when I say freeze, I mean freeze. Not even pressing the X in the top right of the JFrame to close it works. I have to go back into Eclipse (IDE) and stop the script. Also, I know the while loop is causing the problem because I tried removing the while loop and it worked.

Potato
  • 49
  • 2
  • 9
  • 2
    Possibly because calling `run()` doesn't launch a thread, `start()` does. – markspace Sep 18 '17 at 21:14
  • How does `tileon` change its value? – VHS Sep 18 '17 at 21:14
  • Of course it will freeze, there is no exit condition for the while loop if you are not on tile "01", put code that will eventually make that condition false to exit the while loop – Tyler Sep 18 '17 at 21:14
  • @markspace, yes using start() worked. Thank you. Can you make it an answer? – Potato Sep 18 '17 at 21:21
  • I think it's too much "RTFM" to make a good answer. Let's let a mod close this as a duplicate. (Admittedly this is an easy mistake to make. I did exactly the same thing when I was learning Java.) – markspace Sep 18 '17 at 21:21

2 Answers2

1

Your problem is a combination of the problem mentioned in the answser of Johny Rathbone together with the comment on that answser.

In your while loop you do while (!tileOn.equals("01")) just after initializing it as a new String. Thus this will be an andless loop. This however should be no problem since you try to run it on a background-thread.

However by using play.run() instead of play.start(), you run this on the main thread.

The difference with these 2 methods is that start() actually starts a new thread and executes the run() method on it, while calling run() will execute the run() method on the main thread.

So if you change play.run() to play.start() it should unfreeze

Chris Witteveen
  • 495
  • 2
  • 7
0
    while (!tileOn.equals("01")) {

This line is your only reference to 'tileOn', apart from when you call it as a new String. As it isn't equal to "01" there, then this will always be true. And as you don't have anything in the loop, then the while loop will always run, because there's nothing in that loop to break that conditional.

  • 1
    Disagree. I think he's intending to launch that loop in a background thread, and an infinite loop there won't actually freeze his program. His error is more likely that he's calling `run()` instead of `start()`. – markspace Sep 18 '17 at 21:15