Download this source file


// Display some bouncing hoops.  Also, add code to
// close the application when the user clicks the
// mouse in the close box.  This, slightly more complex
// version eliminates the "flicker" of the text.

// Written by Wayne Pollock, Tampa, FL USA, 1999

import java.awt.*;
import java.awt.event.*;

public class Hoops2 extends Frame
{
   int dir = 1;  // "1" for forward, "-1" for backward.
   int hoop1_pos = 10;
   int hoop2_pos = 290 - 40;  // inner frame width - hoop diameter

   public Hoops2 ()
   {
      setTitle( "Bouncing Hoops" );
      setSize( 300, 200 );
      setBackground( Color.cyan );
      setLayout( null );
      Label myLabel = new Label( "Bouncing Hoops!" );
      myLabel.setFont( new Font("SansSerif", Font.BOLD, 24) );
      myLabel.setBounds( new Rectangle(50, 160, 200, 30) );
      add( myLabel );
      setVisible( true );
   }

   public static void main ( String [] args )
   {
      Frame hoopsFrame = new Hoops2();

      // Window closing code:
      hoopsFrame.addWindowListener(
         new WindowAdapter()
         {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );
      for ( ;; )
      {
         try
         {
            Thread.sleep( 30 );
         } catch ( InterruptedException ie ) {}
         hoopsFrame.repaint();
      }
   }

   public void update( Graphics g )
   {
      paint( g );
   }

   public void paint ( Graphics g )
   {
      // Erase previous hoops:
      g.setColor( Color.cyan );
      g.drawOval( hoop1_pos, 60, 40, 40 );
      g.drawOval( hoop2_pos, 60, 40, 40 );

      // Draw the hoops in their new positions:
      if ( hoop1_pos >= (150-40-1) || hoop1_pos < 10)
         dir = -dir;
      hoop1_pos += dir * 3;
      hoop2_pos -= dir * 3;
      g.setColor( Color.red );
      g.drawOval( hoop1_pos, 60, 40, 40 );
      g.setColor( Color.blue );
      g.drawOval( hoop2_pos, 60, 40, 40 );
   }
}