Guest User

Untitled

a guest
Dec 9th, 2019
85
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.bham.pij.assignments.edgedector;
  2.  
  3. import javafx.application.Application;
  4. import javafx.event.ActionEvent;
  5. import javafx.event.EventHandler;
  6. import javafx.scene.Group;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.*;
  9. import javafx.scene.image.ImageView;
  10. import javafx.scene.layout.VBox;
  11. import javafx.stage.FileChooser;
  12. import javafx.stage.Stage;
  13. import javafx.scene.image.Image;
  14. import java.io.File;
  15. import java.nio.file.NoSuchFileException;
  16.  
  17. public class GUI extends Application {
  18. Image raw;
  19. ImageView rawImageView;
  20. VBox root = new VBox();
  21. VBox images = new VBox();
  22. Group rawImageCopy = new Group();
  23. final Scene scene = new Scene(root, 500, 500);
  24.  
  25. @Override
  26. public void start(Stage stage){
  27.  
  28. stage.setTitle("Image Filter");
  29.  
  30. // File menu
  31. Menu fileMenu = new Menu("File");
  32.  
  33. // File menu items
  34. MenuItem openItem = new MenuItem("Open");
  35. MenuItem closeItem = new MenuItem("Close");
  36. closeItem.setDisable(true);
  37. // Tools Menu
  38. Menu toolsMenu = new Menu("Tools");
  39.  
  40. // Tools menu items
  41. MenuItem edgeDetItem = new MenuItem("Edge Detection");
  42. MenuItem revertItem = new MenuItem("Revert");
  43. edgeDetItem.setDisable(true);
  44. revertItem.setDisable(true);
  45.  
  46. // Setting up each Menu with the corresponting items
  47. fileMenu.getItems().addAll(openItem, closeItem);
  48. toolsMenu.getItems().addAll(edgeDetItem, revertItem);
  49.  
  50. // Menu item functions
  51.  
  52. // Open item Action
  53. openItem.setOnAction(new EventHandler<ActionEvent>() {
  54. @Override
  55. public void handle(ActionEvent o) {
  56.  
  57. FileChooser fileChooser = new FileChooser();
  58. fileChooser.setTitle("Open Image File");
  59.  
  60. File file = fileChooser.showOpenDialog(stage);
  61.  
  62. if (file != null){
  63. loadImageFile(file);
  64. openItem.setDisable(true);
  65. edgeDetItem.setDisable(false);
  66. closeItem.setDisable(false);
  67. }
  68. }
  69. });
  70.  
  71. // Close item Action
  72. closeItem.setOnAction(new EventHandler<ActionEvent>() {
  73. @Override
  74. public void handle(ActionEvent c) {
  75. closeItem.setDisable(true);
  76. openItem.setDisable(false);
  77. edgeDetItem.setDisable(true);
  78. revertItem.setDisable(true);
  79. images.getChildren().clear();
  80. }
  81. });
  82.  
  83. // Edge Detection item Action
  84. edgeDetItem.setOnAction(new EventHandler<ActionEvent>() {
  85. @Override
  86. public void handle(ActionEvent actionEvent) {
  87. EdgeDetector obj = new EdgeDetector();
  88. obj.filterImage(raw);
  89.  
  90. openItem.setDisable(true);
  91. revertItem.setDisable(false);
  92. closeItem.setDisable(false);
  93. edgeDetItem.setDisable(true);
  94. }
  95. });
  96.  
  97. // Revert item Action
  98. revertItem.setOnAction(new EventHandler<ActionEvent>() {
  99. @Override
  100. public void handle(ActionEvent actionEvent) {
  101.  
  102. images.getChildren().clear();
  103. root.getChildren().add(rawImageCopy);
  104.  
  105. edgeDetItem.setDisable(false);
  106. openItem.setDisable(true);
  107. closeItem.setDisable(false);
  108. revertItem.setDisable(true);
  109. }
  110. });
  111.  
  112. //Setting up the Menu Bar
  113. final MenuBar menuBar = new MenuBar();
  114. menuBar.getMenus().addAll(fileMenu, toolsMenu);
  115.  
  116. root.getChildren().add(menuBar);
  117.  
  118. stage.setScene(scene);
  119. stage.show();
  120. }
  121.  
  122. private void loadImageFile (File file ){
  123. Image image = new Image("file:" + file.getPath());
  124. ImageView imageView = new ImageView(image);
  125. ImageView rawImage = new ImageView(image);
  126.  
  127. raw = image;
  128. rawImageView = imageView;
  129.  
  130. images.getChildren().add(imageView);
  131. rawImageCopy.getChildren().add(rawImage);
  132. root.getChildren().add(images);
  133. }
  134.  
  135.  
  136.  
  137. public static void main(String[] args) {
  138. launch(args);
  139. }
  140. }
RAW Paste Data