0

I am a first year student to the university. About a month ago I received an assignment on Java which asked me to create a maze game with a ball and after that to program the ball to navigate through the maze. After I finished the whole GUI I started programming the ball but I came across a problem.

After my code was ready the ball needed to move properly but it only move left no matter which button I was pressing on. After I changed the rightX +=38 from rightY +=38 guessing that was the main issue that caused, I saw that the ball after this change wasn't moving at all.

I will provide my full code below (there are some minor error like buttons that doesn't exist because I didn't removed them yet). The code for the ball to move starts on public boolean moveLeft1() and goes all the way until the end.

public class GMazeBall extends JFrame implements ActionListener{

    private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11,
            button12, buttonact, buttongame, button16, buttoncompass, button, buttonsand, button13, buttonLeft,buttonRight, 
            buttonUp, buttonDown;
    private JPanel panel1, panel2, panel3;
    private JTextField textoption, textsquare, textdirection, firsttext, secondtext, thirdtext;
    private JLabel lsquare, ldirection, loption, l1, label2, ldigital, lspeed;
    private ImageIcon gamereset, east, west, north, south, gameact, gamerun, white, sand;
    private JSlider slidebar;
    private JButton SandRoad;
    private JButton SandStone;
    private JButton GoldenBall;
    ArrayList<JButton> myArray = new ArrayList<JButton>();

    ImageIcon South = new ImageIcon("myimg/south.jpg");
    ImageIcon West = new ImageIcon("myimg/west.jpg");
    ImageIcon East = new ImageIcon("myimg/east.jpg");
    ImageIcon North = new ImageIcon("myimg/noth.jpg");

    public static void main(String[] args) {

        GMazeBall frame = new GMazeBall();
        frame.setSize(775, 665);
        frame.createGUI();
        frame.setVisible(true);
    }

    private void createGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(null);

        panel1 = new JPanel();
        panel1.setBounds(0, 0, 610, 500);
        panel1.setBackground(Color.white);
        panel1.setLayout(null);
        window.add(panel1);

        //public void createGoldenBall(int x, int y) {
            Border border = BorderFactory.createEmptyBorder();

            GoldenBall = new JButton(new ImageIcon("myimg/gold-ball.png"));
            GoldenBall.setBounds(570, 0, 38, 38);
            GoldenBall.setBorder(border);
            GoldenBall.setOpaque(false);
            GoldenBall.setContentAreaFilled(false);
            GoldenBall.setBorderPainted(false);
            panel1.add(GoldenBall);



        createSandStone(0, 456);


        Border newborder = BorderFactory.createEmptyBorder();

        sand = new ImageIcon("myimg/sand.jpg");

        for (int i = 0; i < 16; i++) {
            buttonsand = new JButton(sand);
            buttonsand.setBounds(i * 38, 0, 38, 38);
            buttonsand.setBorder(newborder);
            panel1.add(buttonsand);
            myArray.add(buttonsand);
        }

        for (int i = 0; i < 16; i++) {
            buttonsand = new JButton(sand);
            buttonsand.setBounds(i * 38, 114, 38, 38);
            buttonsand.setBorder(newborder);
            panel1.add(buttonsand);
            myArray.add(buttonsand);
        }
        for (int i = 0; i < 16; i++) {
            buttonsand = new JButton(sand);
            buttonsand.setBounds(i * 38, 228, 38, 38);
            buttonsand.setBorder(newborder);
            panel1.add(buttonsand);
            myArray.add(buttonsand);
        }
        for (int i = 0; i < 16; i++) {
            buttonsand = new JButton(sand);
            buttonsand.setBounds(i * 38, 342, 38, 38);
            buttonsand.setBorder(newborder);
            panel1.add(buttonsand);
            myArray.add(buttonsand);
        }
        for (int i = 0; i < 16; i++) {
            buttonsand = new JButton(sand);
            buttonsand.setBounds(i * 38, 456, 38, 38);
            buttonsand.setBorder(newborder);
            panel1.add(buttonsand);
            myArray.add(buttonsand);
        }

        createSandRoad(38, 38);
        createSandRoad(38, 76);
        createSandRoad(190, 38);
        createSandRoad(190, 76);
        createSandRoad(342, 38);
        createSandRoad(342, 76);
        createSandRoad(76, 152);
        createSandRoad(76, 190);
        createSandRoad(228, 152);
        createSandRoad(228, 190);
        createSandRoad(418, 152);
        createSandRoad(418, 190);
        createSandRoad(38, 266);
        createSandRoad(38, 304);
        createSandRoad(190, 266);
        createSandRoad(190, 304);
        createSandRoad(456, 266);
        createSandRoad(456, 304);
        createSandRoad(76, 380);
        createSandRoad(76, 418);
        createSandRoad(228, 380);
        createSandRoad(228, 418);

        white = new ImageIcon("myimg/white32x32.jpg");

        for (int y = 0; y < 13; y++) {
            for (int x = 0; x < 16; x++) {
                button = new JButton(white);
                button.setBounds(x * 38, y * 38, 38, 38);
                button.setContentAreaFilled(false);
                button.setBorder(newborder);
                panel1.add(button);
            }
        }

        panel2 = new JPanel();
        panel2.setBounds(605, 0, 170, 500);
        panel2.setBackground(Color.LIGHT_GRAY);
        panel2.setBorder(newborder);
        window.add(panel2);
        panel3 = new JPanel();
        panel3.setBounds(0, 500, 775, 500);
        panel3.setBackground(Color.LIGHT_GRAY);
        panel3.setBorder(newborder);
        window.add(panel3);

        loption = new JLabel("option:");
        panel2.add(loption);
        textoption = new JTextField(5);

        loption.setPreferredSize(new Dimension(70, 25));
        textoption.setSize(20, 50);
        panel2.add(textoption);

        lsquare = new JLabel("square:");
        panel2.add(lsquare);
        textsquare = new JTextField(5);

        lsquare.setPreferredSize(new Dimension(70, 25));
        textsquare.setSize(20, 50);
        panel2.add(textsquare);

        ldirection = new JLabel("direction:");
        panel2.add(ldirection);
        textdirection = new JTextField(5);

        ldirection.setPreferredSize(new Dimension(70, 25));
        textdirection.setSize(20, 50);
        panel2.add(textdirection);

        ldigital = new JLabel("            DIGITAL TIMER");
        ldigital.setPreferredSize(new Dimension(151, 25));
        panel2.add(ldigital);

        firsttext = new JTextField("00");
        firsttext.setBackground(Color.black);
        firsttext.setPreferredSize(new Dimension(35, 20));
        panel2.add(firsttext);

        l1 = new JLabel(":");
        panel2.add(l1);

        secondtext = new JTextField("00");
        secondtext.setBackground(Color.black);
        secondtext.setPreferredSize(new Dimension(35, 20));
        panel2.add(secondtext);

        label2 = new JLabel(":");
        panel2.add(label2);

        thirdtext = new JTextField("00");
        thirdtext.setBackground(Color.black);
        thirdtext.setPreferredSize(new Dimension(35, 20));
        panel2.add(thirdtext);

        button1 = new JButton("");
        button1.setPreferredSize(new Dimension(41, 24));
        panel2.add(button1);

        buttonUp = new JButton("^");
        buttonUp.setPreferredSize(new Dimension(41, 24));
        buttonUp.addActionListener(this);
        panel2.add(buttonUp);
        button3 = new JButton("");
        button3.setPreferredSize(new Dimension(41, 24));
        panel2.add(button3);
        buttonLeft = new JButton("<");
        buttonLeft.setPreferredSize(new Dimension(41, 24));
        buttonLeft.addActionListener(this);
        panel2.add(buttonLeft);
        button5 = new JButton("");
        button5.setPreferredSize(new Dimension(41, 24));
        panel2.add(button5);

        buttonRight = new JButton(">");
        buttonRight.setPreferredSize(new Dimension(41, 24));
        buttonRight.addActionListener(this);
        panel2.add(buttonRight);

        button7 = new JButton("");
        button7.setPreferredSize(new Dimension(41, 24));
        panel2.add(button7);

        buttonDown = new JButton("v");
        buttonDown.setPreferredSize(new Dimension(41, 24));
        buttonDown.addActionListener(this);
        panel2.add(buttonDown);

        button9 = new JButton("");
        button9.setPreferredSize(new Dimension(41, 24));
        panel2.add(button9);

        button10 = new JButton("Opt.1");
        panel2.add(button10);

        button11 = new JButton("Opt.2");
        panel2.add(button11);

        button12 = new JButton("Opt.3");
        panel2.add(button12);

        button13 = new JButton(" Exit ");
        panel2.add(button13);

        east = new ImageIcon("myimg/north.jpg");
        buttoncompass = new JButton(east);
        panel2.add(buttoncompass);
        buttoncompass.setPreferredSize(new Dimension(85, 80));

        gameact = new ImageIcon("myimg/step.png");
        buttonact = new JButton(gameact);
        buttonact.setPreferredSize(new Dimension(70, 20));
        panel3.add(buttonact);

        gamerun = new ImageIcon("myimg/run.png");
        buttongame = new JButton(gamerun);
        buttongame.setPreferredSize(new Dimension(70, 20));
        panel3.add(buttongame);

        gamereset = new ImageIcon("myimg/reset.png");
        button16 = new JButton(gamereset);

        button16.setPreferredSize(new Dimension(70, 20));
        panel3.add(button16);

        lspeed = new JLabel("                                                                                 speed:    ");
        panel3.add(lspeed);

