import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import java.lang.*; public class BasicRecipeJ2D extends Frame { public BasicRecipeJ2D() throws InterruptedException { //constructor super("Elevator Algorithm"); myCustomCanvas canvas = new myCustomCanvas(); this.add(canvas); this.setSize(700,400); this.setVisible(true); addWindowListener(new WindowEventHandler()); //Setup the table LinkedList[] table = setup(); //print the table for testing //print(table); boolean direction = true; boolean flag = false; boolean start = true; int headPosition = 5; canvas.setPosition(headPosition); //System.out.println("Repaint"); Thread.sleep(500); do { /** Moving upward */ if(direction == true) { if(table[headPosition].size() == 0) { /** If no request for the current cylinder then go to next or if at the end of the cylinders change direction */ if(headPosition == (table.length-1)) { direction = false; canvas.increaseX(false); //System.out.println("Repaint"); //Thread.sleep(500); } else { if(start == true) { start = false; headPosition++; //System.out.println("Repaint"); //Thread.sleep(500); } else { headPosition++; canvas.increaseX(false); //System.out.println("Repaint"); //Thread.sleep(500); } } } else { table[headPosition].clear(); //System.out.println(headPosition); /** checking to see if head is at the end of the cylinders */ if(headPosition == (table.length-1)) { direction = false; canvas.increaseX(true); //System.out.println("Repaint"); Thread.sleep(1500); } else { if(start == true) { start = false; headPosition++; //System.out.println("Repaint"); Thread.sleep(1500); } else { headPosition++; canvas.increaseX(true); //System.out.println("Repaint"); Thread.sleep(1500); } } } } /** Moving downward */ else if(direction == false) { if(table[headPosition].size() == 0) { /** If no request for the current cylinder then go to next or if at the end of the cylinders then end */ if(headPosition == 0) { flag = true; //Thread.sleep(500); //System.out.println("Repaint"); } else { headPosition--; //Thread.sleep(500); canvas.decreaseX(false); //System.out.println("Repaint"); } } else { table[headPosition].clear(); //System.out.println(headPosition); /** if the at the bottom of the cylinders it ends */ if(headPosition == 0) { /** ends the program */ flag = true; canvas.decreaseX(true); //System.out.println("Repaint"); } else { headPosition--; //Thread.sleep(1500); canvas.decreaseX(true); //System.out.println("Repaint"); } } } }while(flag == false); } class WindowEventHandler extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public static void main(String[] args) throws InterruptedException { new BasicRecipeJ2D(); } public static LinkedList[] setup(){ //Create starting load of requests int[] startingRequests = new int[6]; for(int i=0;i<6;i++){ startingRequests[i] = randomNumber(11); } //Create Array of linked lists LinkedList[] table = new LinkedList[11]; for(int i = 0; i < table.length; i++) { table[i] = new LinkedList(); } //Put starting requests into array of linked lists for(int i = 0; i < startingRequests.length; i++){ table[startingRequests[i]].add(startingRequests[i]); } return table; } public static int randomNumber(int input){ Random randomGenerator = new Random(); int randomInt = randomGenerator.nextInt(input); return randomInt; } public static void print(LinkedList[] table){ //Print array of linked list for(int i = 0; i < table.length; i++){ Object[] current = table[i].toArray(); for(int j = 0; j < current.length; j++){ System.out.print(current[j] + ", "); } System.out.println(); } } } //Part 2; Java 2D specific-extend the drawing Component -Canvas- // and override it's paint method. class myCustomCanvas extends Canvas { float x = 0.0f; public void paintComponent(Graphics g){ } public void paint(Graphics g) { //System.out.println("in paint"); // step one of the recipe; cast Graphics object as Graphics2D Graphics2D g2d = (Graphics2D) g; float inc = 0; g2d.setColor(Color.black); g2d.fill(new Rectangle2D.Float(610.0f ,140.0f,60.0f,70.0f)); for(int k = 0; k < 11; k++){ g2d.setColor(Color.black); g2d.drawString("" + k, 10.0f + inc , 220.0f); // draws text g2d.fill(new Rectangle2D.Float(0.0f + inc ,140.0f,60.0f,70.0f)); //draws black background g2d.setColor(Color.GRAY); g2d.fill(new Rectangle2D.Float(10.0f + inc ,150.0f,50.0f,50.0f)); //draws gray squares, used to represent locations inc += 60.0f; } g2d.setColor(Color.yellow); g2d.fill(new Rectangle2D.Float(70.0f + x,150.0f,50.0f,50.0f)); //animated arm location } public void increaseX(boolean print){ x+=60.0f; if(print) { repaint(); } } public void decreaseX(boolean print) throws InterruptedException{ if(print) { repaint(); Thread.sleep(1500); } x-=60.0f; } public void setPosition(int start){ for(int i = 0;i < start-1; i++){ x+=60.0f; } } }