import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class cue extends Applet implements MouseListener{ int cuex, cuey; // where cue is int oldx, oldy; // hold to paint over old location int targetx, targety; // where cue is headed int x, y, startx, starty; // change in x and y, original location int x1, y1; // one ball int oldx1, oldy1; int targetx1, targety1; int dx1, dy1, startx1, starty1; boolean hit; private Image pool; public void init() { pool = getImage( getDocumentBase(), "logo1.gif" ); cuex = 180; cuey = 195; x1 = 420; y1 = 195; addMouseListener( this ); } public void paint( Graphics g ) { g.fillRect( 0, 0, 1000, 1000 ); g.drawImage( pool, 50, 350, this ); g.setColor( new Color( 0, 140, 0 ) ); // trim g.fillRect( 90, 90, 420, 220 ); g.setColor( Color.green ); // felt g.fillRect( 100, 100, 400, 200 ); g.setColor( Color.black ); // pockets g.fillOval( 95, 95, 15, 15 ); // TL g.fillOval( 95, 290, 15, 15 ); // BL g.fillOval( 490, 95, 15, 15 ); // TR g.fillOval( 490, 290, 15, 15 ); // BR g.fillOval( 290, 95, 15, 15 ); // TS g.fillOval( 290, 290, 15, 15 ); // BS g.fillOval( 198, 198, 4, 4 ); // markers g.fillOval( 398, 198, 4, 4 ); g.setColor( Color.white ); g.fillOval( 199, 199, 2, 2 ); g.fillOval( 399, 199, 2, 2 ); g.fillOval( cuex, cuey, 10, 10 ); // initial cue location g.setColor( Color.yellow ); g.fillOval( x1, y1, 10, 10 ); // initial one location } public void update( Graphics g ) { startx = cuex; starty = cuey; y = targety - cuey; x = targetx - cuex; for( int i = 1; i <= 100; i++ ) { oldx = cuex; oldy = cuey; oldx1 = x1; oldy1 = y1; cuex = startx + (int) Math.ceil( x * i / 100 ); if( cuex < 100 ) cuex = 100; if( cuex > 489 ) cuex = 489; cuey = starty + (int) Math.ceil( y * i / 100 ); if( cuey < 100 ) cuey = 100; if( cuey > 289 ) cuey = 289; if( hit ){ x1 = startx1 + (int) Math.ceil( dx1 * i / 100 ); if( x1 < 100 ) x1 = 100; if( x1 > 489 ) x1 = 489; y1 = starty1 + (int) Math.ceil( dy1 * i / 100 ); if( y1 < 100 ) y1 = 100; if( y1 > 289 ) y1 = 289; } g.setColor( Color.green ); g.fillOval( oldx, oldy, 10, 10 ); if( hit ) g.fillOval( oldx1, oldy1, 10, 10 ); g.setColor( Color.black ); // redraw markers in case they get g.fillOval( 198, 198, 4, 4 ); // covered g.fillOval( 398, 198, 4, 4 ); g.setColor( Color.white ); g.fillOval( 199, 199, 2, 2 ); g.fillOval( 399, 199, 2, 2 ); // draw position of cue g.fillOval( cuex, cuey, 10, 10 ); // draw position of one if( hit ) { g.setColor( Color.yellow ); g.fillOval( x1, y1, 10, 10 ); } try { Thread.sleep( 10 ); } catch ( InterruptedException exception ) { System.err.println( exception.toString() ); } if( cuey == 100 && targety != 100 ) // to cue rebound off top bumper { targety = 200 - targety; // send cue in new direction starty = 90; y = targety - cuey; } if( y1 == 100 && targety1 != 100 && hit ) // to one rebound off top bumper { targety1 = 200 - targety1; // send one in new direction starty1 = 90; dy1 = targety1 - y1; } if( cuey == 289 && targety != 289 ) // to rebound cue off bottom bumper { targety = 600 - targety; starty = 279; y = targety - cuey; } if( y1 == 289 && targety1 != 289 && hit ) // to rebound one off bottom bumper { targety1 = 600 - targety1; starty1 = 279; dy1 = targety1 - y1; } if( cuex == 100 && targetx !=100 ) // rebound cue off left { targetx = 200 - targetx; startx = 90; x = targetx - cuex; } if( x1 == 100 && targetx1 !=100 && hit ) // rebound one off left { targetx1 = 200 - targetx1; startx1 = 90; dx1 = targetx1 - x1; } if( cuex == 489 && targetx != 489 ) // rebound cue off right { targetx = 1000 - targetx; startx = 479; x = targetx - cuex; } if( x1 == 489 && targetx1 != 489 && hit ) // rebound one off right { targetx1 = 1000 - targetx1; startx1 = 479; dx1 = targetx1 - x1; } g.setColor( Color.black ); // pockets g.fillOval( 95, 95, 15, 15 ); // TL g.fillOval( 95, 290, 15, 15 ); // BL g.fillOval( 490, 95, 15, 15 ); // TR g.fillOval( 490, 290, 15, 15 ); // BR g.fillOval( 290, 95, 15, 15 ); // TS g.fillOval( 290, 290, 15, 15 ); // BS if( x1-10 < cuex && cuex < x1+10 && y1-10 < cuey && cuey < y1+10 && hit == false ) { dx1 = 9*(x1 - cuex); dy1 = 9*(y1 - cuey); x = cuex - x1; y = cuey - y1; startx1 = x1; starty1 = y1; startx = cuex; starty = cuey; targetx1 = dx1*(targetx - cuex); targety1 = dy1*(targety - cuey); targetx = x*(targetx - cuex); targety = y*(targety - cuey); hit = true; } } } public void mouseClicked( MouseEvent e ) { hit = false; targetx = e.getX() - 5; targety = e.getY() - 5; repaint(); } public void mousePressed( MouseEvent e ) { } public void mouseReleased( MouseEvent e ) { } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } }