Commit d33a3e05 authored by Stefan Michel's avatar Stefan Michel

einzelne schritte werden jetzt gemacht, pause und stopp funktionieren noch nicht !

parent 487fe05c
......@@ -7,6 +7,7 @@ import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import model.Terrain;
import model.ThreadManager;
import view.MainViewCreationFX;
import javax.sound.sampled.AudioInputStream;
......@@ -22,6 +23,7 @@ public class Main extends Application {
private HamsterController hamsterController;
public static final List<String> activePrograms = new ArrayList<>();
private IOController ioController;
ThreadManager threadManager;
public static void main(String[] args) {
launch(args);
......@@ -32,10 +34,11 @@ public class Main extends Application {
TerrainController terrainController = new TerrainController(terrain);
IOController ioController = new IOController(terrain);
HamsterController hamsterController = new HamsterController(terrain);
ThreadManager threadManager = new ThreadManager(terrain, ioController);
terrain.setName(name);
Stage newStage = new Stage();
Scene scene = new Scene(new MainViewCreationFX(terrain, terrainController, ioController, hamsterController), 1000, 500);
Scene scene = new Scene(new MainViewCreationFX(terrain, terrainController, ioController, hamsterController, threadManager), 1000, 500);
newStage.setScene(scene);
newStage.setTitle(terrain.getName());
newStage.show();
......@@ -51,6 +54,7 @@ public class Main extends Application {
terrainController = new TerrainController(terrain);
ioController = new IOController(terrain);
hamsterController = new HamsterController(terrain);
threadManager = new ThreadManager(terrain, ioController);
//TEST-INIT
terrain.setHamsterPostion(0, 0);
......@@ -61,7 +65,7 @@ public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(new MainViewCreationFX(terrain, terrainController, ioController, hamsterController), 1000, 500);
Scene scene = new Scene(new MainViewCreationFX(terrain, terrainController, ioController, hamsterController, threadManager), 1000, 500);
primaryStage.setScene(scene);
primaryStage.setTitle(terrain.getName());
primaryStage.show();
......
......@@ -30,9 +30,4 @@ public class HamsterController {
public int getHamsterCorns() {
return terrain.getHamsterCorns();
}
public void start() {
terrain.getHamster().start();
}
}
......@@ -46,8 +46,7 @@ public class IOController {
Files.createDirectory(dir);
Files.createFile(file);
} catch (FileAlreadyExistsException x) {
err.format("file named %s" +
" already exists%n", file);
// in unserem Fall nicht so schlimm
} catch (IOException x) {
// Some other sort of failure, such as permissions.
err.format("createFile error: %s%n", x);
......@@ -64,6 +63,7 @@ public class IOController {
public void compile(String name) {
mainViewCreationFX.setFooterText("Kompiliere..");
saveFile(name);
ByteArrayOutputStream err = new ByteArrayOutputStream();
boolean success =
......@@ -82,8 +82,7 @@ public class IOController {
} catch (ClassNotFoundException | MalformedURLException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
new Alert(Alert.AlertType.INFORMATION, "Kompilieren erfolgreich! \nAktueller Hamster: " +
terrain.getHamster().getClass().getSimpleName()).showAndWait();
mainViewCreationFX.setFooterText("Kompilieren erfolgreich");
}
}
......
......@@ -3,7 +3,6 @@ package model;
import application.Main;
import model.exceptions.NoCornException;
import model.exceptions.WallException;
import view.Invisible;
/**
* Created by Stefan Michel on 23.10.17.
......@@ -24,24 +23,8 @@ public class Hamster extends Thread {
public void forward() {
try {
int nextRow = terrain.hamsterRow;
int nextCol = terrain.hamsterCol;
switch (terrain.getHamsterDirection()) {
case (Terrain.NORTH):
nextRow--;
break;
case (Terrain.EAST):
nextCol++;
break;
case (Terrain.SOUTH):
nextRow++;
break;
case (Terrain.WEST):
nextCol--;
break;
}
int nextCol = getNextCol();
int nextRow = getNextRow();
if (!terrain.setHamsterPostion(nextCol, nextRow)) {
throw new WallException("Hier steht eine Wand");
......@@ -65,6 +48,44 @@ public class Hamster extends Thread {
terrain.turnLeft();
}
public boolean freeFront() {
int nextCol = getNextCol();
int nextRow = getNextRow();
if (nextCol < 0 || nextRow < 0 || nextCol > terrain.getMaxCols() - 1 || nextRow > terrain.getMaxRows() - 1) {
return false;
}
if (terrain.getCornAmount(nextCol, nextRow) == -1) {
return false;
}
return true;
}
@Invisible
private int getNextCol() {
switch (terrain.getHamsterDirection()) {
case (Terrain.EAST):
return terrain.getHamsterCol() + 1;
case (Terrain.WEST):
return terrain.getHamsterCol() - 1;
default:
return terrain.getHamsterCol();
}
}
@Invisible
private int getNextRow() {
switch (terrain.getHamsterDirection()) {
case (Terrain.NORTH):
return terrain.getHamsterRow() - 1;
case (Terrain.SOUTH):
return terrain.getHamsterRow() + 1;
default:
return terrain.getHamsterRow();
}
}
@Invisible
public Terrain getTerrain() {
return terrain;
......@@ -77,11 +98,6 @@ public class Hamster extends Thread {
@Invisible
public void run() {
// try {
//
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
this.main();
main();
}
}
package view;
package model;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
......
......@@ -54,7 +54,7 @@ public class Terrain extends Observable {
this.hamsterCol = col;
this.hamsterRow = row;
setChanged();
notifyObservers(this);
notifyObservers();
return true;
} else
return false;
......@@ -74,7 +74,7 @@ public class Terrain extends Observable {
if (this.map[col][row] < MAXCORNS && this.map[col][row] >= 0) {
this.map[col][row]++;
setChanged();
notifyObservers(this);
notifyObservers();
} else {
throw new WallException("Auf dieser Position befindet sich eine Wand");
}
......@@ -93,7 +93,7 @@ public class Terrain extends Observable {
if (!(col == hamsterCol && row == hamsterRow)) {
this.map[col][row] = WALL;
setChanged();
notifyObservers(this);
notifyObservers();
}
}
......@@ -183,6 +183,24 @@ public class Terrain extends Observable {
notifyObservers();
}
@Override
public void notifyObservers() {
super.notifyObservers();
if (hamster != null && hamster.isAlive()) {
try {
System.out.println(hamster.getState().toString());
hamster.sleep(500);
} catch (InterruptedException e) {
try {
hamster.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
//prueft, ob sich an dieser position eine wand befindet
public boolean checkWall(int col, int row) {
return this.map[col][row] == WALL;
......@@ -247,5 +265,4 @@ public class Terrain extends Observable {
this.hamster = hamster;
hamster.setTerrain(this);
}
}
package model;
import controller.IOController;
public class ThreadManager {
private Terrain terrain;
private IOController ioController;
public ThreadManager(Terrain terrain, IOController ioController) {
this.terrain = terrain;
this.ioController = ioController;
}
public void startThread() {
ioController.compile(terrain.getName());
terrain.getHamster().setDaemon(true);
terrain.getHamster().start();
}
public void stopThread() {
terrain.getHamster().interrupt();
}
public void pauseThread() {
}
}
......@@ -13,7 +13,9 @@ import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import model.Hamster;
import model.Invisible;
import model.Terrain;
import model.ThreadManager;
import java.io.File;
import java.io.IOException;
......@@ -43,16 +45,19 @@ public class MainViewCreationFX extends VBox {
private TerrainController terrainController;
private IOController ioController;
private HamsterController hamsterController;
Label footer;
private Terrain terrain;
private TextArea textArea;
private ContextMenu contextMenu;
private ThreadManager threadManager;
public MainViewCreationFX(Terrain terrain, TerrainController terrainController, IOController ioController,
HamsterController hamsterController) {
HamsterController hamsterController, ThreadManager threadManager) {
this.terrainController = terrainController;
this.ioController = ioController;
this.hamsterController = hamsterController;
this.threadManager = threadManager;
this.terrain = terrain;
//Territory Pane erstellen
......@@ -83,14 +88,14 @@ public class MainViewCreationFX extends VBox {
splitPane.setPrefHeight(400);
//label erstellen
Label label = new Label("Herzlich willkommen!");
label.setMinHeight(20);
footer = new Label("Herzlich willkommen!");
footer.setMinHeight(20);
//elemente ins Borderpane einsetzen
BorderPane borderPane = new BorderPane();
borderPane.setTop(toolBar);
borderPane.setCenter(splitPane);
borderPane.setBottom(label);
borderPane.setBottom(footer);
//elemente in die VBox einsetzen und returnen
......@@ -414,7 +419,12 @@ public class MainViewCreationFX extends VBox {
Button pause = new Button(null, new ImageView(new Image("/Pause24.gif")));
Button stopp = new Button(null, new ImageView(new Image("/Stop24.gif")));
start.setOnAction(event -> hamsterController.start());
start.setOnAction(event -> {
threadManager.startThread();
start.setDisable(true);
});
pause.setOnAction(event -> threadManager.pauseThread());
stopp.setOnAction(event -> threadManager.stopThread());
// Slider
Slider geschwindigkeitSlider = new Slider();
......@@ -425,4 +435,8 @@ public class MainViewCreationFX extends VBox {
kornImMaul, linksUm, vor, nimm, gib, new Separator(), start, pause, stopp,
new Separator(), geschwindigkeitSlider);
}
public void setFooterText(String s) {
footer.setText(s);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment