/* PROBLEM: Write a program with 2 classes ? Planet and Director. The aim is to draw a planetary system with 4 planets. At time zero, the planets would be aligned along the positive x-axis. Time would be taken as input from the user. Then, the position of the planets would be calculated based on the angular velocities of the planets and the planets would be displayed. */ import javabook.*; import java.awt.*; import java.lang.*; class Planet { float Omega;//Angular velocity double Alpha;//Angle of axis with +ve x axis double xPosition,yPosition;//co-ordinates of center of planet void setX(int x) { xPosition=x; }; void setY(int y) { yPosition=y; }; void setAlpha(double x) { Alpha=x; }; void setOmega(float y) { Omega=y; }; void drawPlanet(Graphics g) { int x1,y1,x2,y2,xx,yy; g.setColor(Color.black); /*axis line coordinates*/ x1=(int)(xPosition+15*Math.cos(Alpha)); y1=(int)(yPosition+15*Math.sin(Alpha)); x2=(int)(xPosition-15*Math.cos(Alpha)); y2=(int)(yPosition-15*Math.sin(Alpha)); g.drawLine(x1,y1,x2,y2); g.fillOval((int)xPosition-10,(int)yPosition-10,2*10,2*10); } } class Director { public static void main(String[] args) { int t,xPosition,yPosition,radius=10,x,Omega,Alpha; double th,slope,th1; MainWindow mainwindow=new MainWindow(); mainwindow.setVisible(true); InputBox input=new InputBox(mainwindow); t=input.getInteger("time"); Graphics g; g=mainwindow.getGraphics(); //drawing grid and orbits of each planet g.setColor(Color.yellow); g.drawLine(500,0,500,1000); g.drawLine(0,350,1000,350); g.drawOval(500-100,350-100,200,200); g.drawOval(500-150,350-150,300,300); g.drawOval(500-200,350-200,400,400); g.drawOval(500-250,350-250,500,500); Planet A,B,C,D; A= new Planet(); A.setOmega(40); Omega=40; x=100; th=(float)Omega*t*(float)3.14/180; th1=(float)(Omega-10)*t*(float)3.14/180; //center of planet xPosition=500+((int)x*Math.cos(th)); yPosition=350-((int)x*Math.sin(th)); A.setX(xPosition); A.setY(yPosition); //finding out Alpha. slope=(((yPosition-(350-(int)(x+50)*Math.sin(th1)))/(xPosition-(500+(int)(x+50)*Math.cos(th1))))); th1=Math.atan(slope); A.setAlpha(th1); A.drawPlanet(g); B= new Planet(); B.setOmega(30); Omega=30; x=150; th=(float)Omega*t*(float)3.14/180; th1=(float)(Omega-10)*t*(float)3.14/180; xPosition=500+((int)x*Math.cos(th)); yPosition=350-((int)x*Math.sin(th)); B.setX(xPosition); B.setY(yPosition); slope=(((yPosition-(350-(int)(x+50)*Math.sin(th1)))/(xPosition-(500+(int)(x+50)*Math.cos(th1))))); th1=Math.atan(slope); B.setAlpha(th1); B.drawPlanet(g); C= new Planet(); C.setOmega(20); Omega=20; x=200; th=(float)Omega*t*(float)3.14/180; th1=(float)(Omega-10)*t*(float)3.14/180; xPosition=500+((int)x*Math.cos(th)); yPosition=350-((int)x*Math.sin(th)); C.setX(xPosition); C.setY(yPosition); slope=(((yPosition-(350-(int)(x+50)*Math.sin(th1)))/(xPosition-(500+(int)(x+50)*Math.cos(th1))))); th1=Math.atan(slope); C.setAlpha(th1); C.drawPlanet(g); D= new Planet(); D.setOmega(10); Omega=10; x=250; th=(float)Omega*t*(float)3.14/180; th1=(float)(Omega-10)*t*(float)3.14/180; xPosition=500+(int)((int)x*Math.cos(th)/*-radius*/); yPosition=350-(int)((int)x*Math.sin(th)/*-radius*/); D.setX(xPosition); D.setY(yPosition); slope=(((yPosition-(350-(int)(x+50)*Math.sin(th1)/*-radius*/))/(xPosition-(500+(int)(x+50)*Math.cos(th1)/*-radius*/)))); th1=Math.atan(slope); D.setAlpha(th1); D.drawPlanet(g); } }