A game based on the dynamical spring system. Two masses connected by a spring, and each time they hit each other or they hit a wall, they lose a bit of energy. The objective is to collect all of the stars before the masses run out of energy. You set an initial velocity for one of the masses and physics does the rest.

read more

system.java

			
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Random;

@SuppressWarnings("serial")
public class system extends java.applet.Applet implements Runnable,
		ActionListener, MouseListener, MouseMotionListener {
	int frame;
	int delay;
	Image face;
	Image secondFace;
	Image star;
	MediaTracker tr;
	boolean invalidClick=false;
	int increment = 1;
	int numberOfPoints = 0;
	int startClickx;
	int startClicky;
	int currentx;
	int currenty;
	int xsize=800;
	int ysize=800;
	double moderator=.1;
	int xycoords[][] = new int[2][100000];
	double endpoints[][] = new double[2][100000];
	int xycoords2[][] = new int[2][100000];
	int angles[] = new int[10000];
	Thread animator;
	Rectangle test = new Rectangle();
	// int points[][]=new int [2][100];
	Random ran = new Random();
	Rectangle obsticle = new Rectangle(30,30,80,80);
	int score = 0;
	ArrayList points = new ArrayList();
	ArrayList secondPoints = new ArrayList();
	ArrayList fakePoints = new ArrayList();
	ArrayList toRemove = new ArrayList();
	ArrayList locations = new ArrayList();

	// Collection allThePoints = new ArrayList();
	// attempt blah = new attempt();
	/**
	 * Initialize the applet and compute the delay between frames.
	 */
	public void init() {
		String str = getParameter("fps");
		int fps = (str != null) ? Integer.parseInt(str) : 50;
		delay = (fps > 0) ? (1000 / fps) : 100;
		// Button nameField =new Button("Type here Something");
		addMouseListener(this);
		addMouseMotionListener(this);
		// nameField.addActionListener(blah);
		// add(nameField);
		tr = new MediaTracker(this);
		star = getImage(getCodeBase(), "star.gif");
		tr.addImage(star,0);
		face = getImage(getCodeBase(), "Ryan copy copy.png");
	    secondFace = getImage(getCodeBase(), "daniel.jpg");
		tr.addImage(face,0);
		tr.addImage(secondFace,0);
		
		for (int i = 0; i < 10; i++) {
			int blah = ran.nextInt(xsize);
			int blah2 = ran.nextInt(ysize);
			//int blah3 = ran.nextInt(1000);
			//int blah4 = ran.nextInt(1000);
			Rectangle blauhu = new Rectangle(blah, blah2, 20, 20);
			points.add(blauhu);
			
			//secondPoints.add(secondFace);			
			
			
			//Rectangle second = new Rectangle(blah3, blah4, 20, 20);
			//fakePoints.add(second);

		}

	}

	/**
	 * This method is called when the applet becomes visible on the screen.
	 * Create a thread and start it.
	 */
	public void start() {
		animator = new Thread(this);
		animator.start();

	}

	/**
	 * This method is called by the thread that was created in the start method.
	 * It does the main animation.
	 */
	public void run() {

		while (Thread.currentThread() == animator) {
			// Display the next frame of animation.
			increment++;
			repaint();

			// Delay for a while
			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				break;
			}

			// Advance the frame
			frame++;
		}
	}

	/**
	 * This method is called when the applet is no longer visible. Set the
	 * animator variable to null so that the thread will exit before displaying
	 * the next frame.
	 */
	public void stop() {
		animator = null;
	}

	/**
	 * Paint a frame of animation.
	 */
	SpringController objects = new SpringController();
	Rectangle massOneBounds = new Rectangle((int) objects.massOne.xPosition,
			(int) objects.massOne.yPosition, 20, 20);
	Rectangle massTwoBounds = new Rectangle((int) objects.massTwo.xPosition,
			(int) objects.massTwo.yPosition, 20, 20);

	boolean collision = false;
	boolean readyToStart = false;
	int constantincrementor = 0;

	public void paint(Graphics g) {

		if(!readyToStart){
			g.drawLine(startClickx, startClicky, currentx, currenty);
			
			g.drawString("Click the top mass and drag to give it an intial velocity", xsize/2, ysize/2);
			
			if(invalidClick){
				
				g.drawString("I'm sorry, but that was an invalid click", (xsize/2), (xsize/2)+20);
			}
			
			this.setSize(xsize, ysize);
		
		
		}
		
		
		 
		  
		  
		massOneBounds.x = (int) objects.massOne.xPosition;
		massOneBounds.y = (int) objects.massOne.yPosition;
		massTwoBounds.x = (int) objects.massTwo.xPosition;
		massTwoBounds.y = (int) objects.massTwo.yPosition;
		//g.drawImage(face, massOneBounds.x-20, massOneBounds.y-20, this);
		//g.drawImage(secondFace, massTwoBounds.x-20, massTwoBounds.y-20, this);
		if (massOneBounds.intersects(massTwoBounds)) {
			objects.massOne.collision = true;
			objects.massTwo.collision = true;
		}
		if(readyToStart){
		objects.spring.update();
		objects.massOne.update();
		objects.massTwo.update();
		}

		xycoords[0][numberOfPoints] = (int) objects.massOne.xPosition;
		xycoords[1][numberOfPoints] = (int) objects.massOne.yPosition;
		xycoords2[0][numberOfPoints] = (int) objects.massTwo.xPosition;
		xycoords2[1][numberOfPoints] = (int) objects.massTwo.yPosition;

		numberOfPoints++;

		//if (numberOfPoints > 1) {
			//for (int i = 0; i < numberOfPoints - 1; i++) {

				// g.drawLine(xycoords[0][i], xycoords[1][i], xycoords[0][i+1],
				// xycoords[1][i+1]);
				// g.drawLine(xycoords2[0][i], xycoords2[1][i],
				// xycoords2[0][i+1], xycoords2[1][i+1]);
			//}
		//}
		
		/**
		for (Rectangle a : points) {
			g.fillRect(a.x, a.y, a.height, a.width);
			if (massOneBounds.intersects(a) || massTwoBounds.intersects(a)) {
				//objects.massOne.collision = true;
				//objects.massTwo.collision = true;
				toRemove.add(a);
				score++;
			}
		}
		*/
		/**
		g.setColor(Color.red);
		g.fill3DRect(obsticle.x, obsticle.y, obsticle.width, obsticle.height, true);
		g.setColor(Color.black);
		
		if (massOneBounds.intersects(obsticle) ) {
			objects.massOne.collision = true;
			toRemove.add(obsticle);
			score++;
		}
		if(massTwoBounds.intersects(obsticle)){
			objects.massTwo.collision = true;
			toRemove.add(obsticle);
			score++;
		}
		*/
		
		for (Rectangle a : points) {
			//g.fillRect(a.x, a.y, a.height, a.width);
			g.drawImage(star, a.x-5, a.y-5, this);
			
			if(points.isEmpty()){
				g.drawString("You win!", xsize/2, ysize/2);
			}
			
			if (massOneBounds.intersects(a) ) {
				//objects.massOne.collision = true;
				toRemove.add(a);
				score++;
			}
			if(massTwoBounds.intersects(a)){
				//objects.massTwo.collision = true;
				toRemove.add(a);
				score++;
			}
			
			
		}
		
		
		//for (Rectangle a : fakePoints) {
			//g.fillRect(a.x, a.y, a.height, a.width);
			//if (massOneBounds.intersects(a) || massTwoBounds.intersects(a)) {
				//objects.massOne.collision = true;
				//objects.massTwo.collision = true;
				//toRemove.add(a);
				//score++;
			//}
		//}
		
		for (Rectangle a : toRemove) {
			points.remove(a);

		}
		g.drawString(Integer.toString(score), xsize-50, ysize-50);
		g.fillOval((int) objects.massOne.xPosition,
				(int) objects.massOne.yPosition, 20, 20);
		g.fillOval((int) objects.massTwo.xPosition,
				(int) objects.massTwo.yPosition, 20, 20);
	
		g.drawLine((int) objects.spring.startingX + 10,
				(int) objects.spring.startingY + 10,
				(int) objects.spring.endingX + 10,
				(int) objects.spring.endingY + 10);
		// g.drawLine((int)objects.massOne.xPosition +10,
		// (int)objects.massOne.yPosition +10,
		// (int)objects.massOne.xPosition+10+
		// (10)*(int)objects.massOne.xVelocity, (int)objects.massOne.yPosition
		// +10+ (10)*(int)objects.massOne.yVelocity);
	}
		

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		if(!massOneBounds.contains(e.getX(), e.getY())){
			System.out.println("Invalid");
		}
		
		startClickx = e.getX();
		startClicky = e.getY();
		currentx=startClickx;
		currenty=startClicky;
		
		}
	

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		if(massOneBounds.contains(startClickx, startClicky)){
			
		objects.massOne.xVelocity=moderator*(currentx-startClickx);
		objects.massOne.yVelocity=moderator*(currenty-startClicky);
		readyToStart = true;
		}else{
			invalidClick =true;
			currentx=startClickx;
			currenty=startClicky;
		}
		
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		currentx = e.getX();
		currenty = e.getY();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

	}

}


