Search code examples
javaswingjlabelgrid-layoutimageicon

Why is my JLabel grid not functioning properly?


I'm trying to make a JLabel grid that updates everytime a button is pressed. Each button(north, south, east, west) moves an image from one JLabel to another position on the grid. I've written and re-written the code over and over, but I still can't get it to update properly. Why does the ImageIcon apear sometimes and not others? Where is my logic flawed?

Also, I did look at a bunch of other questions and such about this problem, but none of them help me...

Any and all help would be greatly apreciated.

ImageIcon man;
ImageIcon grass;
public int xPosition=0;
public int yPosition=0;

    class ButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent evt) {

        if(evt.getActionCommand() == Actions.east.name()){
            System.out.println("east!");
            if(yPosition<4){
                xPosition++;
            }
        }
        if(evt.getActionCommand() == Actions.west.name()){
            System.out.println("west!");
            if(yPosition>0){
                xPosition--;
            }
        }
        if(evt.getActionCommand() == Actions.north.name()){
            System.out.println("north!");
            if(xPosition>0){
                yPosition--;
            }
        }
        if(evt.getActionCommand() == Actions.south.name()){
            System.out.println("south!");
            if(xPosition<4){
                yPosition++;
            }
        }
        URL imageMan = getClass().getResource("man.png");
        man= new ImageIcon(imageMan);

        URL imageGrass = getClass().getResource("grass.jpg");
        grass= new ImageIcon(imageGrass);

        int row=0;            
        if(row==0){
            while(row<=5){
                if(yPosition == row){
                    for(int i=0;i<=5;i++){
                        if(i==xPosition){
                            points[i][row].setIcon(man);
                        }
                        else{
                            points[i][row].setIcon(grass); 
                        }
                    }
                }
                else{
                    for(int i=0;i<=5;i++){
                        points[i][row].setIcon(grass);
                    }
                    row++;
                }
            }
        }
        row=0;
        System.out.println("codinates: ("+xPosition+","+yPosition+")");
    }
}

Here is a screen shot of what I want the outcome to be, with the man being able to move around the screen using the buttons.

screen shot


Solution

  • I figured out the problem and was able to simplify the code significantly.

    Here is the new updated code that works:

    ImageIcon man;
    ImageIcon grass;
    public int xPosition=0;
    public int yPosition=0;
    public int oldX =0;
    public int oldY = 0;
    
        class ButtonListener implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent evt) {
            oldX = xPosition;
            oldY = yPosition;    
            if(evt.getActionCommand() == Actions.east.name()){
                System.out.println("east!");
                if(xPosition<4){
                    xPosition++;
                }
                else{
                    System.out.println("can't go east!");
                }
            }
            if(evt.getActionCommand() == Actions.west.name()){
                System.out.println("west!");
                if(xPosition>0){
                    xPosition--;
                }
                else{
                    System.out.println("can't go west!");
                }
            }
            if(evt.getActionCommand() == Actions.north.name()){
                System.out.println("north!");
                if(yPosition>0){
                    yPosition--;
                }
                else{
                    System.out.println("can't go north!");
                }
            }
            if(evt.getActionCommand() == Actions.south.name()){
                System.out.println("south!");
                if(yPosition<4){
                    yPosition++;
                }
                else{
                    System.out.println("can't go south!");
                }
            }
            URL imageMan = getClass().getResource("man.png");
            man= new ImageIcon(imageMan);
    
            URL imageGrass = getClass().getResource("grass.jpg");
            grass= new ImageIcon(imageGrass);
    
            points[oldX][oldY].setIcon(grass);
            points[xPosition][yPosition].setIcon(man);
    
            System.out.println("codinates: ("+xPosition+","+yPosition+")");
        }
    }