import java.awt.*; import java.awt.event.*; // Create a circle class that extends the base // drawing class. // // The move() method steps the object according to // kinematic equations for an object in a constant // gravitational field. // public class Circle extends ObjectToDraw{ int lastX, lastY,currentX, currentY; int drawXInit,drawYInit; int radius; double g; double time=0.0; double x,y; double VxInit,VyInit; double xRightBound,yBottomBound; double FrictionLoss; boolean xStop,yStop; /*---------------------------------------------------*/ Circle (int centerX, int centerY, int radius, int dX, int dY, double grav){ // super constructor must be called in first line. super(centerX,centerY,2*radius,2*radius,dX,dY); this.radius = radius; drawXInit = drawX = (areaX/2)-radius; drawYInit = drawY; FrictionLoss = 0.05; time=0.0; g = grav;// In pixels per sec**2 VxInit = Vx; VyInit = Vy; xRightBound = areaX; yBottomBound = areaY; } /*---------------------------------------------------*/ public void reset(){ drawX = drawXInit; drawY=drawYInit; lastX = drawX; lastY=drawY; currentX = drawX; currentY = drawY; // Use double value for physical position. x=(areaX/2)-radius; y=drawY; Vx = VxInit; Vy = VyInit; yStop = false; xStop = false; } /*---------------------------------------------------*/ // This method holds most of the physics. // The kinematic equations determine the movement of the // ball in the gravitational field. // // Step the objects in both X,Y. // Include horizontal velocity for optional studies on // its effects. // public synchronized void move(double deltaT){ double dt=0.0; double ytmp; // Horizontal movement has no forces except the // approximation of friction at the walls. x += deltaT*Vx; // Gravitational affects on Y movement y += deltaT*Vy + 0.5*g*deltaT*deltaT; Vy += g*deltaT; if(y >= yBottomBound || x >= xRightBound) { yStop = true; xStop = true; } drawY=(int)Math.round(y); drawX=(int)Math.round(x); } /*---------------------------------------------------*/ void clipToAffectedArea( Graphics g, int oldx, int oldy, int newx, int newy, int width, int height) { int x = Math.min( oldx, newx ); int y = Math.min( oldy, newy ); int w = ( Math.max( oldx, newx ) + width ) - x; int h = ( Math.max( oldy, newy ) + height ) - y; g.clipRect( x, y, w, h ); } /*---------------------------------------------------*/ // Override the base class draw method. public synchronized void draw(Graphics g) { lastX = currentX; lastY = currentY; currentX = drawX; currentY = drawY; clipToAffectedArea( g, lastX, lastY, currentX, currentY, dx, dy ); g.setColor(Color.white); g.fillRect(0,0,areaX,areaY); g.setColor(color); // Now draw a solid color circle. g.fillOval(drawX,drawY,dx,dy); lastX = drawX; lastY=drawY; } }