Results 1 to 5 of 5
  1. #1
    intrepid604 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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

    Java Code:
    currentFloor=(int)(numberFloors-numberFloors*((double)currentY/getHeight()));
    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.)

    ------------------------

    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.

  3. #3
    xinnkl is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    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.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    xinnkl is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    I find this problem as well, as a good elevator, this maybe kill somebody sometime, so It should be a good solution when elevator during going down process the last step is up one more step to keep right position.

    thank your remian me.
    [/I]

Similar Threads

  1. java program simulating darts
    By conor147 in forum New To Java
    Replies: 14
    Last Post: 01-18-2010, 03:38 AM
  2. simulating netstat
    By prashant in forum Networking
    Replies: 1
    Last Post: 03-14-2009, 08:41 AM
  3. Simulating Brownian Motion
    By ixhabbaba in forum Java Applets
    Replies: 2
    Last Post: 11-11-2007, 09:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •