package lab4_part1;
import java.io.File; import java.util.ArrayList; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.event.EventType; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.FlowPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.Modality; import javafx.stage.Stage;
/** * * @author cg3374fj */ public class Lab4_part1 extends Application implements MyConfig{ private String[] menuStr = {"Login", "Create", "Edit"}; private String[][] menuItemStr = {{"Login"}, {"Select locations", "Select Pass-icons", "Maping", "Clear"}, {"Edit pass-code", "Reset"} }; private MenuBar bar = new MenuBar(); private Menu[] menues; private MenuItem[][]items; private ArrayList locations = new ArrayList<>(); private ArrayList locationsCopy = new ArrayList<>(); ImageView[][] imgView = new ImageView[ROWS][COLUMNS]; private Image[][] imgs = new Image[ROWS][COLUMNS]; private String imgDir = "images"; private File file; private String[] imgFiles; private String[] btnCaptions = {"Save", "Undo", "Reset","Cancel"}; public void start(Stage primaryStage) { file = new File(imgDir); imgFiles = file.list(); System.out.println(imgFiles[0]); menues = new Menu[menuStr.length]; items =new MenuItem[menuItemStr.length][]; for(int i=0; i() { @Override public void handle(ActionEvent event) { locationsCopy = (ArrayList)(locations.clone()); Stage newStage = new Stage(); creatLocationStage("Select 3-10 Cells", newStage); newStage.showAndWait(); } }); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); BorderPane root = new BorderPane(); root.setTop(bar); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); }
public Scene getGridScene(GridPane gridPane, ImageView[][] imgView, Image bgImg, Image selectedImg, Stage newStage){ BorderPane bp = new BorderPane(); HBox hb= new HBox(); gridPane.setHgap(MyConfig.H_GAP); gridPane.setVgap(MyConfig.V_GAP); gridPane.setGridLinesVisible(true); int k=0; for(int i=0; i { String id = ((ImageView) event.getSource()).getId(); try{ int num = Integer.parseInt(id); int r = num/1000; int c = num%1000; System.out.println("Clicked at row: " + r + ", column: " + c); locations.add(new Triple(r, c, -1)); ((ImageView) event.getSource()).setImage(selectedImg); if(locations.size()==MAX_CELLS){ Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("Alert: "); alert.setHeaderText("Alert "); alert.setContentText("Clicked " + MAX_CELLS + " spots"); alert.showAndWait(); } } catch(NumberFormatException e){ System.out.println(e.getMessage()); } }); } } bp.setCenter(gridPane); bp.setBottom(hb); Scene scene = new Scene(bp, (MyConfig.COLUMNS)*MyConfig.CELL_SIZE+5, (MyConfig.ROWS+2)*MyConfig.CELL_SIZE); return scene; } private static void setImage4SelectLocation(ImageView[][] imgView, Image image){ for(int i=0; i { //save System.out.println("You have selected " + locations.size() + " cells."); newStage.close(); }); btns[1].setOnAction( (ActionEvent e) -> { //undo if (locations.size() > 0){ Triple tr = locations.get(locations.size()-1); locations.remove(locations.size()-1); imgView[tr.i][tr.j].setImage(bgImg); } }); btns[2].setOnAction( (ActionEvent e) -> { //reset locations.clear(); setImage4SelectLocation(imgView,bgImg); }); btns[3].setOnAction( (ActionEvent e) -> { //cancel locations = (ArrayList)(locationsCopy.clone()); }); return btns; } public void creatLocationStage(String title, Stage newStage){ GridPane gridPane = new GridPane(); ImageView[][] imgV = new ImageView[ROWS][COLUMNS]; Image bgImg = new Image("File:" + imgDir + "/" + imgFiles[10]); Image selectedImg = new Image("File:" + imgDir + "/" + imgFiles[20]); Scene scene = getGridScene(gridPane, imgV, bgImg, selectedImg, newStage); setImage4SelectLocation(imgV,bgImg); Button[] btns = addButtons4SelectLocation(imgV, bgImg, newStage); final BorderPane bp = (BorderPane) (scene.getRoot()); ((HBox)(bp.getBottom())).getChildren().addAll(btns); newStage.setScene(scene); //tell stage it is meannt to pop-up (Modal) newStage.initModality(Modality.APPLICATION_MODAL); newStage.setTitle(title); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }
Can you correct this java program? Can you find errors in it ?