jeudi 26 mai 2016

Using an event handler to switch between two options using a mouse click

For an assignment I was asked to make a gui that randomly creates a map. On the map there are walls and empty spaces. When one is clicked on it will convert to the other, the problem is that I can convert walls to empty spaces but not the other way around. I tried looking at my if statements and my class that creates the object that determines if a space is a wall or not. Here is the gui

package gui;

import LAB7.StreetMap;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.InputEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class MazeGUIPane extends BorderPane {

    public void setUpGui(Stage primaryStage) {
        Button a = new Button();
        a.setText("Run");
        a.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

            }
        });
        Button b = new Button();
        b.setText("Reset");
        b.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

            }
        });
        HBox h = new HBox();
        h.getChildren().add(a);
        h.getChildren().add(b);
        h.getStyleClass().add("Hbox");

        GridPane gridPane = new GridPane();
        StreetMap S = new StreetMap();

        for (int row = 0; row < 15; row++) {
            for (int col = 0; col < 15; col++) {
                Label L = l(row, col, S);

                gridPane.add(L, col, row);
                S.setStreetMap(row, col);
                if (S.getStreetMap(row, col).getstatus() == 'W') {
                    L.getStyleClass().add("Wall");
                } else if (S.getStreetMap(row, col).getstatus() == '_') {
                    L.getStyleClass().add("Empty");
                } else {
                    L.getStyleClass().add("ES");
                }
            }
        }
        setCenter(gridPane);
        setBottom(h);

    }

    public Label l(final int row, final int col, StreetMap S) {
        Label l = new Label();
        l.setMinWidth(50);
        l.setMinHeight(50);
        l.setOnMouseClicked(new EventHandler<InputEvent>() {
            @Override
            public void handle(InputEvent arg0) {
                if(!(row == 0 || row == 14 || col == 0 || col == 14)){

                if ((S.getStreetMap(row, col).getstatus() == 'W')) {
                    S.getStreetMap(row, col).setstatus('_');
                    l.getStyleClass().add("Empty");
                } 
                if ((S.getStreetMap(row, col).getstatus() != '_')) {
                    S.getStreetMap(row, col).setstatus('W');
                    l.getStyleClass().add("Wall");
                }
                }
            }

        });
        return l;
    }

}

and the other two related classes

package LAB7;

public class StreetMap {
    Coordinate map[][] = new Coordinate[15][15];
    public StreetMap(){
        }

        public void setStreetMap(int row, int col){
            map[row][col] = new Coordinate(row,col);
    }
        public Coordinate getStreetMap(int row,int col){
            return map[row][col];
        }
}

and

package LAB7;

import java.util.Random;

public class Coordinate {
    int col;
    int row;
    char status;
    Random random = new Random();
    int stat;
    public Coordinate(int row, int col) {
        this.row = row;
        this.col = col;
        if((row == 0 || row == 14 || col == 0 || col == 14)&&!((row == 0 && col == 1)||(row == 14 && col == 13 ))){
            status = 'W';
        }
        else if(row == 0 && col == 1){
            status = 'S';
        }
        else if(row == 14 && col == 13){
            status = 'E';
        }
        else if (!(row == 0 || row == 14 || col == 0 || col == 14)){
            int stat = random.nextInt(2);
            if(stat == 0){
                status = 'W';
            }
            else if(stat == 1){
                status = '_';
            }
        }
    }
    public char getstatus(){
        return status;
    }
    public void setstatus(char status){
        this.status = status;
    }


}

I assumed that because I used almost identical if statements that if there was a problem It would apply to both of wall to empty conversion and empty to wall conversion. I'm not really sure where the problem is , could anyone point me in the right direction?

Aucun commentaire:

Enregistrer un commentaire