Guten Abend die Herren,
ich weiß nicht wie ich mein Problem beschreiben soll, außer dass ich normalerweise solche Probleme nicht habe. Deshalb weiß ich nicht, ob es daran liegt, dass ich seit Kurzem mit JPanel und paintComponent() arbeite oder nicht...
Eigentlich möchte ich nur ein Objekt in einer Klasse von mehreren Methoden verändern lassen, was wiederum bedeutet, dass alle Methoden auf dieses Objekt zugreifen können müssen.
Nur irgendwie kapiert meine paintComponent()-Methode das nicht ...
Hab schon mit dem Debugger nachgeschaut, sobald der Compiler in paintComponent() springt, meint er, alle meine Objekte seien "null". Alle anderen Methoden können wie gewohnt auf das Objekt zugreifen.
Die if-Abfrage dort dient nur dazu da, dass die paintComponent() erst ganz durchgeführt werden kann, wenn ich auch dem Objekt in einer anderen Methode einen Wert zugewiesen habe, und in dieser die Bedingung wahr werden lassen habe... aber keines von beidem kriege ich hin :-(
ich weiß nicht wie ich mein Problem beschreiben soll, außer dass ich normalerweise solche Probleme nicht habe. Deshalb weiß ich nicht, ob es daran liegt, dass ich seit Kurzem mit JPanel und paintComponent() arbeite oder nicht...
Eigentlich möchte ich nur ein Objekt in einer Klasse von mehreren Methoden verändern lassen, was wiederum bedeutet, dass alle Methoden auf dieses Objekt zugreifen können müssen.
Nur irgendwie kapiert meine paintComponent()-Methode das nicht ...
Hab schon mit dem Debugger nachgeschaut, sobald der Compiler in paintComponent() springt, meint er, alle meine Objekte seien "null". Alle anderen Methoden können wie gewohnt auf das Objekt zugreifen.
Die if-Abfrage dort dient nur dazu da, dass die paintComponent() erst ganz durchgeführt werden kann, wenn ich auch dem Objekt in einer anderen Methode einen Wert zugewiesen habe, und in dieser die Bedingung wahr werden lassen habe... aber keines von beidem kriege ich hin :-(
Java:
import java.io.*;
public class Irrgartenspiel
{
public static void main(String[] args) throws FileNotFoundException, IOException
{
Player player1 = new Player();
player1.play();
}
}
Java:
import java.io.*;
public class Player
{
char[][] maze;
int[] startLoc;
int[] destLoc;
VisualPanel panel;
public Player() throws FileNotFoundException, IOException
{
Maze m = new Maze();
maze = m.getMaze();
startLoc = m.getStartLoc();
destLoc = m.getDestLoc();
}
public void play() throws FileNotFoundException, IOException
{
Tracking t = new Tracking(maze,startLoc,destLoc);
Visual v = new Visual();
int count = 0;
while (!t.hitDest())
{
t.checkPossDirect();
v.draw(t.getCurrLoc());
t.move();
count++;
} // end of while
System.out.println("Schritte: " + count);
}
}
Java:
import java.util.ArrayList;
public class Tracking
{
char[][] maze;
int[] currLoc;
int[] destLoc;
boolean[] possDirect = new boolean[4]; //0 = links, 1 = oben, 2 = rechts, 3 = unten
ArrayList<Location> moveList = new ArrayList<Location>();
int countBackTrack = 0;
public Tracking(char[][] maze, int[] startLoc, int[] destLoc)
{
this.maze = maze;
this.currLoc = startLoc;
this.destLoc = destLoc;
}
public boolean hitDest()
{
if (maze[currLoc[0]][currLoc[1]]=='Z')
{
return true;
} // end of if
else
{
return false;
} // end of if-else
}
public void checkPossDirect()
{
int i;
for (i=0;i<possDirect.length;i++)
{
possDirect[i] = true;
} // end of for
if (hitWall(currLoc[0]-1,currLoc[1]))
{
possDirect[0] = false;
} // end of if
if (hitWall(currLoc[0],currLoc[1]-1))
{
possDirect[1] = false;
} // end of if
if (hitWall(currLoc[0]+1,currLoc[1]))
{
possDirect[2] = false;
} // end of if
if (hitWall(currLoc[0],currLoc[1]+1))
{
possDirect[3] = false;
} // end of if
moveList.add(new Location(currLoc[0],currLoc[1]));
}
public boolean hitWall(int x, int y)
{
if (maze[x][y]=='-' || maze[x][y] == '|' || maze[x][y] == 'X')
{
return true;
} // end of if
else
{
return false;
} // end of if-else
}
public void move()
{
int i;
System.out.print("currLoc: ");
for (i=0;i<currLoc.length;i++)
{
System.out.print(currLoc[i]+" ");
} // end of for
System.out.println();
maze[currLoc[0]][currLoc[1]] = 'X';
int countPossDirects=0;
for (i=0;i<possDirect.length;i++)
{
if (possDirect[i])
{
countPossDirects++;
} // end of if
} // end of for
if (countPossDirects>0)
{
countBackTrack = 0;
int direct = (int)(Math.random()*4);
while (!possDirect[direct])
{
direct = (int)(Math.random()*4);
} // end of while
switch (direct)
{
case 0 : currLoc[0] = currLoc[0]-1;
break;
case 1 : currLoc[1] = currLoc[1]-1;
break;
case 2 : currLoc[0] = currLoc[0]+1;
break;
case 3 : currLoc[1] = currLoc[1]+1;
break;
default : System.out.println("dafuq?");
break;
} // end of switch
} // end of if
else
{
countBackTrack++;
if (countBackTrack<2)
{
moveList.remove(moveList.size()-1);
currLoc[0] = moveList.get(moveList.size()-1).getX();
currLoc[1] = moveList.get(moveList.size()-1).getY();
} // end of if
else
{
moveList.remove(moveList.size()-1);
moveList.remove(moveList.size()-1);
currLoc[0] = moveList.get(moveList.size()-1).getX();
currLoc[1] = moveList.get(moveList.size()-1).getY();
} // end of if-else
} // end of if-else
}
public Location getCurrLoc()
{
return moveList.get(moveList.size()-1);
}
}
Java:
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class Visual extends JFrame
{
Maze m;
char[][] maze;
String[] mazeLines;
VisualPanel panel = new VisualPanel();
public Visual() throws FileNotFoundException, IOException
{
m = new Maze();
maze = m.getMaze();
mazeLines = getMazeLines();
panel.setMaze(maze);
int i;
for (i=0;i<mazeLines.length;i++)
{
System.out.println("mazeLines["+i+"] " + mazeLines[i]);
} // end of for
Visual frame = new Visual("Irrgartenspiel");
frame.setVisible(true);
}
public Visual(String title)
{
super(title);
setBounds(10,10,300,300);
setLayout(null);
panel.setBounds(0,0,300,300);
add(panel);
}
public String[] getMazeLines()
{
int i,j,k=0;
String[] mL = new String[maze[0].length];
for (i=0;i<maze[0].length;i++)
{
mL[i] = "" + maze[0][i];
} // end of for
for (i=0;i<maze[0].length;i++)
{
for (j=0;j<maze.length-1;j++)
{
mL[k] = mL[k] + "" + maze[j+1][i];
} // end of for
k++;
} // end of for
return mL;
}
public void draw(Location loc)
{
int x = loc.getX();
int y = loc.getY();
int i;
char[] c = mazeLines[y].toCharArray();
c[x] = 'X';
String newLine = "" + c[0];
for (i=1;i<c.length;i++)
{
newLine = newLine + "" + c[i];
} // end of for
mazeLines[y] = newLine;
/*for (i=0;i<mazeLines.length;i++)
{
System.out.println(mazeLines[i]);
} // end of for*/
mazeLines = getMazeLines();
panel.setCurrLoc(loc);
panel.setCurrMaze();
repaint();
}
}
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class VisualPanel extends JPanel
{
char[][] maze;
char[][] currMaze;
String[] currMazeString;
Location loc;
int x = 0;
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (x!=0)
{
int i;
for (i=0;i<currMazeString.length;i++)
{
g.drawString(currMazeString[i],10,20*i);
} // end of for
} // end of if
System.out.println("x");
System.out.println(x);
}
public void setMaze(char[][] maze)
{
this.maze = maze;
x++;
}
public void setCurrMaze()
{
currMaze = maze;
int x = loc.getX();
int y = loc.getY();
currMaze[x][y] = 'X';
currMazeString = new String[currMaze[x].length];
int i,j;
String s = "" + currMaze[0][0];
for (i=0;i<currMaze[0].length;i++)
{
for (j=1;j<currMaze.length;j++)
{
currMazeString[i] = s + "" + currMaze[j][i];
s = currMazeString[i];
} // end of for
s = "" + currMaze[0][i];
} // end of for
}
public void setCurrLoc(Location loc)
{
this.loc = loc;
}
}
Java:
public class Location
{
int x;
int y;
public Location(int x, int y)
{
this.x = x;
this.y = y;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
}