import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import
javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import
javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import
javafx.stage.Stage;
public class GameApp extends Application
{
public Stage stage;
public Scene currentScene;
public Button startButton;
public TextField inText;
public Button goButton;
public GameState gs;
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void
start(Stage primaryStage) throws Exception
{
gs = new GameState();
stage = primaryStage;
currentScene = introScene();
primaryStage.setScene(currentScene);
primaryStage.setTitle("The
game of games");
primaryStage.show();
}
public Scene
introScene()
{
Image
img = new Image(GameApp.class.getResourceAsStream("introScreen.png"));
ImageView
imv = new ImageView();
imv.setImage(img);
startButton = new Button();
startButton.setText("Start Game");
startButton.setOnAction(new MyActionHandler());
VBox
vb = new VBox();
vb.setSpacing(8);
vb.setAlignment(Pos.CENTER);
vb.getChildren().add(imv);
vb.getChildren().add(startButton);
Scene
s = new Scene(vb,800,600);
return s;
}
public void
displayGameScreen()
{
Text
txt = new Text(gs.getTitle());
txt.setFont(new Font("Arial",36));
String filename = gs.getImageFileName();
Image
img = new Image(GameApp.class.getResourceAsStream(filename));
ImageView
imv = new ImageView();
imv.setImage(img);
Text
t = new Text();
t.setFont(new Font("Arial",14));
t.setWrappingWidth(450);
String desc = gs.getDescription();
t.setText(desc);
inText = new
TextField();
inText.setOnAction(new
MyActionHandler());
goButton = new Button();
goButton.setText("Go");
goButton.setOnAction(new
MyActionHandler());
HBox hb = new HBox();
hb.setSpacing(8);
hb.setAlignment(Pos.CENTER);
hb.getChildren().add(inText);
hb.getChildren().add(goButton);
VBox
vb = new VBox();
vb.setSpacing(8);
vb.setAlignment(Pos.CENTER);
vb.getChildren().add(txt);
vb.getChildren().add(imv);
vb.getChildren().add(t);
vb.getChildren().add(hb);
Scene
s = new Scene(vb,800,600);
stage.setScene(s);
}
public class
MyActionHandler implements EventHandler<ActionEvent>
{
@Override
public void
handle(ActionEvent e)
{
if
(e.getSource()==startButton)
{
displayGameScreen();
}
else if
(e.getSource()==goButton || e.getSource()==inText)
{
String inp = inText.getText();
gs.updateState(inp);
displayGameScreen();
}
}
}
}
|