user7212768
user7212768

Reputation:

JLabel doesn't move

I've wanted to move a little Picture. It's an ImageIcon in a JLabel. My plan was to press a JButton executing an while loop which moves it every second 50px to the right.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class Game extends JFrame implements ActionListener{

JButton jbUUp;
JButton jbUDw;
JButton jbUSh;


public static ImageIcon shot;
public static ImageIcon spL;
public static ImageIcon spR;

public static JLabel LspL;
public static JLabel LspR;
public static JLabel Lshot;

public static int shPosUser = 150;
public static int shPosKI = 150;
public static int shXPosUser = 180;


Game(){setLayout(null);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       setSize(1000, 500);
       getContentPane().setBackground(Color.WHITE);
                   
       spR = new ImageIcon("src/spR.jpg");
       LspR = new JLabel(spR);
       LspR.setSize(170, 170);
       LspR.setLocation(820, shPosKI);
       LspR.setVisible(true);
                   
       spL = new ImageIcon("src/spL.jpg");
       LspL = new JLabel(spL);
       LspL.setSize(170, 170);
       LspL.setLocation(10, shPosUser);
       LspL.setVisible(true);
                   
       shot = new ImageIcon("src/shot.gif");
       Lshot = new JLabel(shot);
       Lshot.setSize(21, 15);
       Lshot.setLocation(shXPosUser, shPosUser + 77);
       Lshot.setVisible(false);
                   
       jbUUp = new JButton("U");
       jbUUp.setSize(60, 30);
       jbUUp.setLocation(10, 350);
       jbUUp.addActionListener(this);
       
       jbUDw = new JButton("D");
       jbUDw.setSize(60, 30);
       jbUDw.setLocation(10, 420);
       jbUDw.addActionListener(this);
       
       jbUSh = new JButton("S");
       jbUSh.setSize(60, 30);
       jbUSh.setLocation(10, 385);
       jbUSh.addActionListener(this);
                   
       add(LspR);
       add(LspL);
       add(Lshot);
       add(jbUUp);
       add(jbUDw);
       add(jbUSh);
       }

public void actionPerformed(ActionEvent e){if(e.getSource() == jbUUp){User.moveUp();} 

                                           if(e.getSource() == jbUDw){User.moveDown();}
                                           
                                           if(e.getSource() == jbUSh){User.shot();}
                                          }





}

These are the two classes

public class User {

User(){}

public static  void moveUp(){Game.shPosUser = Game.shPosUser - 10; 
                             Game.LspL.setLocation(10, Game.shPosUser);}

public static  void moveDown(){Game.shPosUser = Game.shPosUser + 10; 
                               Game.LspL.setLocation(10, Game.shPosUser);}

public static void shot(){Game.Lshot.setVisible(true);
                          while(Game.shXPosUser < 500){timeout(1000);
                                                       Game.shXPosUser = Game.shXPosUser + 50;
                                                       System.out.println(Game.shXPosUser);
                                                       Game.Lshot.setLocation(Game.shXPosUser, Game.shPosUser);
                                                        }
                          Game.Lshot.setVisible(false);
                          Game.shXPosUser = 180;
                          Game.Lshot.setLocation(Game.shXPosUser, Game.shPosUser );
                          }

public static void timeout(int time){try{Thread.sleep(time);}
                                     catch(Exception e){}
                              }

}

Now my problem is that the Picture doesn't move. The Coordinates are changed but it's not relocated.

Upvotes: 0

Views: 410

Answers (3)

camickr
camickr

Reputation: 324098

First of all:

  1. Post properly formatted code when you ask a question. The indentation of your code is all over the place. If you want us to take the time to read your code, then make sure the code is readable.

  2. Get rid of all the static variables. That is not how the static keyword should be used.

  3. Follow Java naming conventions. Variable names should NOT start with an upper case character.

My plan was to press a JButton executing an while loop wich moves it every second 50px to the right

You can't use a while loop. The Thread.sleep() inside the loop will prevent the GUI from repainting itself until the loop finishes executing, in which case you will only see the icon in its final position.

The solution is to use a Swing Timer. The Timer will generate an event at which time you can calculate the new location. You will then need to stop the Timer when you want the animation to finish.

Read the section from the Swing tutorial on How to Use Swing Timers for more information.

Upvotes: 1

EKW
EKW

Reputation: 2113

The JLabel is moving. The screen isn't being repainted, so it can't show the new position of the JLabel. Somewhere in the Game file, you need to call repaint(); to repaint the screen;

Upvotes: 1

ELITE
ELITE

Reputation: 5940

You have to repaint the frame to view changes, so call repaint() method of JFrame to update the ui.

for example,

if(e.getSource() == jbUUp){
    User.moveUp();
    repaint();
}

Hope it'll work.

Upvotes: 1

Related Questions