Skip to content

Commit

Permalink
Better code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
tah5in committed Mar 3, 2024
1 parent 04323f7 commit c6fbb9b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
12 changes: 7 additions & 5 deletions src/main/java/dude/gui/DialogBox.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package dude.gui;

import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.fxml.FXML;

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

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

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

// @@author Jeffry Lum
// Solution below is reused from https://se-education.org/guides/tutorials/javaFxPart4.html

/**
* 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
Expand Down
40 changes: 26 additions & 14 deletions src/main/java/dude/gui/MainView.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* Controller for MainView. Provides the layout for the other controls.
*/
public class MainView extends AnchorPane {
private static final String USER_IMAGE_PATH = "/images/user.png";
private static final String DUDE_IMAGE_PATH = "/images/dude.png";
@FXML
private ScrollPane scrollPane;

Expand All @@ -25,37 +27,47 @@ public class MainView extends AnchorPane {
@FXML
private VBox dialogContainer;

private Image userImage = new Image(this.getClass().getResourceAsStream("/images/user.png"));
private Image dudeImage = new Image(this.getClass().getResourceAsStream("/images/dude.png"));

private Image userImage;
private Image dudeImage;
private Dude dude;

@FXML
public void initialize() {
dude = new Dude("data/tasks.ser");
this.dude = new Dude("data/tasks.ser");
this.userImage = new Image(this.getClass().getResourceAsStream(USER_IMAGE_PATH));
this.dudeImage = new Image(this.getClass().getResourceAsStream(DUDE_IMAGE_PATH));
}

/**
* Creates two dialog boxes, one echoing user input and the other containing Dude's reply and then appends them to
* the dialog container. Clears the user input after processing.
*/
@FXML
public void handleUserInput() {
String input = userInputField.getText();
String response = dude.getResponse(input);
if (input.equals("bye")) {
System.exit(0);
}

System.out.println("User input: " + input);
String response = dude.getResponse(input);
;
userInputField.clear();

dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
DialogBox.getDukeDialog(response, dudeImage)
);
showInputAndResponse(input, response);
scrollDown();
}

private void scrollDown() {
dialogContainer.heightProperty().addListener((observable) -> {
scrollPane.setVvalue(1.0);
});
}

if (input.equals("bye")) {
System.exit(0);
}

private void showInputAndResponse(String input, String response) {
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(input, userImage),
DialogBox.getDukeDialog(response, dudeImage)
);
}

}

0 comments on commit c6fbb9b

Please sign in to comment.