// class ElevatorPanel.java //:Assignment 4: //:Student Name: Duo Zhou //:Studnet Number: 722452 /**FileName: ElevatorPanel.java * Date: July, 27, 2000 * Purpose: Learn how to use JFrame, JPanel, Synchronized method, creating * multithread, the basic idea of creating GUI * Name of Files: This assignment include Building.java, ConnectedWall.java, * Elevator.java, ElevatorFrame.java, ElevatorPanel.java, * Floor.java, MailDeliveryApp.java, MailPiece.java, * Position.java, Robot.java * Brief Discription: This file is added to simulate the movement of an elevator. */ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.awt.geom.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; import java.lang.*; class ElevatorPanel extends JPanel { private int currFloor; //user current Floor; private int targetFloor; //user target floor; private int r; //row; private int c; //col; private int width; //width of the container to hold the elevator; private int height; //height of the container to hold the elevator; private int level; //current pixel position of the up left corner of the elevator; protected int diff;//the pixel difference of the user target floor and the user current floor; protected static Building building; protected Image humanImage; protected Image robotImage; public ElevatorPanel(int cf, int tf, Building b) { this.building = b; currFloor=cf; targetFloor=tf; r=80; c=25; //this is used to the elevator in the middle of it container; width = 100; height = 150; //width and height of the container to hold the elevator; if (targetFloor-currFloor >=0){ //if human's target floor is above the next floor, level=r+10+(3-currFloor)*height; //calculate the pixel position of the up left //corner of the elevator; diff=(targetFloor-currFloor)*height; //the pixel difference of the user target //floor and the user current floor; } else { level=r+10+(3-currFloor)*height; diff=(currFloor-targetFloor)*height; //if the user want to move down, then the diff must //be current floor number - target floor number; } Toolkit tk=Toolkit.getDefaultToolkit(); //get Toolkit; humanImage=tk.getImage("human.gif"); //get human image; robotImage=tk.getImage("robot0.gif"); setBackground(Color.black); //set background to white; Thread f_runner=new Thread() { //create a new thread to move the elevator; public void run() { while (diff>=0) { Elevator currentElevator=building.getElevator(); //the elevator is synchronized try {Thread.sleep(1);} //sleep 1 millisecond once the elevator moved one pixel; catch(InterruptedException ex) {}; ElevatorPanel.this.paintImmediately(c, level-5, 150, height+5); //paint this elevator //once a step it moved; } } }; f_runner.start(); //setSize(150, 700); } //*************************** set the size of the panel; ******************************* public Dimension getPreferredSize() { return new Dimension(150, 700); } public Dimension getMaximumSize() { return getPreferredSize(); } public Dimension getMinimumSize() { return getPreferredSize(); } //**************************** the paintComponent method ******************************* public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.white); g.drawRect(c, r, width, height); //use rectangle to create a container for the elevator, //at the 3rd floor; g.drawRect(c, r+height, width, height); //create a container at the 2nd floor; g.drawRect(c, r+2*height, width, height);//create a container at the 1st floor; g.drawRect(c, r+3*height, width, height);//create a container at the basement; if (targetFloor-currFloor >=0){ //if human's target floor is above the next floor, //continues moveing human up; g.setColor(Color.yellow); g.drawRect(c+10, level, width-20, height-20); //draw the elevator, moving up; if ((building.getRobot().getRow()>25) && (building.getRobot().getCol()>25) && (building.getRobot().getFloorNum()==currFloor)) g.drawImage(robotImage, 55, level+75, this); g.drawImage(humanImage, 80, level+40, this); //draw the human image in the elevator level--; diff--; //update the difference from the bottom of the elevator to the //bottom of the next floor, and current pixel position of the elevator; } else if (targetFloor-currFloor <=0){ g.setColor(Color.yellow); g.drawRect(c+10, level, width-20, height-20); //draw the elevator, moving down; g.drawImage(humanImage, 75, level+50, this);// the bottom of the upper floor will become closer; level++; diff--; } else return; } }