0% found this document useful (0 votes)
75 views3 pages

Simple Elevator Java

This Java code simulates an elevator system with multiple classes. The elevat class creates an Elevator panel and ButtonPanel to control it. The Elevator class represents the elevator and handles its movement between floors over time. The ButtonPanel class contains floor buttons that call the elevator's move method when clicked to change its destination floor.

Uploaded by

Zaheer Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views3 pages

Simple Elevator Java

This Java code simulates an elevator system with multiple classes. The elevat class creates an Elevator panel and ButtonPanel to control it. The Elevator class represents the elevator and handles its movement between floors over time. The ButtonPanel class contains floor buttons that call the elevator's move method when clicked to change its destination floor.

Uploaded by

Zaheer Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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);
}
}
}
}
}

You might also like