sdfg
sdfg

Reputation: 23

Game with multiple buttons in Java

EDIT: Solved, thanks to MGorgon.

I'm creating a simple game that(for the moment) allows you to move a dot 1 step in each direction of the current position with mouse clicks. However this only works when the board is a square(you can chose the size of the board). The board is represented by buttons with images and a 2D array to keep track of the position where '1' is the position of the dot and '0' is a free space. Pos1 and Pos2 in the actionlistener prints out the position of the button which is clicked. So, this works great when the game board is a square but when I change the board into rectangles I get incorrect pos1 and pos2 values which I'm using to move the dot.

    //creates the array and sets dot start position.
    for(int i=0;i<xAxis;i++)
    {
        for(int j=0;j<yAxis;j++)
        {           
            board[i][j]=0;
        }               
    }
    board[(xAxis/2)][(yAxis/2)]=1;

,

    //Creates buttons and adds actionslistener and images to the buttons.
    for(int i=0;i<xAxis;i++)
    {                   
        for(int j=0;j<yAxis;j++)
        {
            buttons[i][j] = new JButton();
            buttons[i][j].setIcon(circleIMG);
            buttons[i][j].setBorderPainted(false);
            buttons[i][j].addActionListener(GBC);
            buttons[i][j].setPreferredSize(new Dimension(52,52));  
            buttons[i][j].setRolloverIcon(circleDotIMG);
            buttons[i][j].setRolloverEnabled(true);

            GameBoardPanel.add(buttons[i][j]);  
        }       
    }

    buttons[(xAxis/2)][(yAxis/2)].setIcon(circleDotIMG);

.

    //implements the actionlistener
    public class GridButtonClick implements ActionListener
    {   
    //int xControl=0;       
    int pos1=0;
    int pos2=0;
    boolean Move = false;

    public void actionPerformed(ActionEvent e) 
    {   
        int posx=FindXpos(board);
        int posy=FindYpos(board);           

        for(int i=0;i<xAxis;i++)
        {
            for(int j=0;j<yAxis;j++)
            {
                if(buttons[i][j] == e.getSource())
                {                       
                    pos1=i;
                    pos2=j;
                    //System.out.println("posx: "+posx);
                    //System.out.println("posy: "+posy);
                    System.out.println("pos1: "+pos1);
                    System.out.println("pos2: "+pos2);
                    System.out.println("\n");


                        if((pos1 == posx+1 && pos2 == posy+1) || (pos1 == posx-1 && pos2 == posy-1))
                            Move=true;                                                              
                        if((pos1 == posx+1 && pos2 == posy-1) || (pos1 == posx-1 && pos2 == posy+1))
                            Move=true;                                      
                        if((pos1 == posx+1 && pos2 == posy) || (pos1 == posx-1 && pos2 == posy))
                            Move=true;                                  
                        if((pos2 == posy+1 && pos1 == posx) || (pos2 == posy-1 && pos1 == posx))
                            Move=true;                      



                    if(Move)
                    {
                        if(yAxis>xAxis)
                        buttons[(xAxis/2)][(yAxis/2)-1].setIcon(circleIMG);

                        else if((xAxis>yAxis) || (xAxis==yAxis))
                        buttons[(xAxis/2)][(yAxis/2)].setIcon(circleIMG);


                        RemovePreviousMark(board);
                        board[i][j] = 1;
                        buttons[i][j].setIcon(circleDotIMG);
                        System.out.println("Button press: "+"["+i+"]"+"["+j+"]");
                        //System.out.println("x: "+i+" "+"y "+j);
                        Move=false;
                    }
                }
            }
        }   
    }       
}

Upvotes: 0

Views: 265

Answers (1)

Tim B
Tim B

Reputation: 41168

This seems very complicated.

I'm not sure from the code listed whether you already are, but if you aren't you should remember the current position in a currX and currY field. Then to move all you need to do is blank out the current location, change the variables appropriately, then set the new location.

Make your button objects your own subclass of button and store an x,y co-ordinate inside the button. Then you immediately know the co-ordinates of the button click without having to scan the array.

Upvotes: 1

Related Questions