FlagApplet.java

Live Applet here!
Author: Roseanne Zhang
import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.net.*; 
import java.util.*; 
import java.io.*; 


public class FlagApplet extends Applet implements ActionListener, Runnable { 
   static boolean   bIsApp   = false;
   TextField txtNum     = new TextField(3);
   Label     lbl        = new Label("1..00");
   Button    goBtn      = new Button("Go"); 
   Button    backBtn    = new Button("Back"); 
   Button    firstBtn   = new Button("First"); 
   Button    lastBtn    = new Button("Last"); 
   Button    stopBtn    = new Button("Stop"); 
   Button    restartBtn = new Button("Restart"); 
   Button    killBtn    = new Button("Kill"); 
   Panel     btnPanel   = new Panel(); 
   TextField txtFld     = new TextField();
   
   String[]  sFlag;
   int       count = -1;
   int       maxCount;
   Image[]   imgFlag;
   
   Thread            drawThread; 
   LoadImageThread1  loadImgThread;
   
   
   boolean   bStopped = false;
   boolean   bKilled  = false;
   boolean   bUserSet = false;
   boolean   bNoImage = false;
   int w; 
   int h;
   int btnH;
   int txtH;
   
   boolean getData(){ 
      try {
         LineNumberReader lnr;
         if (bIsApp) {
            lnr = new LineNumberReader(new InputStreamReader(new FileInputStream("Flags.txt") ));
         }
         else {
             lnr = new LineNumberReader(new InputStreamReader(new URL(getCodeBase(), "Flags.txt").openStream() ));
         }
         
         String sTmp = lnr.readLine();
			maxCount = Integer.parseInt(sTmp);
			sFlag    = new String[maxCount];
			imgFlag  = new Image[maxCount];
         for (int i = 0; i < maxCount; i++) {
            sTmp = lnr.readLine();
            sTmp.trim();
            sFlag[i] = new String(sTmp);
         }
      }
      catch (Exception e) {
          txtFld.setText(e.toString());
         return false;
      } 
      return true;   
   }
   
   public FlagApplet(){
      this.setBackground(new Color(208, 208, 255)); 
      setLayout(new BorderLayout()); 

      btnPanel.add(lbl); 
      btnPanel.add(txtNum); 
      btnPanel.add(goBtn); 
      btnPanel.add(backBtn); 
      btnPanel.add(firstBtn); 
      btnPanel.add(lastBtn); 
      btnPanel.add(stopBtn); 
      btnPanel.add(restartBtn); 
      btnPanel.add(killBtn); 
      
      add(btnPanel,BorderLayout.NORTH); 
      add(txtFld,BorderLayout.SOUTH); 
      
      goBtn     .addActionListener(this); 
      lastBtn   .addActionListener(this); 
      firstBtn  .addActionListener(this); 
      backBtn   .addActionListener(this); 
      stopBtn   .addActionListener(this); 
      restartBtn.addActionListener(this); 
      killBtn   .addActionListener(this); 
   }
   
   public void init() { 
      if (!getData()) {
         txtFld.setText("No images found.");
      }
      else {
         String sTmp = "1.." + maxCount;
         lbl.setText(sTmp);
         loadImgThread = new LoadImageThread1(this);
         loadImgThread.start();
      
         if (bIsApp) {
            start();
         }
      }       
   }
    
   public void start()  { 
      if (bKilled)
         return;
      
      if (drawThread == null)   
      {
         drawThread = new Thread(this); 
         drawThread.start(); 
      }
      else {
         bStopped = false;
      }         
   }
   
   public void stop()  {
      bStopped = true; 
   }
   
   public void run()  { 
      txtFld.setText("Thread started");
      while(!bKilled) { 
         if (!bStopped) {
            if (!bUserSet && loadImgThread.bLoaded) {
               count++; 
               count %= maxCount;// wrap around to zero 
            }
            repaint(); 
         }
         bUserSet = false;
         try  { 
            Thread.sleep(3000); 
         } 
         catch(InterruptedException e) { 
            //txtFld.setText("Interrupted"); 
         } 
      } 
      txtFld.setText("Thread dead");
   } 

