I'm a complete noob in java. I still really don't know how to code. I am having a problem in my maze. I can now go from start(2) to end(3) but the problem is the icon for (start). I tried to print out the values of our maze (mazePlan) and it was fine. The only problem is the icon. The icons changed only after the recursive function is finished ( I used setIcons). I need to get those icon for start move. Thanks! i really need some help. Thanks!
class solveButton implements ActionListener{
private boolean goal;
public void actionPerformed(ActionEvent e1)
{
int i,j, a = 0,b = 0;
goal = false;
//Find 2/start
for(i = 0; i<10; i++){
for ( j = 0; j<10; j++){
if (getMazePlan()[i][j] == 2){
a= i; b = j;
}
}
}
function(a,b);
//print only
for (i=0;i<10;i++){
for(j = 0; j < 10; j++){
System.out.print(getMazePlan()[i][j] + " ");
}
System.out.println();
}
}
public void function(int y, int x){
int ctr = 0,b ,a;
//CHECKING FOR WALLS
//LEFT
if(x-1 >= 0 && x-1 <= 9){
if(getMazeBoolean()[y][x-1] == false)
ctr++;
}
//UP
if(y-1 >= 0 && y-1 <= 9){
if(getMazeBoolean()[y-1][x] == false)
ctr++;
}
//RIGHT
if(x+1 >= 0 && x+1 <= 9){
if(getMazeBoolean()[y][x+1] == false)
ctr++;
}
//DOWN
if(y+1 >= 0 && y+1 <= 9){
if(getMazeBoolean()[y+1][x] == false)
ctr++;
}
try{
Thread.sleep(1000);
}catch(Exception e){}
//print only
System.out.println("Move");
for (int i=0;i<10;i++){
for(int j = 0; j < 10; j++){
System.out.print(getMazePlan()[i][j] + " ");
}
System.out.println();
}
stack1.push(x);
stack1.push(y);
setMazePlan1(0, y, x);
maze[y][x].setIcon(end);
setMazeBoolean(true, y , x);
//LEFT
if(ctr > 0 && goal == false){
if(x-1 >= 0 && x-1 <= 9){
if(getMazeBoolean()[y][x-1] == false && getMazePlan()[y][x-1] != 3){
setMazePlan1(2, y, x-1);
function(y, x-1);
ctr--;
if(ctr != 0 && goal == false){
b = stack1.pop();
a = stack1.pop();
setMazePlan1(2, b , a);
stack1.push(a);
stack1.push(b);
try{
Thread.sleep(1000);
}catch(Exception e){}
setMazePlan1(0, b , a);
}
}
else if(getMazePlan()[y][x-1] == 3){
System.out.println("FINISH");
goal = true;
}
}
}
//UP
if(ctr > 0 && goal == false){
if(y-1 >= 0 && y-1 <= 9){
if(getMazeBoolean()[y-1][x] == false && getMazePlan()[y-1][x] != 3){
setMazePlan1(2, y-1, x);
function(y-1, x);
ctr--;
if(ctr != 0 && goal == false){
b = stack1.pop();
a = stack1.pop();
setMazePlan1(2, b , a);
stack1.push(a);
stack1.push(b);
try{
Thread.sleep(1000);
}catch(Exception e){}
setMazePlan1(0, b , a);
}
}
else if(getMazePlan()[y-1][x] == 3){
System.out.println("FINISH");
goal = true;
}
}
}
//RIGHT
if(ctr > 0 && goal == false){
if(x+1 >= 0 && x+1 <= 9){
if(getMazeBoolean()[y][x+1] == false && getMazePlan()[y][x+1] != 3 ){
setMazePlan1(2, y, x+1);
function(y, x+1);
ctr--;
if(ctr != 0 && goal == false){
b = stack1.pop();
a = stack1.pop();
setMazePlan1(2, b , a);
stack1.push(a);
stack1.push(b);
try{
Thread.sleep(1000);
}catch(Exception e){}
setMazePlan1(0, b , a);
}
}
else if(getMazePlan()[y][x+1] == 3){
System.out.println("FINISH");
goal = true;
}
}
}
//DOWN
if(ctr > 0 && goal == false){
if(y+1 >= 0 && y+1 <= 9){
if(getMazeBoolean()[y+1][x] == false && getMazePlan()[y+1][x] != 3){
setMazePlan1(2, y+1, x);
function(y+1, x);
ctr--;
if(ctr != 0 && goal == false){
b = stack1.pop();
a = stack1.pop();
setMazePlan1(2, b , a);
stack1.push(a);
stack1.push(b);
try{
Thread.sleep(1000);
}catch(Exception e){}
setMazePlan1(0, b , a);
}
}
else if(getMazePlan()[y+1][x] == 3){
System.out.println("FINISH");
goal = true;
}
}
}
//DEADEND
if(goal == false && ctr == 0){
b = stack1.pop();
a = stack1.pop();
setMazePlan1(0, b , a);
try{
b = stack1.pop();
a = stack1.pop();
stack1.push(a);
stack1.push(b);
setMazePlan1(2, b , a);
}
catch(Exception e){
}
try{
Thread.sleep(1000);
}catch(Exception e){}
//print only
System.out.println("Pop");
for (int i=0;i<10;i++){
for(int j = 0; j < 10; j++){
System.out.print(getMazePlan()[i][j] + " ");
}
System.out.println();
}
}
}
}
Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling
Thread.sleep(n)
implement a Swing Timer
for repeating tasks or a SwingWorker
for long running tasks.
See Concurrency in Swing for more details.
Without trying to decipher your hauge chunk of code, sounds like to need to force a redraw/paint after you have set the icon.
There isn't actually enough code here to know what to call repaint()...
You might also want to have a look at Java GUI apps / GUI threads (i.e. you shouldn't do all your "maze" stuff on the GUI thread if it can take a while)