I am learning java for like 3 months already, and this is my second project in swing. My problem now is that I don't know how to make a JButton move some image by redrawing it, to make it look like a motion movement, so far everything I could do is make it move while hovering on the button, or when I keep it pressed, but the motion doesn't happen just when I click the button. Basically the button is supposed to be part of an elevator console, which by clicking on it makes the elevator move to the specified floor. Lemme know if u have an idea, or if it is possible at all to do what I am looking for. Maybe this whole thing has to be done in a completely different manner, I am open to suggestions and I appreciate the help.
Many thanks, and kind regards!
Things I tried:
◘ JButton.getModel().isArmed();
◘ JButton.getModel().isRollOver();
◘ JButton.getModel().isClicked();
◘ while loop(it made the elvator teleport directly) - in button's action listener and in the method:
JButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
while(yPosition!=300){
yPosition = yPosition - yVelocity;
repaint();
}
}
} );
◘ for loop - as in the button's action listener and in the method:
floor1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(int i = 500; i <= 300; i--){
yPosition = yPosition - yVelocity;
repaint();
}
}
} );
/////////////////////////////////////////////////////////////////////////
Actual code(there is a separate frame class where this panel is added to):
// MainClass
public class Main {
public static void main(String[] args) {
new Frame();
}
}
// FrameClass
import javax.swing.*;
public class Frame extends JFrame {
Frame(){
ElevatorPanel elevatorPanel = new ElevatorPanel();
this.setSize(elevatorPanel.PANEL_WIDTH, elevatorPanel.PANEL_HEIGHT);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.add(elevatorPanel);
this.setVisible(true);
}
}
// PanelClass
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ElevatorPanel extends JPanel implements ActionListener {
/*final int r = 1;
final int g = 100;
final int b = 100;
final int a = 100;
*/
final int PANEL_HEIGHT = 800;
final int PANEL_WIDTH = 800;
final int BUTTON_HEIGHT = 40;
final int BUTTON_WIDTH = 130;
int xVelocity = 1;
int yVelocity = 1;
int xPosition = 120;
int yPosition = 500;
Image elevator;
JButton groundLvl = new JButton();
JButton floor1 = new JButton();
JButton floor2 = new JButton();
ElevatorPanel() {
Timer time = new Timer(20, this);
time.start();
elevator = new ImageIcon("Elevator\\Resources\\elevator.png").getImage();
groundLvl.setBounds(500, 550, BUTTON_WIDTH, BUTTON_HEIGHT);
groundLvl.setText("Ground level");
groundLvl.setFocusable(false);
floor1.setBounds(500, 500, BUTTON_WIDTH, BUTTON_HEIGHT);
floor1.setText("1st Floor");
floor1.setFocusable(false);
floor1.addActionListener(this);
/*floor1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(int i = 500; i <= 300; i--){
yPosition = yPosition - yVelocity;
repaint();
}
}
} );
*/
floor2.setBounds(500, 450, BUTTON_WIDTH, BUTTON_HEIGHT);
floor2.setText("2nd Floor");
floor2.setFocusable(false);
this.add(groundLvl);
this.add(floor1);
this.add(floor2);
this.setOpaque(false);
this.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
this.setLayout(null);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
//g2D.drawImage(bg,0,0,null);
g2D.drawImage(elevator, xPosition, yPosition, null);
}
@Override
public void actionPerformed(ActionEvent ex) {
if (ex.getSource() == floor1) {
yPosition = yPosition - yVelocity;
repaint();
}
}
}