Hi guys,
i am trying to implement elevator simulation but stuck in some ways.
1st. i would like to set the background of my elevator class area into yellow but when i tried to do so by typing app.setBackground(COLOR.YELLOW) under class Elevator, i unable to change it.
2nd. i tried to simulate the movement of the elevator moving up and down using timer but under paint component i already set the elevator area to be yco = height * 7 which will make the elevator in the 1st floor but then it will makes the timer does not work. but if i delete the yco = height * 7, it will work but it will at the top floor.
the codes:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//The main class
public class Elevator_Simulation extends JFrame {
public JLabel state; // display the state of the elevator
private JLabel id; //your name and group
public ButtonPanel control; //the button control panel
private Elevator elevator; // the elevator area
//constructor
public Elevator_Simulation() {
// Create GUI
//container for all the panels
Container container = getContentPane();
//name and group (with a color)
id = new JLabel("Name : Anthony Salim Kwang", SwingConstants.CENTER);
id.setForeground(Color.red);
JPanel id_panel = new JPanel();
id_panel.add(id);
container.add(id, BorderLayout.NORTH);
//all the 8 buttons panel
control = new ButtonPanel();
JPanel control_panel = new JPanel();
control_panel.add(control);
container.add(control, BorderLayout.WEST);
//state of elevator (up/down)
state = new JLabel("elevator up/down", SwingConstants.CENTER);
JPanel state_panel = new JPanel();
state_panel.add(state);
container.add(state, BorderLayout.SOUTH);
}
// Main method
public static void main(String[] args) {
// Create a frame and display it
Elevator_Simulation frame = new Elevator_Simulation();
frame.setTitle("lab1: Elevator Simulation");
frame.setSize(400, 300);
//add the elevator area
frame.getContentPane().add(new Elevator(frame), BorderLayout.CENTER);
//exit upon closing
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//center the frame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screenSize.width - frame.getWidth()) / 2;
int y = (screenSize.height - frame.getHeight()) / 2;
frame.setLocation(x, y);
frame.setVisible(true);
}
} //the end of Elevator_Simulation class
//The ButtonPanel class receives and handles button pressing events
class ButtonPanel extends JPanel implements ActionListener {
public JButton b[] = new JButton[8]; // 8 Buttons
public boolean bp[] = new boolean[8]; // the state of each button, pressed or not
//constructor
public ButtonPanel() {
//create GUI
setLayout(new GridLayout(8, 1));
//create all the 8 buttons and register button to listener event
for (int i = 1; i <= b.length; i++) {
b[8 - i] = new JButton("F" + Integer.toString(8 - (i - 1)));
b[8 - i].setBackground(Color.cyan);
b[8 - i].addActionListener(this);
add(b[8 - i]);
}
}
public void actionPerformed(ActionEvent e) {
//handle the button pressing events
int buttonNumber = Integer.parseInt(String.valueOf(e.getActionCommand().charAt(1)));
if(bp[8 - buttonNumber] = true) {
System.out.println(bp[8 - buttonNumber]);
b[buttonNumber-1].setBackground(Color.red);
}
}
} //the end of ButtonPanel class
// The elevator class draws the elevator area and simulates elevator movement
class Elevator extends JPanel implements ActionListener {
//Declaration of variables
private Elevator_Simulation app; //the Elevator Simulation frame
private boolean up; // the elevator is moving up or down
private int width; // Elevator width
private int height; // Elevator height
private int xco; // The x coordinate of the elevator's upper left corner
private int yco; // The y coordinate of the elevator's upper left corner
private int dy0; // Moving interval
private int topy; //the y coordinate of the top level
private int bottomy; // the y coordinate of the bottom level
private Timer tm; //the timer to drive the elevator movement
//other variables to be used ...
//constructor
public Elevator(Elevator_Simulation app) {
//necessary initialization
dy0 = 10;
tm = new Timer(1000, this);
tm.start();
}
// Paint elevator area
public void paintComponent(Graphics g) {
width = getWidth() / 8;
height = getHeight() / 8;
//put elevator in middle and bottom floor
xco = getWidth() / 2 - 45 ;
yco = height * 7;
topy = 0;
bottomy = height * 7;
//clear the painting canvas
super.paintComponent(g);
//start the Timer if not started elsewhere
if (!tm.isRunning()) {
tm.start();
}
//draw horizontal lines and the elevator
for (int i = 0; i <= 8; i++) {
g.setColor(Color.red);
g.drawLine(0, height * i, getWidth(), height * i);
}
g.setColor(Color.GRAY);
g.fillRect(xco, yco, width, height);
g.setColor(Color.RED);
g.drawLine(xco + (width / 2), yco, xco + (width / 2), (yco + height));
}
//Handle the timer events
public void actionPerformed(ActionEvent e) {
//loop if the elevator needs to be stopped for a while
//adjust Y coordinate to simulate elevator movement
if(up) {
yco -= dy0;
//System.out.println(yco);
}
else {
yco += dy0;
}
//change moving direction when hits the top and bottom
//repaint the panel
repaint();
//update the state of the elevator
}
}
//the end of Elevator class
Hope you guys able to give me feedback on how should i solve this problem or maybe give some advices. Thanks much.