Skip to content

Commit

Permalink
JavaFX
Browse files Browse the repository at this point in the history
  • Loading branch information
reneeyeow02 committed Sep 3, 2022
1 parent 0685afe commit 08cacd5
Show file tree
Hide file tree
Showing 16 changed files with 2,992 additions and 6 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.5'
id 'checkstyle'
id 'com.github.johnrengelman.shadow' version '5.1.0'
}
Expand Down
3 changes: 2 additions & 1 deletion data/tasks.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
T | false | one
T | false | two
T | false | three
D | false | three | 2020-01-01T23:59:59
E | false | four | 5pm
886 changes: 886 additions & 0 deletions hs_err_pid12670.log

Large diffs are not rendered by default.

890 changes: 890 additions & 0 deletions hs_err_pid12745.log

Large diffs are not rendered by default.

858 changes: 858 additions & 0 deletions hs_err_pid12985.log

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package meowmeow;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import meowmeow.Meowmeow;

/**
* A GUI for Duke using FXML.
*/
public class Main extends Application {

private Meowmeow meowmeow = new Meowmeow();

@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);
fxmlLoader.<meowmeow.MainWindow>getController().setMeowmeow(meowmeow);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
54 changes: 54 additions & 0 deletions src/main/java/MainWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package meowmeow;

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 meowmeow.DialogBox;
import meowmeow.Meowmeow;

/**
* Controller for MainWindow. Provides the layout for the other controls.
*/
public class MainWindow extends AnchorPane {
@FXML
private ScrollPane scrollPane;
@FXML
private VBox dialogContainer;
@FXML
private TextField userInput;
@FXML
private Button sendButton;

private Meowmeow meowmeow;

private Image userImage = new Image(this.getClass().getResourceAsStream("/images/DaUser.png"));
private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png"));

@FXML
public void initialize() {
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());
}

public void setMeowmeow(Meowmeow m) {
meowmeow = m;
}

/**
* Creates two dialog boxes, one echoing user input and the other containing Duke's reply and then appends them to
* the dialog container. Clears the user input after processing.
*/
@FXML
private void handleUserInput() {
String input = userInput.getText();
String response = meowmeow.getResponse(input);
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
DialogBox.getMeowmeowDialog(response, dukeImage)
);
userInput.clear();
}
}
61 changes: 61 additions & 0 deletions src/main/java/meowmeow/DialogBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package meowmeow;

import java.io.IOException;
import java.util.Collections;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

/**
* An example of a custom control using FXML.
* This control represents a dialog box consisting of an ImageView to represent the speaker's face and a label
* containing text from the speaker.
*/
public class DialogBox extends HBox {
@FXML
private Label dialog;
@FXML
private ImageView displayPicture;

private DialogBox(String text, Image img) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(meowmeow.MainWindow.class.getResource("/view/DialogBox.fxml"));
fxmlLoader.setController(this);
fxmlLoader.setRoot(this);
fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}

dialog.setText(text);
displayPicture.setImage(img);
}

/**
* Flips the dialog box such that the ImageView is on the left and text on the right.
*/
private void flip() {
ObservableList<Node> tmp = FXCollections.observableArrayList(this.getChildren());
Collections.reverse(tmp);
getChildren().setAll(tmp);
setAlignment(Pos.TOP_LEFT);
}

public static DialogBox getUserDialog(String text, Image img) {
return new DialogBox(text, img);
}

public static DialogBox getMeowmeowDialog(String text, Image img) {
var db = new DialogBox(text, img);
db.flip();
return db;
}
}
13 changes: 13 additions & 0 deletions src/main/java/meowmeow/Launcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package meowmeow;

import javafx.application.Application;
import meowmeow.Meowmeow;

/**
* A launcher class to workaround classpath issues.
*/
public class Launcher {
public static void main(String[] args) {
Application.launch(meowmeow.Main.class, args);
}
}
157 changes: 156 additions & 1 deletion src/main/java/meowmeow/Meowmeow.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
package meowmeow;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


import meowmeow.commands.Command;

