import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TimedProgress extends JApplet
{
private Display canvas;
public TimedProgress ( )
{
this.setBackground ( Color.blue );
canvas = new Display ( );
canvas.setBackground ( Color.black );
getContentPane().add ( canvas, BorderLayout.CENTER );
}
public Insets getInsets ( )
{
return new Insets ( 5, 5, 5, 5 );
}
}
class Display extends JPanel implements ActionListener
{
private int boxheight;
private Timer timer;
private Font mine;
private String message;
public Display ( )
{
setLayout ( null );
mine = new Font ( "Serif", Font.PLAIN, 48 );
boxheight = 400;
if (timer == null)
{
timer = new Timer ( 50, this );
timer.start ( );
}
}
public void paintComponent ( Graphics g )
{
super.paintComponent ( g );
g.setColor ( Color.white );
g.fillRect ( 15, 15, 410, 50 );
g.setColor ( Color.red );
g.fillRect ( 20, 20, boxheight, 40 );
g.setFont ( mine );
g.setColor ( Color.white );
switch ( boxheight )
{
case 400: message = "Better hurry!"; break;
case 350: message = "Time marches on . . ."; break;
case 300: message = "Hey, speed up!"; break;
case 250: message = "Don't dilly-dally!"; break;
case 200: message = "Go, go, go!"; break;
case 150: message = "Pick up the pace!"; break;
case 100: message = "Watch the time!"; break;
case 50: message = "Running out of time!"; break;
case 0: message = "Time is up!"; break;
}
g.drawString ( message, 15, 160 );
}
public void actionPerformed(ActionEvent evt)
{
if ( boxheight > 0 )
{
boxheight--;
repaint ( );
}
else
{
timer.stop();
timer = null;
}
}
}
Back to Lesson 31 Examples
Back to Java Main Page