-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
350 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package duke.gui; | ||
|
||
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; | ||
import javafx.scene.shape.Rectangle; | ||
|
||
/** | ||
* 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(MainWindow.class.getResource("/view/DialogBox.fxml")); | ||
fxmlLoader.setController(this); | ||
fxmlLoader.setRoot(this); | ||
fxmlLoader.load(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
double width = displayPicture.getFitWidth(); | ||
double height = displayPicture.getFitHeight(); | ||
Rectangle circleMask = new Rectangle(width, height); | ||
circleMask.setArcHeight(height); | ||
circleMask.setArcWidth(width); | ||
|
||
|
||
dialog.setText(text); | ||
displayPicture.setClip(circleMask); | ||
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 getDukeDialog(String text, Image img) { | ||
var db = new DialogBox(text, img); | ||
db.flip(); | ||
return db; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package duke.gui; | ||
|
||
import javafx.application.Application; | ||
|
||
/** | ||
* A launcher class to workaround classpath issues. | ||
*/ | ||
public class Launcher { | ||
public static void main(String[] args) { | ||
Application.launch(Main.class, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package duke.gui; | ||
|
||
import java.io.IOException; | ||
|
||
import duke.Duke; | ||
import javafx.application.Application; | ||
import javafx.fxml.FXMLLoader; | ||
import javafx.scene.Scene; | ||
import javafx.scene.layout.AnchorPane; | ||
import javafx.scene.paint.Color; | ||
import javafx.stage.Stage; | ||
|
||
/** | ||
* A GUI for Duke using FXML. | ||
*/ | ||
public class Main extends Application { | ||
|
||
private Duke duke = new Duke("data/tasks.txt"); | ||
|
||
@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("Calico"); | ||
scene.setFill(Color.TRANSPARENT); | ||
fxmlLoader.<MainWindow>getController().setDuke(duke); | ||
stage.show(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package duke.gui; | ||
|
||
import duke.Duke; | ||
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; | ||
|
||
|
||
|
||
/** | ||
* 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 Duke duke; | ||
|
||
private Image userImage = new Image(this.getClass().getResourceAsStream("/images/DaUser.png")); | ||
private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png")); | ||
|
||
/** | ||
* Initialise Main Window. Introduces Duke. | ||
*/ | ||
@FXML | ||
public void initialize() { | ||
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty()); | ||
} | ||
|
||
public void setDuke(Duke d) { | ||
duke = d; | ||
dialogContainer.getChildren().addAll( | ||
DialogBox.getDukeDialog(duke.intro(), dukeImage) | ||
); | ||
} | ||
|
||
/** | ||
* 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 = duke.getResponse(input); | ||
dialogContainer.getChildren().addAll( | ||
DialogBox.getUserDialog(input, userImage), | ||
DialogBox.getDukeDialog(response, dukeImage) | ||
); | ||
if (userInput.getText().equals("bye")) { | ||
userInput.clear(); | ||
Platform.exit(); | ||
} | ||
userInput.clear(); | ||
} | ||
} |
Oops, something went wrong.