Beau Beau - 8 days ago 4
Java Question

Java: while loop freezes program

I'm making a game and i need to update the JProgressBar every 3 seconds. To do that i use a while loop. The problem is that my program freezes becuse of the while loop (i read it in other questions, they didn't help me to solve this). I don't know how to solve it. Here is my code:

public static void city (String[] args){
//loading some of the saveData (not all of it is made yet)
try{
File saveDataFile = new File("save.dat");
Scanner saveDataSc = new Scanner(saveDataFile);

int power = saveDataSc.nextInt();
int people = saveDataSc.nextInt();
int food = saveDataSc.nextInt();
int wood = saveDataSc.nextInt();
int iron = saveDataSc.nextInt();
int stone = saveDataSc.nextInt();

saveDataSc.close();

} catch (FileNotFoundException ex){}

frame.remove(introImage4);
frame.remove(startingScreen);
frame.add(background1);

//background properties
background1.setLayout(null);

//sideBar properties
sideBar.setLayout(null);
sideBar.setBounds(700,0,200,600);

//button properties
AltarListB.setBounds(50,20,100,100);
InventortyB.setBounds(25,515,150,20);
ArmyB.setBounds(25,545,150,20);
MessagesB.setBounds(25,485,150,20);
MissionsB.setBounds(25,455,150,20);
MapB.setBounds(117,170,65,30);
FieldB.setBounds(18,170,65,30);
TownB.setBounds(68,135,65,30);

//image properties
underline1.setBounds(0,205,200,31);
underline2.setBounds(0,426,200,24);

//prograssbar properties
powerProg.setMinimum(0);
peopleProg.setMinimum(0);
woodProg.setMinimum(0);
foodProg.setMinimum(0);
ironProg.setMinimum(0);
stoneProg.setMinimum(0);

powerProg.setMaximum(500);
peopleProg.setMaximum(500);
woodProg.setMaximum(500);
foodProg.setMaximum(500);
ironProg.setMaximum(500);
stoneProg.setMaximum(500);


powerProg.setStringPainted(true);
peopleProg.setStringPainted(true);
woodProg.setStringPainted(true);
foodProg.setStringPainted(true);
ironProg.setStringPainted(true);
stoneProg.setStringPainted(true);

powerProg.setValue(power);
peopleProg.setValue(people);
woodProg.setValue(wood);
foodProg.setValue(food);
ironProg.setValue(iron);
stoneProg.setValue(stone);

powerProg.setString("Power: " + power + "/" + "maxPower");
peopleProg.setString("People: " + people + "/" + "maxPeople");
woodProg.setString("Wood: " + wood + "/" + "maxWood");
foodProg.setString("Food: " + food + "/" + "maxFood");
ironProg.setString("Iron: " + iron + "/" + "maxIron");
stoneProg.setString("Stone: " + stone + "/" + "maxStone");

powerProg.setBounds(14,241,170,20);
peopleProg.setBounds(14,273,170,20);
woodProg.setBounds(14,305,170,20);
foodProg.setBounds(14,337,170,20);
ironProg.setBounds(14,369,170,20);
stoneProg.setBounds(14,401,170,20);

//adding stuff
sideBar.add(AltarListB);
sideBar.add(InventortyB);
sideBar.add(ArmyB);
sideBar.add(MessagesB);
sideBar.add(MissionsB);
sideBar.add(MapB);
sideBar.add(FieldB);
sideBar.add(TownB);
sideBar.add(underline1);
sideBar.add(underline2);
sideBar.add(powerProg);
sideBar.add(peopleProg);
sideBar.add(woodProg);
sideBar.add(foodProg);
sideBar.add(ironProg);
sideBar.add(stoneProg);

background1.add(sideBar);


background1.revalidate();
background1.repaint();
frame.revalidate();
frame.repaint();
resourceLoader(new String[] {"a","b","c"});
}

public static void resourceLoader (String[] args){
while(true){
try{
File saveDataProgFile = new File("save.dat");
Scanner saveDataProgSc = new Scanner(saveDataProgFile);

power = saveDataProgSc.nextInt();
people = saveDataProgSc.nextInt();
food = saveDataProgSc.nextInt();
wood = saveDataProgSc.nextInt();
iron = saveDataProgSc.nextInt();
stone = saveDataProgSc.nextInt();

saveDataProgSc.close();

powerProg.setValue(power);
peopleProg.setValue(people);
woodProg.setValue(wood);
foodProg.setValue(food);
ironProg.setValue(iron);
stoneProg.setValue(stone);

background1.revalidate();
background1.repaint();
frame.revalidate();
frame.repaint();

saveDataProgSc.reset();

try{
Thread.sleep(3000);
} catch(InterruptedException e){
Thread.currentThread().interrupt();
}
} catch (FileNotFoundException ex){}
}
}


Can you please help me with this?

Answer

You should run your loop in an own Thread:

        new Thread( new Runnable() {
            @Override
            public void run()
            {
                resourceLoader (null);
            }}).start();

BTW: If you do not use the "String[] args" in the method there is no reason to have it declared in the method.

Comments