SpringController.java

			
public class SpringController {
	public Mass massOne;
	public Mass massTwo;
	public Spring spring;
	public SpringController(){
	this.massOne = new Mass(0,0,0,0, 2, 90, 60, 0,false);
	this.massTwo = new Mass(0,0,0,0, 2, 90, 200, 0,true);
	this.spring = new Spring(massOne, massTwo);
	}
}

Spring.java

			
public class Spring {
	private Mass massOne;
	private Mass massTwo;
	public double startingX = 0; 
	public double startingY = 0; 
	public double endingX = 0; 
	public double endingY = 0;
	public double tension = 0; 
	public double Xtension = 0; 
	public double Ytension = 0; 
	public double currentTime = 0;
	public double gravity = .25;
	public double springConstant = 30; 
	public double scaler = .001;
	public double angle = 0;

	public Spring(Mass massOne, Mass massTwo) {
		this.massOne = massOne;
		this.massTwo = massTwo;
		massOne.spring = this;
		massTwo.spring = this;
		this.startingX = massOne.xPosition; 
		this.startingY = massOne.yPosition; 
		this.endingX = massTwo.xPosition; 
		this.endingY = massTwo.yPosition;
		//this.Ytension = massTwo.mass*gravity;
	}

public void update(){
			
		
		this.startingX = massOne.xPosition; 
		this.startingY = massOne.yPosition; 
		this.endingX = massTwo.xPosition; 
		this.endingY = massTwo.yPosition;
		//this.tension = scaler*springConstant*Math.hypot((massOne.xPosition-massTwo.xPosition), (massOne.yPosition-massTwo.yPosition)); //in newtons/pixel
		this.Xtension = scaler*springConstant*(massOne.xPosition-massTwo.xPosition);
		this.Ytension = scaler*springConstant*(massOne.yPosition-massTwo.yPosition);
		//System.out.println(this.Ytension);
		
		
		
	
}

}

