import java.awt.*; import java.applet.*; import java.lang.*; /* Pool3 is an Applet that uses form data in a HTML Document (using Javascript), to draw three balls on a Carom Billiard Table, showing the path the cue ball will take if hit directly toward one of the cushions. inputs include the location and color of the three balls, which cushion is being struck, the force that the cue ball is being struck, and the location along cushion that the cue ball is being aimed. This is designed to help students discover parallel lines and complementary angles. version 1.0 October 1998 -- Fr. Chris Thiel */ public class pool3 extends Applet { // globals String Title; int x[]; int y[]; int bx=0; int by=0; Object color[]; int n; //number of balls int yMax=200; int force=40; // how many traces drawn float deltaX=0; float deltaY=0; double x0=0; double y0=0; // the code... public void init() { int bgcolor = Integer.parseInt(getParameter("bgcolor")); setBackground(new Color(bgcolor)); String s; Title = new String("Press Redraw to Update"); s = getParameter("n"); if ( s == null) { n=0; } else { n=Integer.parseInt(s); } // set up the array of balls x = new int[n]; y = new int[n]; color = new Color[n]; //find out the vertical limit to adjust for co-ords s = getParameter("ymax"); if ( s == null) { yMax=200; //default change this to change the shape of } else { yMax=Integer.parseInt(s); } repaint(); } public void paint( Graphics g ) { // String ang = angle.toString(); // g.drawString(ang, 30, 45); g.drawString(Title, 30, 30 ); g.setColor(Color.white); g.fillOval(x[0]-5, y[0]-5, 11, 11); g.setColor((Color)color[0]); g.fillOval(x[1]-5, y[1]-5, 11, 11); g.setColor((Color)color[1]); g.fillOval(x[2]-5, y[2]-5, 11, 11); g.setColor(Color.yellow); for (int j=50; j< 400; j = j+50){ g.drawLine(j,200,j,195); g.drawLine(j,0,j,5); } for (int j=50; j< 200; j = j+50){ g.drawLine(0,j,5,j); g.drawLine(395,j,400,j); } g.setColor(Color.green); //bounces g.drawLine(x[0],y[0],bx,by); // g.drawLine(bx[0],by[0],bx[1],by[1]); x0 =bx; y0 = by; deltaX = bx-x[0]; deltaY = by-y[0]; for (int i=1; i=400)) deltaX = -1 * deltaX; //change direction if ((y0<=0)||(y0>=200)) deltaY = -1 * deltaY; x0=x0+(.03*deltaX); y0=y0+(.03*deltaY); g.fillOval((int)(x0),(int)(y0),3,3); } //g.fillOval(x[i]-1, y[i]-1, 3, 3); // g.fillOval(x[i+1]-1, y[i+1]-1, 3, 3); } public void setString(String aString) { Title = aString; repaint(); } public void setShot( String Direction, float Diamond, float f) { //set initial bounce site if (Direction.equals("top")) { by = 0; bx = (int)(50*Diamond); } if (Direction.equals("bottom")) { by = 200; bx = (int)(50*Diamond); } if (Direction.equals("left")) { bx = 0; by = 200-(int)(50*Diamond); } if (Direction.equals("right")) { bx = 400; by = 200-(int)(50*Diamond); } force=40; if ((f >0)&&(f < 1000)) force=(int)(f); } public void setPoint( int i, float xpt, float ypt, String colour) { x[i-1] = (int)(50*xpt); y[i-1] = 200-(int)(50*ypt); if (colour != null) { if (colour.equals("red")) { color[i-1] = Color.red; } else if (colour.equals("green")) { color[i-1] = Color.green; } else if (colour.equals("blue")) { color[i-1] = Color.blue; } else if (colour.equals("pink")) { color[i-1] = Color.pink; } else if (colour.equals("orange")) { color[i-1] = Color.orange; } else if (colour.equals("magenta")) { color[i-1] = Color.magenta; } else if (colour.equals("cyan")) { color[i-1] = Color.cyan; } else if (colour.equals("white")) { color[i-1] = Color.white; } else if (colour.equals("yellow")) { color[i-1] = Color.yellow; } else if (colour.equals("gray")) { color[i-1] = Color.gray; } else if (colour.equals("darkGray")) { color[i-1] = Color.darkGray; } else { color[i-1] = Color.black; } } else { color[i-1] = Color.black; } repaint(); } }