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.