Download this source file
// Display some bouncing hoops. Also added code to close the
// application when the user clicks the mouse in the close box.
// Written by Wayne Pollock, Tampa, FL USA, 1999
import java.awt.*;
import java.awt.event.*;
public class Hoops 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 Hoops ()
{
setTitle( "Bouncing Hoops" );
setSize( 300, 200 );
setBackground( Color.cyan );
setVisible( true );
}
public static void main ( String [] args )
{
Frame hoopsFrame = new Hoops();
// Window closing code:
hoopsFrame.addWindowListener(
new WindowAdapter()
{ public void windowClosing( WindowEvent e )
{ System.exit( 0 );
}
}
);
for ( int i=0; i<5000; ++i )
{
hoopsFrame.repaint();
try
{ Thread.sleep( 30 );
} catch ( InterruptedException ie ) {}
}
System.exit( 0 );
}
public void paint ( Graphics g )
{
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 );
g.setColor( Color.black );
g.setFont( new Font("SansSerif", Font.BOLD, 24) );
g.drawString( "Bouncing Hoops!", 50, 170);
}
}