import java.awt.
*;
import java.awt.event.*;
import javax.swing.*;
public class elevat extends JApplet {
private Elevator elevator = new Elevator();
public void init() {
ButtonPanel lb = new ButtonPanel(elevator);
setLayout(new BorderLayout());
add(lb, BorderLayout.WEST);
add(elevator, BorderLayout.CENTER);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Elevator Simulation");
elevat applet = new elevat();
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(800, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class Elevator extends JPanel {
private boolean isUp;
private int numberFloors=5;
private int destinationFloor = 0;
private int currentFloor=5;
private int width = 30;
private int height;
private int x = 50;
private int currentY = 0 ;
private int dy = 4; // Moving interval
private Timer timer = new Timer(200, new Listener());
public Elevator() {
setBackground(Color.gray);
}
// Set elevator color
public void setColor(Color color) {
setForeground(color);
}
// Move the elevator to destination
public void move(int toFloor) {
destinationFloor = toFloor;
//System.out.println("destinationfloor="+destinationFloor);
move();
}
private void move() {
if (destinationFloor > currentFloor)
isUp = true;
else // Moving down
isUp = false;
timer.start();
}
class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (isUp) {
if (currentFloor < destinationFloor) {
currentY = currentY - dy;
repaint();
}
else
timer.stop();
}
else {
if (currentFloor > destinationFloor) {
currentY=currentY+dy;
repaint();
}
else
timer.stop();
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
height = getHeight() /numberFloors;
currentFloor=(int)(numberFloors-numberFloors*((double)currentY/getHeight()));
g.fillRect(x, currentY, width, height);
}
}
class ButtonPanel extends JPanel {
private Elevator elevator;
private JButton floor5=new JButton("Floor 5");
private JButton floor4=new JButton("Floor 4");
private JButton floor3=new JButton("Floor 3");
private JButton floor2=new JButton("Floor 2");
private JButton floor1=new JButton("Floor 1");
ButtonPanel(Elevator elevator) {
// Pass the elevator, frame, status to the button panel
this.elevator = elevator;
setLayout(new GridLayout(5,1));
add(floor5);
add(floor4);
add(floor3);
add(floor2);
add(floor1);
setBackground(Color.blue);
floor5.addActionListener(new ButtonListener());
floor4.addActionListener(new ButtonListener());
floor3.addActionListener(new ButtonListener());
floor2.addActionListener(new ButtonListener());
floor1.addActionListener(new ButtonListener());
}
class ButtonListener implements ActionListener {
// Handle button actions
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton)
{
if (e.getSource()==floor5)
{
elevator.move(5);
}
if (e.getSource()==floor4)
{
elevator.move(4);
}
if (e.getSource()==floor3)
{
elevator.move(3);
}
if (e.getSource()==floor2)
{
elevator.move(2);
}
if (e.getSource()==floor1)
{
elevator.move(1);
}
}
}
}
}