Results 1 to 5 of 5
Thread: Simulating an elevator
- 03-04-2011, 10:44 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Simulating an elevator
Hey all, I'm trying to program an "elevator" to move up and down in a frame based on button presses. My problem is that for some reason the elevator won't move to the correct floor. In fact it's moving to locations between floors. I'm sure it's just something stupid I've done--does anyone have any ideas?
Thanks!
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Q1813 extends JApplet { private Elevator elevator = new Elevator(); public void init() { // Create left buttons ButtonPanel lb = new ButtonPanel(elevator); //Create right buttons ButtonPanel rb = new ButtonPanel(elevator); // Set applet layout setLayout(new BorderLayout()); add(lb, BorderLayout.WEST); add(rb, BorderLayout.EAST); add(elevator, BorderLayout.CENTER); } // Enable the applet to run standalone // Main method public static void main(String[] args) { // Create a frame JFrame frame = new JFrame("Elevator Simulation"); // Create an instance of the applet Q1813 applet = new Q1813(); // Add the applet instance to the frame frame.add(applet, BorderLayout.CENTER); // Invoke init() and start() applet.init(); applet.start(); // Display the frame frame.setSize(800, 500); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } //The elevator class encapsulates elevator operations class Elevator extends JPanel { private boolean isUp; // Moving up or down? private int numberFloors=7; private int destinationFloor = 0; // Elevator destination floor private int currentFloor=7; private int width = 30; // Elevator width private int height; // Elevator height private int x = 50; // The x coordinator of the elevator's upper left corner private int currentY = 0 ; // Elevator current location private int dy = 4; // Moving interval private Timer timer = new Timer(200, new Listener()); // Run the while loop on a separate thread public Elevator() { setBackground(Color.yellow); // Set elevator background color } // 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) // Moving up isUp = true; else // Moving down isUp = false; timer.start(); } class Listener implements ActionListener { public void actionPerformed(ActionEvent e) { if (isUp) { // Moving up if (currentFloor < destinationFloor) { currentY = currentY - dy; repaint(); } else timer.stop(); } else { // Moving down if (currentFloor > destinationFloor) { currentY=currentY+dy; repaint(); } else timer.stop(); } } } // Paint elevator public void paintComponent(Graphics g) { super.paintComponent(g); //System.out.println("currentY="+currentY); height = getHeight() /numberFloors; currentFloor=(int)(numberFloors-numberFloors*((double)currentY/getHeight())); //System.out.println("currentfloor="+currentFloor); g.fillRect(x, currentY, width, height); } } //The ButtonPanel encapsulates buttons class ButtonPanel extends JPanel { private Elevator elevator; // The elevator private JButton floor7=new JButton("Floor 7"); private JButton floor6=new JButton("Floor 6"); 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(7,1)); add(floor7); add(floor6); add(floor5); add(floor4); add(floor3); add(floor2); add(floor1); setBackground(Color.blue); floor7.addActionListener(new ButtonListener()); floor6.addActionListener(new ButtonListener()); 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()==floor7) // pressed floor7 button { elevator.move(7); } if (e.getSource()==floor6) // pressed floor6 button { elevator.move(6); } if (e.getSource()==floor5) // pressed floor5 button { elevator.move(5); } if (e.getSource()==floor4) // pressed floor4 button { elevator.move(4); } if (e.getSource()==floor3) // pressed floor3 button { elevator.move(3); } if (e.getSource()==floor2) // pressed floor2 button { elevator.move(2); } if (e.getSource()==floor1) //pressed floor1 button { elevator.move(1); } } } } }
- 03-04-2011, 11:24 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
My problem is that for some reason the elevator won't move to the correct floor. In fact it's moving to locations between floors.
Running the program it seems that the elevator stops when its base is first inside any part of the target floor.
I wonder if
isn't suspect in some way. I would throw System.out.println()s in there and check that currentFloor is being calculated correctly. (Define, in advance, what you mean for the elevator to "be" at a given floor.)Java Code:currentFloor=(int)(numberFloors-numberFloors*((double)currentY/getHeight()));
------------------------
Why add the elevators and buttons to an applet and then add the applet to a frame? (I'm not saying this is wrong - it's not something I've come accross before, just asking). I would have added the panel to a frame or an applet depending on what was wanted.
Also I find it a bit odd to calculate the floor in the paintComponent() method. SHouldn't that method just paint? You might find it easier to model the behaviour of the elevator in a class that is quite separate from the gui.
- 06-04-2011, 01:40 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
Hi mate, I try your code, and find a way to fix the problem, It looks like need one more condition to check when elevator going down
as below:
if (isUp) { // Moving up
if (currentFloor < destinationFloor) {
currentY = currentY - dy;
repaint();
}
else
timer.stop();
}
else
{ // Moving down
if (currentFloor > destinationFloor)
{
currentY = currentY + dy;
repaint();
}
else if(currentFloor == destinationFloor)
{
currentY = currentY + dy;
repaint();
}
else
timer.stop(); }
}
}
hope have some help.
- 06-04-2011, 05:32 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Years ago I was in Spain in an ancient building which had an ancient elevator. It responded just like your simulation does: when the elevator had reached a certain floor, more or less, it stopped and you had to crawl out and jump down to the floor or climb a bit up to be able to reach the floor. Elderly people who couldn't make it had to jump up and down in the elevator to adjust its level somewhat. It was a fun elevator ;-)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-05-2011, 10:43 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
java program simulating darts
By conor147 in forum New To JavaReplies: 14Last Post: 01-18-2010, 02:38 AM -
simulating netstat
By prashant in forum NetworkingReplies: 1Last Post: 03-14-2009, 07:41 AM -
Simulating Brownian Motion
By ixhabbaba in forum Java AppletsReplies: 2Last Post: 11-11-2007, 08:53 PM


LinkBack URL
About LinkBacks
Reply With Quote.gif)

Bookmarks