/* The following program is the solution for the problem of Esc101 Lab. Assginment on Thursday of week of 23/08/04 */ import java.awt.* ; import javabook.* ; class Clock // Clock class { // variables for storing the center of the clock and angles made by the // hours and minutes hand with the positive x-axis. int xPos; int yPos; double hAngle; double mAngle; // Function to set the x-cordinate of the center of the clock public void setx(int x) { xPos = x; } // Function to set the y-cordinate of the center of the clock public void sety(int y) { yPos = y; } // Function to set the angle made by the hours hand public void sethAngle(double h) { hAngle = h; } // Function to set the angle made by the minutes hand public void setmAngle(double m) { mAngle = m; } public void draw(Graphics g) // Function to draw the clock { int x,y; // temporary variables to calculate the // end points of the hours hand and minutes hand g.drawOval(xPos-50,yPos-50,100,100); // drawing the circle of the clock g.drawOval(xPos-3,yPos-3,6,6); // circle to show the center of the clock x = xPos + (int)Math.round(30 * Math.cos(hAngle)); // Finding the end points of the hours hand y = yPos + (int)Math.round(30 * Math.sin(hAngle)); g.drawLine(xPos,yPos,x,y); // Drawing the hours hand x = xPos - (int)Math.round(40 * Math.cos(mAngle)); // Finding the end points of the minutes hand y = yPos - (int)Math.round(40 * Math.sin(mAngle)); g.drawLine(xPos,yPos,x,y); // Drawing the minutes hand } } class Director // Director class { public static void main(String args[]) { int t,x1,y1,x2,y2; // (x1,y1) and (x2,y2) are the initial centers of // clocks t is time int xc1,xc2,yc1,yc2; // (xc1,yc1) and (xc2,yc2) are the new // centers of the clocks double ha1,ma1,ha2,ma2; // ha1,ma1 and ha2,ma2 are the angles made by the // hours and minutes hands of the two circles // respectively double sqrt5; // to store the value of square root of 5 MainWindow window = new MainWindow(); window.setVisible(true); Graphics g; // Graphics object Clock c1,c2; // Clock variables InputBox ib = new InputBox(window); t = ib.getInteger("Enter time:"); // Getting the time c1 = new Clock(); // initializing the clock variable by creating objects c2 = new Clock(); g = window.getGraphics(); // initializing the graphics variable x1 = 50; // initial positions of the centers y1 = 50; x2 = 50; y2 = 50; /* One clock is moving along (1,2) direction. It meams if the clock moves 1 unit along x direction then it will move 2 units along y direction. It's speed is 10. Therefore it's speed along x direction is 2 times sqrt5 and it's speed along y direction is 4 times sqrt5. Thus after a time 't' it moves a distance of '2*t*sqrt5' and '4*t*sqrt5' along x and y directions respectively. Similarly for the other clock, after a time 't' it moves a distance of '8*t*sqrt5' and '4*t*sqrt5' along x and y directions respectively. */ sqrt5 = Math.sqrt(5.0); xc1 = x1 + (int)Math.round(2*t*sqrt5); // Finding the new centers of the clocks yc1 = y1 + (int)Math.round(4*t*sqrt5); xc2 = x2 + (int)Math.round(8*t*sqrt5); yc2 = y2 + (int)Math.round(4*t*sqrt5); ha1 = Math.atan((0.0 - yc1) / (600.0 - xc1)); // Finding the angles made by the hour and minute hands ma1 = Math.atan((600.0 - yc1) / (0.0 - xc1)); // of the clocks ha2 = Math.atan((0.0 - yc2) / (600.0 - xc2)); ma2 = Math.atan((600.0 - yc2) / (0.0 - xc2)); c1.setx(xc1); // Calling the functions of the clock class to c1.sety(yc1); // set the centers and angles made by the hours c1.sethAngle(ha1); // and minutes hand with positive x-axis c1.setmAngle(ma1); c2.setx(xc2); c2.sety(yc2); c2.sethAngle(ha2); c2.setmAngle(ma2); g.drawRect(0,0,600,600); // drawing square c1.draw(g); // Calling the function of clock to draw c2.draw(g); // the clock } }