   public void paint(Graphics g) { 
      w    = getSize().width; 
      h    = getSize().height;
      btnH = btnPanel.getSize().height;
      txtH = txtFld.getSize().height; 
      
      if (loadImgThread.bLoaded)
      {
         if (count>=0) {
            txtFld.setText(sFlag[count] + "  #" + (count + 1));
            g.drawImage(imgFlag[count], 0, btnH, w, h - btnH - txtH, this);
         }
      }
      else
         g.drawString("Loading images...", 10, btnH + 20);
   } 

   public void actionPerformed(ActionEvent evt) { 
      if (bUserSet || bKilled)
         return;
         
      String command = evt.getActionCommand(); 
      if (command == "Kill")  { 
         bKilled = true;
         txtFld.setText(command);
         return;
      }
      
      if (command == "Stop")  { 
         bStopped = true;
         txtFld.setText(command);
         return;
      }
      
      if (bStopped){
         if (command == "Restart")  { 
            bStopped = false;
            txtFld.setText(command);
         }
         return;
      }
      
      if (command != "Restart") {
         if (command == "Go")  { 
            String s = txtNum.getText();
            try {
               count=Integer.parseInt(s) - 1;
            }
            catch (NumberFormatException e) {
               count = 0;
            }
            if (count < 0) 
               count = maxCount - 1 ; 
            else
               count %= maxCount;// wrap around to zero 
               
            txtNum.setText(new Integer(count + 1).toString()); 
         }  
         else if(command == "Back") { 
            count--; 
            if (count < 0) 
               count = maxCount - 1 ; 
         } 
         else if (command == "First") { 
            count = 0; 
         } 
         else if (command == "Last")  { 
            count = maxCount - 1; 
         } 
         txtFld.setText(command);
         bUserSet = true;
         drawThread.interrupt();
         return;
      }
   } 
   
   public static void main(String[] args) {
      Frame  f = new Frame("Test");
      f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
         });
      f.setSize(500, 400);
      
      FlagApplet.bIsApp = true;         
      FlagApplet a = new FlagApplet();

      f.add(a);
      a.init();
      f.setVisible(true);
   }
}

class LoadImageThread1 extends Thread {
   boolean    bLoaded = false;
   FlagApplet prt;
   
   LoadImageThread1(FlagApplet prt){
      super();
      this.prt = prt;
   }
   
   public void run() {
      if (prt.imgFlag == null)
         return;
         
      MediaTracker tracker = new MediaTracker(prt); 
      Toolkit t = null;
      if (prt.bIsApp) {
         t = Toolkit.getDefaultToolkit();
      }
       
      try {
         int maxCount = prt.imgFlag.length;   
         for (int a = 0; a < maxCount; a++)  { 
            if (prt.bIsApp) {
               prt.imgFlag[a] = t.getImage(prt.sFlag[a].toLowerCase() + ".gif"); 
            }
            else {
               prt.imgFlag[a] = prt.getImage (prt.getCodeBase(), prt.sFlag[a].toLowerCase() + ".gif"); 
            }
            if (prt.imgFlag[a] == null){
               prt.bNoImage = true;
               throw new Exception("no flags");
            }
            else
               tracker.addImage(prt.imgFlag[a], 0); 
         } 
      }
      catch (Exception e) {
         prt.txtFld.setText(e.toString()); 
      }
      
      try  { 
         tracker.waitForAll(); 
      } 
      catch(InterruptedException e)  { 
         prt.txtFld.setText(e.toString()); 
      } 
      bLoaded = true;
   }
}

Last updated: 08-06-2000
[ Code Share] [ SCJP FAQ] [ SCJD Group] [JavaChina]
Copyright © 1999 - 2001 Roseanne Zhang, All Rights Reserved