Skip to content

Commit

Permalink
Update GUI to display greetings
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeMingDe committed Aug 30, 2020
1 parent 1e5989d commit f014387
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion data/tasks.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
T | 0 | return book |2/12/2019 1800
T | 0 | return book
8 changes: 7 additions & 1 deletion src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Duke(String filePath) {
* Executes the running of the chat bot.
*/
public void run() {
System.out.println(ui.greet());
System.out.println(Ui.greet());
boolean isExit = false;
while (!isExit) {
try {
Expand All @@ -70,6 +70,12 @@ public static void main(String[] args) {
new Duke("data/tasks.txt").run();
}

/**
* Parses user inputs into commands and returns it as a string.
*
* @param input User input.
* @return String of executed command.
*/
public String getResponse(String input) {
try {
Command c = Parser.parse(input);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/duke/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ public class Main extends Application {

private Duke duke = new Duke();

/**
* Sets the scene.
*
* @param stage Stage of a JavaFX application.
*/
@Override
public void start(Stage stage) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/view/MainWindow.fxml"));
AnchorPane ap = fxmlLoader.load();
Scene scene = new Scene(ap);
stage.setScene(scene);
stage.setTitle("Duke");
fxmlLoader.<MainWindow>getController().setDuke(duke);
stage.show();
} catch (IOException e) {
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ public Ui() {
*
* @return String containing greeting message.
*/
public String greet() {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
return ("Hello from\n" + logo + "\n"
+ "~ \n Hello I'm the Terminator \n What can I do for you? \n~");
public static String greet() {
return ("~ \n Hello I'm the Terminator \n What can I do for you? \n~");
}

/**
Expand Down
31 changes: 27 additions & 4 deletions src/main/java/duke/gui/MainWindow.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package duke.gui;

import duke.Duke;
import duke.Ui;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.util.Duration;

/**
* Controller for MainWindow. Provides the layout for the other controls.
*/
Expand All @@ -26,9 +32,15 @@ public class MainWindow extends AnchorPane {
private Image userImage = new Image(this.getClass().getResourceAsStream("/images/DaUser.png"));
private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png"));

/**
* Initializes the dialog container and display greeting message.
*/
@FXML
public void initialize() {
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());
dialogContainer.getChildren().addAll(
DialogBox.getDukeDialog(Ui.greet(), dukeImage)
);
}

public void setDuke(Duke d) {
Expand All @@ -43,10 +55,21 @@ public void setDuke(Duke d) {
private void handleUserInput() {
String input = userInput.getText();
String response = duke.getResponse(input);
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
DialogBox.getDukeDialog(response, dukeImage)
);
if (input.toLowerCase().equals("bye")) {

KeyFrame keyFrame = new KeyFrame(Duration.seconds(0), e -> dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
DialogBox.getDukeDialog(response, dukeImage)
));
KeyFrame exit = new KeyFrame(Duration.seconds(1.5), e -> Platform.exit());
Timeline timeline = new Timeline(keyFrame, exit);
Platform.runLater(timeline::play);
} else {
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
DialogBox.getDukeDialog(response, dukeImage)
);
}
userInput.clear();
}
}

0 comments on commit f014387

Please sign in to comment.