1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
public class TestView {
protected static Game game = new Game("Random Game");
protected static BorderPane root = new BorderPane();
protected static GridPane grilleHaut = new GridPane();
public static void main(String[] args) throws InterruptedException {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestView.initialisation();
}
});
game.runSomething();
}
public static void initialisation() {
JFrame mainSwingFrame = new JFrame("Game");
JFXPanel mainFXFrame = new JFXPanel();
mainSwingFrame.add(mainFXFrame);
mainSwingFrame.setSize(1200, 900);
mainSwingFrame.setVisible(true);
mainSwingFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Platform.runLater(new Runnable() {
public void run() { initFX(mainFXFrame);}
} );
}
public static void initFX(JFXPanel frameFX) {
Scene scene = newScene();
frameFX.setScene(scene);
}
public static Scene newScene() {
Scene scene = new Scene(root, Color.LIGHTGOLDENRODYELLOW);
Text gameName = new Text(game.name);
grilleHaut.add(gameName, 0, 0);
GridPane.setConstraints(gameName, 0, 0, 1, 1, HPos.CENTER, VPos.TOP);
Text turn = new Text("It's " + game.currentPlayer.getName() + "'s turn to play.");
game.currentPlayer.name.addListener((observable, oldValue, newValue) -> {
turn.setText("C'est au tour de : " + newValue);
});
game.currentPlayer.name.addListener(new ChangeListener<String>() {
public void changed(ObservableValue<? extends String> o,String oldv,String newv) {
turn.setText("It's time to play for : " + newv);
System.out.println("accessed on TestView.");
}
});
grilleHaut.add(turn, 0, 1);
GridPane.setConstraints(turn, 0, 1, 1, 1, HPos.CENTER, VPos.CENTER);
BorderPane.setAlignment(grilleHaut, Pos.TOP_RIGHT);
root.setTop(grilleHaut);
return(scene);
}
}
//GAME CLASS ==============
//=========================
class Game {
String name;
Player p1 = new Player("Person 1");
Player p2 = new Player("Human 2");
Player currentPlayer;
public Game(String n) {
name=n;
currentPlayer = p1;
}
public void runSomething() {
//here goes code to run a game
System.out.println(currentPlayer.getName());
currentPlayer = p2; //at some point in the game, it's p2's time to play
String pressToContinue; Scanner ptc = new Scanner(System.in); pressToContinue = ptc.nextLine();
System.out.println(currentPlayer.getName());
currentPlayer = p1; //and then p1's again
String pressToContinue2; Scanner ptc2 = new Scanner(System.in); pressToContinue2 = ptc.nextLine();
System.out.println(currentPlayer.getName());
}
}
//PLAYER CLASS ==============
//===========================
class Player {
protected StringProperty name = new SimpleStringProperty();
public Player(String n) {
name.setValue(n);
name.addListener(new ChangeListener<String>() {
public void changed(ObservableValue<? extends String> o,String oldv,String newv) {
Text turn = new Text("It's time to play for : " + newv);
TestView.grilleHaut.add(turn, 0, 1);
System.out.println("accessed on Player.");
}
});}
public String getName() { return name.getValue(); }
} |
Partager