Capt. Horatio T.P. Webb
Applet Demo
Parks -- Fall 2001
Last Updated 12PM 11/19/2001


Captain Webb Flips Out

Then source code for the applet above is shown below. It is based on the Sun Java 2 SDK 1.3. In addition to the Java source, there are fifty images that make up the flip (named cfli0001.gif thru cfli0050.gif and the loader background named cflip,gif).

The general idea is to demonstrate:

  1. the general structure of the applet (class, init, start, run)
  2. two radio buttons and their associated ItemListener (ItemStateChanged)
  3. a slider for speed control (AdjustmentListener)
  4. some labels
  5. FlowLayout
  6. the thread
  7. memory sprites and loading them from *.gif files (ImageLoader)
  8. the run loop of that does: drawImage, paint and repaint

The java source code (showflip.java) is shown below.

//***********************************************************************
//
// showflip.applet 11/21/2001
//
// by: parks@uh.edu
//
//
//***********************************************************************

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import java.awt.image.*;
import java.awt.Graphics.*;

public class showflip extends Applet implements Runnable, ItemListener,  AdjustmentListener


{
private boolean isRunning = false;
private int cur;
private int swidth, sheight;        // windows sizes
private Image flipimage;       // 320x240 3d window
private Graphicsg_handle;//graphics handle for 3d window
Image memSprite[]=new Image[51]; // vector of memory image sprites
private String load_string = "Loading Image: "; // loading string appears in 3d window
private String fn;  //file name string
private String intpart; //integer part of fn string
private int itemp;
private int fb;
private int waittime;   // delay between frame display
private String delaystring;
private Thread showflip_thread = null;
public CheckboxGroup forw_back_radios; // checkbox group id for flip forward or backwards
public Checkbox frad; // checkbox for forward flip
public Checkbox brad; // checkbox for backward flip
public Scrollbar flipspeed;   //scrollbar for speed choice
public Label slowertext;
public Label fastertext;
public Label msspeed;


public showflip ()
{

}
public String getAppletInfo()
{
return "Name:showflip\r\n"+"Author: Parks";

}
public void init()
{
              CheckboxGroup forw_back_radios = new CheckboxGroup();
              Checkbox frad = new Checkbox ("Front", false, forw_back_radios);
              Checkbox brad = new Checkbox ("Back", true, forw_back_radios);

              flipspeed = new Scrollbar(Scrollbar.HORIZONTAL,25,5,0,105);

              fastertext = new Label("Faster",Label.LEFT);
              slowertext = new Label("Slower Delay=",Label.LEFT);
              msspeed = new Label ("25ms",Label.LEFT);

              setForeground(Color.black);
              swidth=340;
              sheight=300;
              setLayout(new FlowLayout(FlowLayout.LEFT,0,0));

              add (frad);
              frad.addItemListener(this);

              add (brad);
              brad.addItemListener(this);

              add(fastertext);
              add(flipspeed);
              flipspeed.addAdjustmentListener(this);
              add(slowertext);
              add(msspeed);

              flipimage = createImage(swidth,sheight);

              g_handle = flipimage.getGraphics();

              if (showflip_thread == null)
                {
                 showflip_thread = new Thread(this);
                 showflip_thread.start();
                 }
               isRunning = true;// set applet switch to on

} // end init

public void destroy()
{
}
public void update( Graphics g )
{
    paint( g );
}
public void paint(Graphics g)
{
    if ( isRunning ) //draw the current image
      {
       g.drawImage(flipimage,0,0,this );
      }
}
public void start()
{
              cur=1;
              fb=2;
              waittime=25;
}
public void stop()
{
   if (showflip_thread != null ) // && showflip_thread.isAlive() )
      {
        showflip_thread.stop();
        showflip_thread = null;
       }
}
public void run()
{

Color ltgray = new Color(192,192,192);
g_handle.setColor( ltgray );
g_handle.fillRect( 0, 0, 360, 300 );
repaint();
for (itemp=0 ; itemp < 51 ; itemp++)
   {
    if (itemp==0)  //****** load and display title image
      {
         memSprite[0]=ImageLoader( getDocumentBase(), "cflip.gif" );
         g_handle.drawImage( memSprite[0], 20, 40, this );
         repaint();
      }
    else           //*** set wait times, build file names and load them
      {
        if (itemp < 10) fn="cfli000";
        if (itemp >=10 && itemp <100) fn="cfli00";
        fn=fn+Integer.toString(itemp);
        intpart=load_string+Integer.toString(itemp)+" of 50";
        fn=fn+".gif";
        g_handle.setColor( Color.blue);
        g_handle.drawImage( memSprite[0], 20, 40, this  );
        g_handle.drawString(intpart,25,100);
        repaint();
        memSprite[itemp]=ImageLoader( getDocumentBase(), fn );
       }
}
while (true)
{
  try
     {
       if ( isRunning )
         {
          g_handle.drawImage( memSprite[cur], 20, 40, this);
         }
        repaint();
        if (cur == 1)
           {
            Thread.sleep(1000);
           }
        else
          {
           Thread.sleep(waittime);
          }
        if(fb == 2) //***forward back
          {
           cur=cur+1;
           if (cur > 50) cur = 1;
          }
        else    //**** backward flip
          {
           cur=cur-1;
           if (cur <1) cur=50;
          }
      }
      catch (InterruptedException e)
         {
           stop();
         }
}

} //end run
private Image ImageLoader( URL url, String sFilename )
{
  MediaTracker tracker = new MediaTracker( this );
  Image img = getImage( url, sFilename );
  tracker.addImage( img, 0 );
  try
   {
     tracker.waitForAll();
    }
   catch (InterruptedException e) {}
    return img;
}
public void itemStateChanged(ItemEvent event)
{
//
//process the flip choice
//
  String iselect;
  iselect=event.getItem().toString();
  if(iselect.equals("Front"))
    {
      fb=1;
      cur=1;
    }
  if(iselect.equals("Back"))
    {
     fb=2;
     cur=50;
    }
} //**** end forward backward radios

public void adjustmentValueChanged(AdjustmentEvent event)
     {
//
//   process the slider adjustment
//
     waittime=flipspeed.getValue();
     delaystring=String.valueOf(waittime);
     delaystring=delaystring.trim();
     msspeed.setText(delaystring+"ms");
     }
}//end applet