/**
* <p>The Meowmeow class is the main class of the program.</p>
* <p>This class is used to run the program.</p>
*/
public class Meowmeow {
public class Meowmeow extends Application {

private Storage storage;
private TaskList tasks;
private Ui ui;

//User and bot images
private Image user = new Image(this.getClass().getResourceAsStream("/images/DaUser.png"));
private Image duke = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png"));

//GUI components
private ScrollPane scrollPane;
private VBox dialogContainer;
private TextField userInput;
private Button sendButton;
private Scene scene;

/**
* Constructor for the Meowmeow class.
*
Expand All @@ -22,6 +48,10 @@ public Meowmeow(String filePath) {
tasks = new TaskList(storage.load(), storage);
}

public Meowmeow() {

}

/**
* Method that runs the program.
*/
Expand Down Expand Up @@ -50,4 +80,129 @@ public void run() {
public static void main(String[] args) {
new Meowmeow("data/tasks.txt").run();
}


/**
* Method that initializes the JavaFX application.
*
* @param stage the primary stage of the JavaFX application.
*/
@Override
public void start(Stage stage) {
//Step 1. Setting up required components

//The container for the content of the chat to scroll.
scrollPane = new ScrollPane();
dialogContainer = new VBox();
scrollPane.setContent(dialogContainer);

userInput = new TextField();
sendButton = new Button("Send");

AnchorPane mainLayout = new AnchorPane();
mainLayout.getChildren().addAll(scrollPane, userInput, sendButton);

scene = new Scene(mainLayout);

stage.setScene(scene);
stage.show();

stage.setTitle("MeowMeow");
stage.setResizable(false);
stage.setMinHeight(600.0);
stage.setMinWidth(400.0);

mainLayout.setPrefSize(400.0, 600.0);

scrollPane.setPrefSize(385, 535);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

scrollPane.setVvalue(1.0);
scrollPane.setFitToWidth(true);

// You will need to import `javafx.scene.layout.Region` for this.
dialogContainer.setPrefHeight(Region.USE_COMPUTED_SIZE);

userInput.setPrefWidth(325.0);

sendButton.setPrefWidth(55.0);

AnchorPane.setTopAnchor(scrollPane, 1.0);

AnchorPane.setBottomAnchor(sendButton, 1.0);
AnchorPane.setRightAnchor(sendButton, 1.0);

AnchorPane.setLeftAnchor(userInput , 1.0);
AnchorPane.setBottomAnchor(userInput, 1.0);


// You will need to import `javafx.scene.layout.Region` for this.
dialogContainer.setPrefHeight(Region.USE_COMPUTED_SIZE);

userInput.setPrefWidth(325.0);

sendButton.setPrefWidth(55.0);

AnchorPane.setTopAnchor(scrollPane, 1.0);

AnchorPane.setBottomAnchor(sendButton, 1.0);
AnchorPane.setRightAnchor(sendButton, 1.0);

AnchorPane.setLeftAnchor(userInput , 1.0);
AnchorPane.setBottomAnchor(userInput, 1.0);


//Part 3. Add functionality to handle user input.
sendButton.setOnMouseClicked((event) -> {
handleUserInput();
});

userInput.setOnAction((event) -> {
handleUserInput();
});

//Scroll down to the end every time dialogContainer's height changes.
dialogContainer.heightProperty().addListener((observable) -> scrollPane.setVvalue(1.0));

}

/**
* Iteration 1:
* Creates a label with the specified text and adds it to the dialog container.
* @param text String containing text to add
* @return a label with the specified text that has word wrap enabled.
*/
private Label getDialogLabel(String text) {
// You will need to import `javafx.scene.control.Label`.
Label textToAdd = new Label(text);
textToAdd.setWrapText(true);

return textToAdd;
}

/**
* Iteration 2:
* Creates two dialog boxes, one echoing user input and the other containing Duke's reply and then appends them to
* the dialog container. Clears the user input after processing.
*/
private void handleUserInput() {
Label userText = new Label(userInput.getText());
Label dukeText = new Label(getResponse(userInput.getText()));
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(userText, new ImageView(user)),
DialogBox.getDukeDialog(dukeText, new ImageView(duke))
);
userInput.clear();
}


/**
* You should have your own function to generate a response to user input.
* Replace this stub with your completed method.
*/
private String getResponse(String input) {
return "Meowmeow heard: " + input;
}

}
4 changes: 2 additions & 2 deletions src/main/java/meowmeow/events/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Deadline(String taskName, LocalDateTime date) {
*/
@Override
public String toString() {
return "[D] " + super.toString() + " (by:"
return "[D] " + super.toString() + "(by:"
+ date.format(DateTimeFormatter.ofPattern(" hh:mm a 'on' dd/MM/yyyy")) + ")";
}

Expand All @@ -39,6 +39,6 @@ public String toString() {
*/
@Override
public String getSaveData() {
return " D" + " | " + super.isDone() + " | " + taskName + " | " + date;
return "D" + " | " + super.isDone() + " | " + taskName + " | " + date;
}
}
Loading

0 comments on commit 08cacd5

Please sign in to comment.