I'm a relatively new programmer, so bear with me. I created a JFrame with the image of the game's main character. But I can't figure out how to move it (by adding values to the x axis, like x++ or x--). Is there anyway to do this?
Adding the character:
final JLabel carl = new JLabel("");
carl.setIcon(new ImageIcon(gui.class.getResource("/main/carl.png")));
carl.setBounds(12, 90, 64, 69);
levelOne.add(carl);
Moving the character on button click (I have it set to move to specified coords at the moment):
JButton RightButtonLevelOne = new JButton("Move right");
RightButtonLevelOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
carl.setBounds(24, 90, 64, 69);
}
});
Thanks in advance.
public void actionPerformed(ActionEvent arg0) {
Rectangle bounds = carl.getBounds(null);
bounds.setLocation(bounds.getX() + 1, bounds.getY());
carl.setBounds(bounds);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.