Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Elevator Simulation using GUI... HELP!

807580Aug 29 2008 — edited Jan 13 2010
I need immediate help on how to make the elevator move from floor to floor... this is not using java applet... I'll paste the program that I have done so far... I don't know how to use timers and listeners in order to make the Elevator move.... Wat should I do...


Here is my program:


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

setTitle("Elevator Simulation");
getContentPane().add(new ButtonPanel(), BorderLayout.WEST);
id = new JLabel(" Name: Abinaya Vasudevan Group:SE3");

getContentPane().add(id, BorderLayout.NORTH);
}

// Main method
public static void main(String[] args)
{
// Create a frame and display it
Elevator_Simulation frame = new Elevator_Simulation();
frame.setSize(800,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Elevator(frame));

//Get the dimension of the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;

int x = (screenWidth - frame.getWidth())/2;
int y = (screenHeight - 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));

for (int i=1; i<=8; i++)
{
b[8-i] = new JButton("F"+(8-(i-1)));
b[8-i].addActionListener(this);
add(b[8-i]);
}
}

public void actionPerformed(ActionEvent e)
{
//handle the button pressing events
bp[8-((int)(e.getActionCommand().charAt(1)))] = true;

}
} //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
tm = new Timer(1000, this);
tm.setInitialDelay(300);
tm.start();

}

// Paint elevator area
public void paintComponent(Graphics g)
{
//obtain geometric values of components for drawing the elevator area
xco = getWidth()/2-10;
yco = getHeight()/2-20;
width = 10;
height = 20;
//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
g.setColor(Color.magenta);
g.drawLine(0,0,getWidth(), 0);
g.drawLine(0,93,getWidth(), 93);
g.drawLine(0,186,getWidth(), 186);
g.drawLine(0,279,getWidth(), 279);
g.drawLine(0,372,getWidth(), 372);
g.drawLine(0,465,getWidth(), 465);
g.drawLine(0,558,getWidth(), 558);
g.drawLine(0,651,getWidth(), 651);
g.drawLine(0,744,getWidth(), 744);
g.drawLine(0,837,getWidth(), 837);

g.setColor(Color.black);
g.fill3DRect(getWidth()/2 - 50, 93, 100, 93, true);
g.setColor(Color.magenta);
g.drawLine(getWidth()/2, 93, getWidth()/2, 186);
}

//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 elevetor movement
//change moving direction when hits the top and bottom
//repaint the panel
//update the state of the elevator
}
} //the end of Elevator class


The skeleton was provided but I am still not sure how to do it... So pls help... asap....
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 10 2010
Added on Aug 29 2008
16 comments
2,209 views