package shotcaller;import java.awt.*; import java.applet.*;import java.util.*;import javax.swing.*;import java.math.*;import java.awt.event.*;import java.awt.event.MouseMotionListener;import javax.swing.event.MouseInputListener;public class shot extends Applet { //main class for game named shot, extends applet class int x1; //stores x position of the mouse int y1; //stores y position of the mouse double power; // stores the power of the shot double deltax; // stores the distance between the mouse and the white ball on the X plane double deltay; // stores the distance between the mouse and the white ball on the Y plane double x2; //stores the power after being translated into X coordinates; double y2; //stores the power after being translated into Y coordinates; int r; // stores x position of the ball int h; //stores y position of the ball double angle; // stores the angle of the shot double dist1; //stores the real distance of the shot boolean canshoot = true; //used to test if it is legal to shoot int i; //used in the i loop int j; //used in the j loop int count=0; // stores amout of balls sunk int shots =0; //stores amount of shots int bmax = 8; // sets maximum number of balls private final int WIDTH =550; //used to set size of window private final int HEIGHT = 300; //used to set size of window java.util.Timer timer; //declacres timer as a Timer object Graphics doublebuffer; //declares doublebuffer as a Graphics object Image offscreen; //declares offscreen as an image object Object hole; //declares hole ans a object hole[] H = new hole[6]; //creates a object array of the object hole Object ball; // declares ball as a object ball[] b = new ball [8]; //creates a object array of the object ball public void shoot(){ //creates shoot() method if(canshoot){ //checks the canshoot boolean deltax = (r -x1) ; // gets distance between ball's and mouse's x positions deltay = (h - y1) ; // gets distance between ball's and mouse's y positions angle = Math.atan2(deltax,deltay)/(Math.PI/180); //converts degrees into an angle dist1 = Math.sqrt(deltax+deltay); //calculates squared distance between the ball and mouse x2 = (power*Math.sin(angle*(Math.PI/180))); //converts angle back into x coordinates and times it by power y2 = (power*Math.cos(angle*(Math.PI/180))); //converts angle back into y coordinates and times it by power x2*=-1; //inverts x2; y2*=-1; //inverts y2; b[0].vx = b[0].vx + x2; //adds power onto the balls X speed b[0].vy = b[0].vy + y2; // adds power onto the balls y speed power=0; //rests power shots+=1; //adds 1 to the shot counter canshoot = false; // makes shooting after this function illegal } } public class motion implements MouseInputListener { public void add(){ addMouseMotionListener(this); addMouseListener(this); } public void mouseReleased(MouseEvent mouseEvent) { shoot(); } public void mousePressed(MouseEvent mouseEvent) { //if mouse pressed if(b[0].vx==0){ // if ball is not moving on the x plane if(b[0].vy==0){ //if ball is not moving on the y plane canshoot=true; //makes it legal for ball to shoot power=0; //resets power } } } public void mouseMoved(MouseEvent mouseEvent) { r = (int) b[0].x +12; // fetches x position of the ball and puts it into r variable h = (int) b[0].y +12 ;// fetches y position of the ball and puts it into h variable x1 = mouseEvent.getX(); // fetches x position of the mouse and puts it into x1 variable y1 = mouseEvent.getY(); // fetches y position of the mouse and puts it into y1 variable } public void mouseClicked(MouseEvent mouseEvent) {} //empty functions need to be declared for it to be possible to implement Mouseinputlistner public void mouseEntered(MouseEvent mouseEvent) {} public void mouseExited(MouseEvent mouseEvent) {} public void mouseDragged(MouseEvent mouseEvent) {} } public void create(){ //method that creats the ball and hole objects for(i = 0; i<6; i++){ // i for loop H[i]= new hole(); //creates an instance of the hole class and puts it into the H array } for (i= 0; i < bmax; i++){ // i for loop b[i] = new ball(); //creates an instance of the ball class and puts it into the b array } //set postions of the holes H[0].x=0; //top left H[0].y=0; H[1].x=520; //topright H[1].y=0; H[2].x=520; //bottom right H[2].y=270; H[3].x=260; //top middle H[3].y=0; H[4].x=260;//bottom middle H[4].y=270; H[5].x=0; //bottom left H[5].y=270; //sets positions of the balls b[0].x=70; b[1].x=387; b[2].x=414; b[3].x=444; b[4].x=444; b[5].x=444; b[6].x=414; b[7].x=-100; b[0].y=132; b[1].y=132; b[2].y=117; b[3].y=103; b[4].y=134; b[5].y=165; b[6].y=150; } public void init(){ //init method, used to start program create(); //runs creat method to create the balls motion m = new motion(); //creates an instance of the motion class m.add(); //runs method used to create the balls this.setSize(WIDTH,HEIGHT); // sets the size of the applet window timer = new java.util.Timer(); //creates an instance of the Timer class timer.schedule(new gameTask(),0,1*10); // creates a thread using TimerTask offscreen = createImage(640,480); //creats an image in the offscreen object doublebuffer = offscreen.getGraphics(); //defines double buffer } class gameTask extends TimerTask { public void run(){ power+=.5; //adds .5 to power if(power>40){ //sets dosent let power exceed 40 power=40; } for (i = 0; i<7; i++){ // i loop used to interact with all balls using one method if((b[i].x>0&&b[i].x<15&&b[i].y>0&&b[i].y<15) //big IF statment, used to check if ball has it any of the holes ||(b[i].x>520&&b[i].x<535&&b[i].y>0&&b[i].y<15) ||(b[i].x>520&&b[i].x<535&&b[i].y>270&&b[i].y<285) ||(b[i].x>260&&b[i].x<290&&b[i].y>0&&b[i].y<30) ||(b[i].x>260&&b[i].x<290&&b[i].y>270&&b[i].y<300) ||(b[i].x>0&&b[i].x<15&&b[i].y>270&&b[i].y<285)){ if(i>0){ //tests if ball is not the white ball b[i].dead= true ; H[0].sunk=1; } else { b[i].x=70; b[i].y=132; b[i].vx=0; b[i].vy=0; } } if(b[i].dead){ //tests if ball is "dead" b[i].dead(); //runs dead method used to get rid of ball } if(b[i].vx<0.1 && b[i].vx>-.1){ // used to stop balls xspeed b[i].vx=0; } if(b[i].vy<0.1 && b[i].vy>-.1){ //used to stop balls yspeed b[i].vy=0; } //these tests the ball against the boundries and get it to bounce back if(b[i].x>520){b[i].vx*=-.9;b[i].x-=1;} if(b[i].y>270){b[i].vy*=-.9;b[i].y-=1;} if(b[i].y<10){b[i].vy*=-.9;b[i].y+=1;} if(b[i].x<10){b[i].vx*=-.9;b[i].x+=1;} //applies physics to ball. i.e xspeed,yspeed, and decelleration b[i].vx*=b[i].damp; b[i].vy*=b[i].damp; b[i].x +=b[i].vx; b[i].y +=b[i].vy; for (j=1; j<7; j++){ //loop to test balls against other balls double dx = b[j].x-b[i].x; //gets x distance double dy = b[j].y-b[i].y; //gets y distance double dist = Math.sqrt(dx*dx+dy*dy); //finds real distance if(dist <b[i].rad+b[j].rad){ //checks collision //!@#!@$%!@$!@$!@$----BLACK BOX----@!@#!@#!@#!@// //methods understood but not explainable in plain english, interperate directly double angle = Math.atan2(dy,dx); double cosa = Math.cos(angle); double sina = Math.sin(angle); double vx1p = cosa*b[i].vx+sina*b[i].vy; double vy1p = cosa*b[i].vy-sina*b[i].vx; double vx2p = cosa*b[j].vx+sina*b[j].vy; double vy2p = cosa*b[j].vy-sina*b[j].vx; double P = vx1p*b[i].m+vx2p*b[j].m; double V = vx1p-vx2p; vx1p = (P-b[j].m*V)/(b[i].m+b[j].m); vx2p = V+vx1p; b[i].vx = cosa*vx1p-sina*vy1p; b[i].vy = cosa*vy1p+sina*vx1p; b[j].vx =cosa*vx2p-sina*vy2p; b[j].vy = cosa*vy2p+sina*vx2p; double diff =((b[i].rad+b[j].rad)-dist)/2; double cosd=cosa*diff; double sind = sina*diff; b[i].x-=cosd; b[i].y -= sind; b[j].x += cosd; b[j].y += sind; //end of black box! } } } repaint(); //used to call paint method } } public void update (Graphics g){ //middle method used to update screen, called in repaint method paint (g); //calls paint method directly. } public void paint(Graphics g){ //creates paint method, this is were all graphical objects are put to the screen doublebuffer.clearRect(0,0,550,300); //clears screen; doublebuffer.setColor(Color.black); //sets colour to black doublebuffer.fillRect(0,0,550,300); //makes a black rectangle (used as background) doublebuffer.setColor(Color.green); //sets color to green doublebuffer.fillRoundRect(10,10,530,280,30,30); //draws a green round rectangle (makes pool table) doublebuffer.setColor(Color.darkGray); //sets color to gray for(i = 0; i<6; i++){ //loop used to draw holes doublebuffer.fillOval((int)H[i].x,(int)H[i].y,30,30); //draws holes in there positions count += H[i].sunk; // checks if any balls have sunk in hole and adds to count H[i].sunk=0; //resets the sunk counter } for (i=0; i<8; i++){ //i loop used to draw balls //sets colours for balls if(i==0){doublebuffer.setColor(Color.white);} if(i==1){doublebuffer.setColor(Color.black);} if(i==2){doublebuffer.setColor(Color.red);} if(i==3){doublebuffer.setColor(Color.blue);} if(i==4){doublebuffer.setColor(Color.magenta);} if(i==5){doublebuffer.setColor(Color.PINK);} if(i==6){doublebuffer.setColor(Color.yellow);} if(i==7){ doublebuffer.setColor(Color.black);} if(i==8){doublebuffer.setColor(Color.lightGray);} doublebuffer.fillOval((int)b[i].x,(int)b[i].y,24,24); //draws balls } doublebuffer.setColor(Color.black); //sets color to black doublebuffer.drawString("Strength = "+(int)power,40,280); //draws text to tell you how much power is going to be applied to ball when shot doublebuffer.drawString("Shots = "+(int)shots,40,240); //draws text to tell you how many shots youve taken doublebuffer.drawString("Balls Sunk = "+(int)count,+40,260); // draws how many balls youve sunk doublebuffer.setColor(Color.blue); //sets colour to blue doublebuffer.fillRect(80,290,(int)power*10,20); //draws bar to visually represent power doublebuffer.drawLine( r,h,(int)x1, (int)y1 ); //draws line from ball to mouse to help with aiming g.drawImage(offscreen,0,0,this); //draws all of the ubuve into the image, that then will be drawn on the screen }}//////////////////////////////////////////////////////////ball class////package shotcaller; public class ball { double grav =.6; double damp =.98; int rad = 12; double x; double y; double vx =0; double vy =0; boolean dead = false; int m = 3; public void dead(){grav =0; damp =.0; rad = 0; x =10000000; y =1000000000; vx =10000000; vy =1000000; m = 3; } }/////////////////hole class///////////////////package shotcaller; public class hole{ int rad =15; int x =0; int y = 0; int sunk =0; }