3
\$\begingroup\$

I've been trying to write a simplified Google Maps program. My programming experience is limited. I have come up with a solution but I'm looking to make it more user friendly by splitting it up into various classes.

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;



public class MyController {

    @FXML
    private ImageView mainpic;

    @FXML 
    private ImageView thumbpic;

    @FXML
    private Button strt;

    @FXML 
    private Button forward;

    @FXML
    private Button back;

    @FXML
    private Button left;

    @FXML
    private Button right;

    // matrix variables //
    private int x = 7;
    private int y = 7;

    public int count = 0;

    public int count2 = 0;



    // declare a matrix of string to hold the image URLs //
   String[][] ImageURL = new String[x][y];


   //------------------------//
   //-----BUTTON METHODS-----//
   //-----------------------//

   // Start button method brings up the starting image //
    public void Start(ActionEvent event)
    {
        forward.setDisable(false);
        back.setDisable(true);


            x = 2;
            y = 1;
   // Load the images into their respective location in the image matrix (images are downloaded from my website) //
        ImageURL[2][0] = ".....jpg";
        ImageURL[3][0] = "......jpg";
        etc....

        Image image = new Image(ImageURL[x][y]);
        mainpic.setImage(image);
    }


    public void goForwards(ActionEvent event)
    {

        // if the forward or backward button was disabled before, un-disable it //
        forward.setDisable(false);
        back.setDisable(false);

    if (count2==2 || count==2) {
            y = y-2;
            x = x; }    

    else { y = y + 2;
          x = x; }

       Image image = new Image(ImageURL[x][y]);
    // tests to check if neighbouring matrix element is 0 (so we can't go through a wall) //    
        if (ImageURL[x][y+1]==null)
        {
        forward.setDisable(true);
        }
        mainpic.setImage(image);
    }

    public void goBackwards(ActionEvent event)
    {

        back.setDisable(false);
        forward.setDisable(false);

        x = x;
        y = y-2;
        Image image = new Image(ImageURL[x][y]);

        if (ImageURL[x][y-1]==null)
        {
        back.setDisable(true);
        }
        mainpic.setImage(image);
    }


    public void turnLeft(ActionEvent event)
    {
        forward.setDisable(false);
        back.setDisable(false);

//        if (ImageURL[x+1][y]==null) {
//            forward.setDisable(true); }
//        if (ImageURL[x-1][y]==null) {
//          back.setDisable(true);}

        if (count2==1) {
            count = 0;
            count2 = 0;}
        else if (count2==2) {
            count = 2;
            count2 = 0;}
        else if (count2==3) {
            count = 1;
            count2 = 0;}

    count++;

   if (count>0 && count<2)
   {
        x = x-1;
        y = y;
    Image image = new Image(ImageURL[x][y]);
    mainpic.setImage(image);
   }

    else if (count>=1 && count<3)
    {
        x = x+1;
        y = y-1;
    Image image2 = new Image(ImageURL[x][y]);
    mainpic.setImage(image2);
    }

   else if (count>=2 && count<4)
    {
        x = x+1;
        y = y+1;
    Image image3 = new Image(ImageURL[x][y]);
    mainpic.setImage(image3);
    }

   else if (count>=4 )
    {
        x = x-1;
        y = y;
    Image image4 = new Image(ImageURL[x][y]);
    mainpic.setImage(image4);
    count = 0;
    }
   }

    public void turnRight(ActionEvent event)
    {
        forward.setDisable(false);
        back.setDisable(false);

//        if (ImageURL[x+2][y]==null) {
//            forward.setDisable(true); }
//        if (ImageURL[x-2][y]==null) {
//          back.setDisable(true);}


        if (count==1) {
            count2 = 0;
            count = 0;}
        else if (count==2) {
            count2 = 2;
            count = 0;}
        else if (count==3) {
            count2 = 1;
            count = 0;}

    count2++;
    System.out.println(count2);

    if (count2>0 && count2<2)
   {
        x = x+1;
        y = y;
    Image image = new Image(ImageURL[x][y]);
    mainpic.setImage(image);
    }

    else if (count2>=1 && count2<3)
    {
        x = x-1;
        y = y-1;
    Image image2 = new Image(ImageURL[x][y]);
    mainpic.setImage(image2);
    }

   else if (count2>2 && count2<4)
    {
        x = x-1;
        y = y+1;
    Image image3 = new Image(ImageURL[x][y]);
    mainpic.setImage(image3);
    }

   else if (count2>=4)
    {
        x = x+1;
        y = y;
    Image image4 = new Image(ImageURL[x][y]);
    mainpic.setImage(image4);
    count2 = 0;
    }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.