Mass.java

			
public class Mass {
	
	public double startingxAcceleration;
	public double startingyAcceleration;
	public double startingyVelocity;
	public double startingxVelocity;
	public double xAcceleration;
	public double yAcceleration;
	public double xVelocity;
	public double yVelocity;
	public double xPosition;
	public double yPosition;
	public double currentTime;
	public boolean underInfluence;
	public double mass;
	public Spring spring;
	public int yincrementer;
	public int xincrementer;
	public boolean collision;
	public double energyRetained = .8;
	public int xsize = 800;
	public int ysize = 800; 

	public Mass(double startingxAcceleration, double startingyAcceleration,double startingxVelocity, double startingyVelocity,double mass,
			double xPosition, double yPosition, double currentTime,boolean underInfluence) {
		
		this.startingxAcceleration = startingxAcceleration;
		this.startingyAcceleration = startingyAcceleration;
		this.startingyVelocity = startingyVelocity;
		this.startingxVelocity = startingxVelocity;
		this.xPosition = xPosition;
		this.yPosition = yPosition;
		this.currentTime = currentTime;
		this.underInfluence = underInfluence;
		this.mass = mass;
		this.yVelocity = startingyVelocity;
		this.xVelocity = startingxVelocity;
	}	
	
public void update(){
			
	if(this.underInfluence){
		this.xAcceleration=(((spring.Xtension)+this.startingxAcceleration))/this.mass;
		this.yAcceleration=(((spring.Ytension)+this.startingyAcceleration))/this.mass;
	}
	if(!this.underInfluence){
		this.xAcceleration=((-spring.Xtension)+this.startingxAcceleration)/this.mass;
		this.yAcceleration=((-spring.Ytension)+this.startingyAcceleration)/this.mass;
	}
	
	//if(!this.underInfluence&&this.xVelocity>=10){
		//this.xAcceleration=0;
		//this.xVelocity=1;
		
	//}
	
	
	yincrementer--;
	xincrementer--;
	
	if((this.yPosition>=ysize||this.yPosition<=0)&&yincrementer<=0){
		this.yVelocity=(-1)*this.yVelocity;
	
			this.yVelocity=this.energyRetained*this.yVelocity;
			
			
		yincrementer=5;
		
	}
	
	if((this.xPosition>=xsize||this.xPosition<=0)&&xincrementer<=0){
		this.xVelocity=(-1)*this.xVelocity;
		this.xVelocity=this.energyRetained*this.xVelocity;
		xincrementer=5;
		
	}
	if((this.collision)){
		this.xVelocity=(-1)*this.xVelocity;
		this.yVelocity=(-1)*this.yVelocity;
		this.collision = false;
		this.yVelocity=this.energyRetained*this.yVelocity;
		this.xVelocity=this.energyRetained*this.xVelocity;
	}
	
	
	this.xVelocity += this.xAcceleration;
	this.yVelocity += this.yAcceleration;
	
	
	this.xPosition=this.xPosition+(this.xVelocity);
	this.yPosition=this.yPosition+(this.yVelocity);
	
		
	
}
	
	
}

star.gif