// Body object // Needed for Nbody.class // M.Dubson ENVD 3252 final project import java.awt.*; public final class Body { double xPos, yPos, vX, vY, aX, aY; double xInit, yInit, vXInit, vYInit; double vXInitCM, vYInitCM; //initial velocity in C.M. frame double xLast, yLast; double xLastPaint, yLastPaint; double aXLast, aYLast, aXBeforeLast, aYBeforeLast; double mass; static double velFactor = 0.01; int radius; Color color; static Color colorArray[]= {Color.red, Color.orange, Color.green, Color.blue,Color.yellow, Color.white, Color.pink}; int w = 400; int h = 300; double xMovie[] = new double [10000]; double yMovie[] = new double [10000]; double vXMovie[] = new double [10000]; double vYMovie[] = new double [10000]; double pX = mass*vX; double pY = mass*vY; public Body (int i) { if (i == 0) { xPos = 0.3*w; yPos = 0.5*h; vX = 0.0; vY = 0.0; mass = 400; } if (i == 1) { xPos = 0.8*w; yPos = 0.5*h; vX = 0.0; vY = -0.05; mass = 2; } if (i == 2) { xPos = 0.9*w; yPos = 0.3*h; vX = 1.0; vY = 0.0; mass = 0; } //mass = (double) Math.round(Math.random()*100); //30.0; radius = 1 + (int) (1.5*Math.pow(mass, 0.333)); //xPos = Math.random()*w*0.8; yPos = Math.random()*h*0.6 + h*0.2; xLast = xPos; yLast = yPos; xLastPaint = xPos; yLastPaint = yPos; //vX = 0.0; vY = 0.0; xInit = xPos; yInit = yPos; vXInit = vX; vYInit = vY; color = colorArray[i]; //System.out.println("Red is "+ rV + ". Green is "+gV+". Blue is "+bV+"."); } public Body ( double massBody, int r, double x, double y, double velX, double velY) { mass = massBody; radius = r; xPos = x; yPos = y; vX = velX; vY = velY; } public final void paint(Graphics g) { g.setColor(color); g.fillOval((int)xPos - radius, (int)yPos - radius, 2*radius, 2*radius); g.drawLine((int) xPos, (int) yPos, (int) (xPos + vX/velFactor) , (int) (yPos + vY/velFactor)); //g.setColor(Color.black); //g.drawOval((int)xPos - radius, (int)yPos - radius, 2*radius, 2*radius); } public final void paintTrajectory(Graphics g) { g.setColor(color); g.drawLine((int) xLastPaint, (int) yLastPaint, (int)xPos , (int)yPos ); } public final void posPaintIn(Graphics g, int xNow, int yNow) { //int vXs = (int) (vX/velFactor); int vYs = (int) (vY/velFactor); g.setColor(color); g.fillOval(xNow - radius, yNow - radius, 2*radius, 2*radius); //g.drawLine(xNow, yNow, xNow + vXs , yNow + vYs); //g.setColor(Color.black); //g.drawOval(xNow - radius, yNow - radius, 2*radius, 2*radius); } public final void velPaintIn(Graphics g, int xNow, int yNow) { g.setColor(color); g.fillOval((int) xPos - radius, (int) yPos - radius, 2*radius, 2*radius); g.drawLine((int)xPos, (int) yPos, xNow , yNow); //g.setColor(Color.black); //g.drawOval((int)xPos - radius, (int)yPos - radius, 2*radius, 2*radius); } public final void paintOut(Graphics g) { int vXs = (int) (vX/velFactor); int vYs = (int) (vY/velFactor); g.setColor(Color.black); g.fillOval((int) xPos - radius, (int) yPos - radius, 2*radius, 2*radius); //g.drawOval((int)xPos - radius, (int)yPos - radius, 2*radius, 2*radius); g.drawLine((int)xPos , (int)yPos , (int)xPos + vXs, (int)yPos + vYs); } public final void setR(int xNow, int yNow) { xPos = (double) xNow; yPos = (double) yNow; xInit = (double) xNow; yInit = (double) yNow; xLastPaint = (double) xNow; yLastPaint = (double) yNow; } public final void setV(int xNow, int yNow) { vX = velFactor* ((double) xNow - xPos); vY = velFactor* ((double) yNow - yPos); } public boolean bodClicked (int x, int y) { if ( x >= (int) xPos - radius && x <= (int) xPos + radius && y >= (int) yPos - radius && y <= (int) yPos + radius ) { return true; } else return false; }//end of bodClicked method public boolean velClicked (int x, int y) { int vXs = (int) (vX/velFactor); int vYs = (int) (vY/velFactor); if ( x >= (int) xPos + vXs - radius && x <= (int) xPos + vXs + radius && y >= (int) yPos + vYs - radius && y <= (int) yPos + vYs + radius ) { return true; } else return false; }//end of velClicked method }//end of class Body