        slidebar = new JSlider(JSlider.HORIZONTAL, 200, 1000, 1000);
        slidebar.setPreferredSize(new Dimension(182, 26));
        panel3.add(slidebar);

    }

    public void createSandRoad(int x, int y) {
        Border border = BorderFactory.createEmptyBorder();

        SandRoad = new JButton(new ImageIcon("myimg/sand.jpg"));
        SandRoad.setBounds(x, y, 38, 38);
        SandRoad.setContentAreaFilled(false);
        SandRoad.setBorder(border);
        panel1.add(SandRoad);
    }

    public void createSandStone(int x, int y) {
        Border border = BorderFactory.createEmptyBorder();

        SandStone = new JButton(new ImageIcon("myimg/sandstone.jpg"));
        SandStone.setBounds(x, y, 38, 38);
        SandStone.setContentAreaFilled(false);
        SandStone.setBorder(border);
        panel1.add(SandStone);
    }



    public boolean moveLeft1() {
        int leftX = GoldenBall.getX();
        int leftY = GoldenBall.getY();
        leftX -= 38;

        for (int i = 0; i < myArray.size(); i++) {
        if (myArray.get(i).getX() == leftX && myArray.get(i).getY() == leftY)

            return true;

        }   

        return false;
    }

    public boolean moveRight1() {
        int rightX = GoldenBall.getX();
        int rightY = GoldenBall.getY();
        rightX += 38;

        for (int i = 0; i < myArray.size(); i++) {
        if (myArray.get(i).getX() == rightX && myArray.get(i).getY() == rightY)

            return true;

    }

        return false;

    }

    public boolean moveUp1() {
        int upX = GoldenBall.getX();
        int upY = GoldenBall.getY();
        upY -= 38;

        for (int i = 0; i < myArray.size(); i++) {
        if (myArray.get(i).getX() == upX && myArray.get(i).getY() == upY)

            return true;

        }

            return false;
    }

    public boolean moveDown1() {
        int downX = GoldenBall.getX();
        int downY = GoldenBall.getY();
        downY += 38;

        for (int i = 0; i < myArray.size(); i++) {
        if (myArray.get(i).getX() == downX && myArray.get(i).getY() == downY)

        return true;

        }

        return false;
    }

    public void moveLeft2() {

        int LeftX = GoldenBall.getX();
        int LeftY = GoldenBall.getY();
        LeftX -= 38;

        GoldenBall.setBounds(LeftX, LeftY, 38, 38);
    }

    public void moveRight2() {

        int RightX = GoldenBall.getX();
        int RightY = GoldenBall.getY();
        RightX += 38;

        GoldenBall.setBounds(RightX, RightY, 38, 38);
    }

    public void moveUp2() {

        int UpX = GoldenBall.getX();
        int UpY = GoldenBall.getY();
        UpY -= 38;

        GoldenBall.setBounds(UpX, UpY, 38, 38);
    }

    public void moveDown2() {

        int DownX = GoldenBall.getX();
        int DownY = GoldenBall.getY();
        DownY += 38;

        GoldenBall.setBounds(DownX, DownY, 38, 38);
    }

    public void actionPerformed(ActionEvent movement) {
        if (movement.getSource() == buttonLeft);
        //buttoncompass.setIcon(west);

        {
            if (moveLeft1())
                moveLeft2();
        }

        if (movement.getSource() == buttonRight);
        //buttoncompass.setIcon(east);

        {
            if (moveRight1())
                moveRight2();
        }

        if (movement.getSource() == buttonUp);
        //buttoncompass.setIcon(north);

        {
            if (moveUp1())
                moveUp2();
        }

        if (movement.getSource() == buttonDown);
        //buttoncompass.setIcon(south);

        {
            if (moveDown1())
                moveDown2();
        }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • "*plz replay ASAP*" - Good questions are timeless. In general, SOers react somewhat allergic to the phrase "ASAP". --- "*i am a first year student to the university*" - You should ask your instructor/professor. I teach myself, I do not like it if students ask SO instead of me. Lesson learned: next time, start earlier and ask you instructor/professor. --- "*There are some minor error*" - The code is too much already. If it is buggy, i.e. will not compile, I doubt someone will take a closer look. – Turing85 Apr 20 '18 at 18:05
  • Some general remarks on your code: Don't put multiple variable declaration in one line. --- Give variables meaningful names, not `button1`, `button2`, ... --- Even if possible, never omit optional parentheses after `if`, `else`, `for`,... --- Variable- and field-names should always start with a lowercase letter --- [Don't compare objects with `==`](https://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java) – Turing85 Apr 20 '18 at 18:17
  • I would suggest taking this code, creating a backup of it, and then cutting out all material non-essential to demonstrating the problem. The process of creating a [mcve] will probably help you find the bug! – halfer Apr 20 '18 at 21:03

1 Answers1

0

First, follow the advices in the comments in the main question. Your code is a chaos (and i am not reviewing, i am just saying that no matter how much help you ask, few people will do it).

If you want to avoid the "only moving left" problem, here is how i would do it:

I would remove "implements ActionListener" from the class, and add the listeners with lambdas.

buttonUp.addActionListener(e->{
            System.out.println("I am moving up");
            if (moveUp1())
                moveUp2();
        });
 buttonLeft.addActionListener(e->{
            System.out.println("I am moving left");
            if (moveLeft1())
                moveLeft2();
        });
 buttonRight.addActionListener(e->{
            System.out.println("I am moving right.");
             if (moveRight1())
                 moveRight2();
        });
buttonDown.addActionListener(e->{
            System.out.println("I am moving down");
            if (moveDown1())
                moveDown2();
        });
George Z.
  • 6,643
  • 4
  • 27
  • 47