S
Spellsleeper
Gast
Arbeite an einem Projekt, Input-Klasse funzt wunderbar, nur gehen vor dem generieren des Rooms irgendwie alle Attribute des Objektes wieder auf default(sprich 0,null). Sieht hier jemand was verkehrt gelaufen ist. Bitte erleuchtet mich. PS(Das mit den Werten ist im Debugger gut sichtbar).
Reihenfolge: Main-Input-Room
Reihenfolge: Main-Input-Room
Java:
package main;
import data.Input;
import data.Room;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Input input=new Input();
Room room=input.getRoom(input);
for(int iy=0;iy<room.getyLength();iy++){
for(int ix=0;ix<room.getxLength();ix++){
room.roomMatrix[iy][ix].print();
}
System.out.println("");
}
}
}
Java:
package data;
import java.io.*;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Input {
private int xl, yl, rpx, rpy, gX1, gX2, gY1, gY2, barrierCount;
private int[][] barriers ;
public Input() {
JFrame frame = new JFrame();
frame.setIconImage(new ImageIcon("media/open_folder.png").getImage());
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame.setVisible(false);
JFileChooser jfc = new JFileChooser("C:/user/"
+ System.getProperty("user.name") + "/desktop");
jfc.setDialogTitle("Wählen der vorgegebenen txt-Datei");
jfc.setDialogType(JFileChooser.OPEN_DIALOG);
jfc.setFileFilter(new FileNameExtensionFilter("Textdateien", "txt"));
int state = jfc.showOpenDialog(frame);
if (state == JFileChooser.APPROVE_OPTION) {
new Input(jfc.getSelectedFile().getAbsolutePath());
}
}
public Input(String path) {
String thisLine;
try {
File file = new File(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
for (int i = 0; (thisLine = reader.readLine()) != null; i++) {
String[] sa = thisLine.split(" ");
for(String temp:sa){
temp.trim();
}
for (int temp = 0; temp < sa.length; temp++) {
int val = Integer.parseInt(sa[temp]);
// row 1
if (i == 0) {
if (temp == 0) {
xl = val;
} else if (temp == 1) {
yl = val;
}
}
// row 2
else if (i == 1) {
if (temp == 0) {
rpx = val;
} else if (temp == 1) {
rpy = val;
}
}
// row 3
else if (i == 2) {
switch (temp) {
case 0:
gX1 = val;
break;
case 1:
gX2 = val;
break;
case 2:
gY1 = val;
break;
case 3:
gY2 = val;
break;
}
}
// row 4
else if (i == 3) {
barrierCount = val;
barriers = new int[barrierCount][4];
}
// row 5
else if (i > 3) {
System.out.println(i-4+"\t"+temp);
barriers[i - 4][temp] = val;
}
}
}
reader.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"Fehler beim Einlesen der Datei!", "EinleseFehler",
JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
public Room getRoom(Input input) {
Room room = new Room();
room.generateRoom(input.xl, input.yl);
for (int i = 0; i < input.barrierCount; i++) {
room.generateBarrier(input.barriers[i][0], input.barriers[i][1],
input.barriers[i][2], input.barriers[i][3]);
}
room.setGate(input.gX1, input.gX2, input.gY1, input.gY2);
room.setRoboPos(input.rpx, input.rpy);
return room;
}
}
Java:
package data;
import java.util.ArrayList;
import java.util.Random;
import data.Field.Condition;
public class Room {
enum Direction {
NORTH, EAST, SOUTH, WEST
};
private int xLength, yLength, barrierCount;
public Position roboPos = null;
public Gate gate;
public Direction direction;
public Field[][] roomMatrix;
public ArrayList<Barrier> barriers = new ArrayList<Barrier>();
/**
*
* @param xLength
* @param yLength
* <p>
* Renerates a Room-Matrix with a the outer wall
* </p>
*/
public void generateRoom(int xLength,int yLength){
this.roomMatrix=new Field[yLength][xLength];
for(int i1=0;i1<yLength;i1++){
for (int i2=0;i2<xLength;i2++){
roomMatrix[i1][i2]=new Field();
if((i1==0||i1==yLength-1)||(i2==0||i2==xLength-1)){
roomMatrix[i1][i2].condition=Condition.BARRIER;
}else{
roomMatrix[i1][i2].condition=Condition.EMPTY;
}
roomMatrix[i1][i2].x=i2;
roomMatrix[i1][i2].y=i1;
}
}
}
/**
* @param xLength
* @param yLength
* <p>
* roomMatrix=Field[yLength][yLength]
* </p>
*/
public void setRoomMatrix(int xLength, int yLength) {
this.xLength = xLength;
this.yLength = yLength;
this.roomMatrix = new Field[yLength][xLength];
}
/**
* @param x
* @param y
* <p>
* Delets old Pos & sets new Robo-Pos
* </p>
*/
public void setRoboPos(int x, int y) {
if (roboPos != null) {
roomMatrix[roboPos.getY()-1][roboPos.getX()-1].condition = Field.Condition.EMPTY;
}
this.roboPos.setX(x);
this.roboPos.setY(y);
roomMatrix[y-1][x-1].condition = Field.Condition.ROBO;
}
/**
* @param x1
* @param y1
* @param x2
* @param y2
* <p>
* Generates a Barrier in the Matrix. If Nummer 1 > Number2 they
* will be changed automatically.
* </p>
*/
public void generateBarrier(int x1, int y1, int x2, int y2) {
if (x1 > x2) {
int temp = x1;
x1 = x2;
x2 = temp;
}
if (y1 > y2) {
int temp = y1;
y1 = y2;
y2 = temp;
}
barriers.add(new Barrier(x1, x2, y1, y2));
barrierCount++;
for (int i1 = y1; i1 <= y2; i1++) {
for (int i2 = x1; i2 <= x2; i2++) {
roomMatrix[i1-1][i2-1].condition = Field.Condition.BARRIER;
}
}
}
/**
*
* @param x1
* @param x2
* @param y1
* @param y2
* <p>
* Generates the Room-Gate
* </p>
*/
public void setGate(int x1, int x2, int y1, int y2) {
gate = new Gate(x1, x2, y1, y2);
for (int i1 = x1-1; i1 < x2; i1++) {
for (int i2 = y1-1; i2 < y2; i2++) {
roomMatrix[i2][i1].condition = Field.Condition.GATE;
}
}
}
public void setDirection(Direction direction) {
switch (this.direction) {
case NORTH:
if (direction != Direction.SOUTH) {
this.direction = direction;
} else {
generateRandomDirection(direction);
}
;
case WEST:
if (direction != Direction.EAST) {
this.direction = direction;
} else {
generateRandomDirection(direction);
}
;
case EAST:
if (direction != Direction.WEST) {
this.direction = direction;
} else {
generateRandomDirection(direction);
}
;
case SOUTH:
if (direction != Direction.NORTH) {
this.direction = direction;
} else {
generateRandomDirection(direction);
}
;
}
this.direction = direction;
}
/**
*
* @param direction
* @return <p>
* Generates safety Random-Direction-Change
* </p>
*/
private Direction generateRandomDirection(Direction direction) {
double d;
d = Math.random() * 2;
if (d < 1.0) {
int temp = direction.ordinal() - 1;
if (temp < 0) {
temp = 3;
}
direction = Direction.values()[(direction.ordinal() - 1)];
} else {
int temp = direction.ordinal() + 1;
if (temp > 3) {
temp = 0;
}
direction = Direction.values()[(temp)];
}
return direction;
}
public int getxLength(){
return xLength;
}
public int getyLength(){
return yLength;
}
}