⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 countdown.java

📁 Java Applet实例讲解 N多例子
💻 JAVA
字号:
import java.net.*;
import java.applet.*;
import java.awt.*;

public class CountDown extends Applet implements Runnable 
{
   private int width,height;
   private Image offI;
   private Graphics offG;
   
   private Thread    thread;
   private String    initMessage;
   private String    loadedMessage;
   private String    errMessage;   
   private Dimension lastSize;	   
   private int       lastMessageLen;
   private int       delay;		
   private Font      font;		
   private FontMetrics fm;
   
   private Color     bgColor;		
   private Color     fgColor;		
   private URL       url = null;	
   private boolean   urlLoaded = false;	
   private boolean   debug = false;	

   public void countdown()
   {
     font = new Font("Courier", Font.BOLD, 200);
     offG.setFont(font);
     fm = offG.getFontMetrics();
     String s = new String("");
     resize(width,height);
     int i;
     for(i=10; i>=0; i--)
     {
       try
       {
         s = String.valueOf(i);
         offG.setColor(Color.black);
         offG.fillRect(0,0,width,height);
         offG.setColor(Color.red);
         offG.drawString(s,(width-fm.stringWidth(s))/2,height*4/5);
         repaint();
         Thread.sleep(1000);
       }
       catch(InterruptedException e){}
       
       if(i==0)
       {
         offG.setColor(Color.black);
         offG.fillRect(0,0,width,height);
         offG.setColor(Color.red);
         offG.drawString("Go!!",(width-fm.stringWidth("Go!!"))/2,height*4/5);
         repaint();
       }
     }
     
   }
   
   public void errorMsg(String str) 
   {
      showStatus("Error: " + str);
      System.err.println("Error: " + str);
      System.err.flush();
   }
   
   public void dbg(String str) 
   {
      if (debug) 
      {
	 System.out.println("Debug: " + str);
	 System.out.flush();
      }
   }    
   
   public void init()
   {
      width = this.getSize().width;
      height = this.getSize().height;
      offI = createImage(width,height);
      offG = offI.getGraphics();
      
      dbg("init()");
      errMessage= null;
      lastSize = new Dimension(1,1);
      lastMessageLen = 0;

      String dbgString = getParameter("debug");
      if (dbgString != null) 
	 debug = true;
      
      initMessage = getParameter("initmsg");
      if (initMessage == null)
	 initMessage = "";
      loadedMessage = getParameter("loadedmsg");
      if (loadedMessage == null)
	 loadedMessage = "Already loaded. Click to force re-load...";

      String fontName = getParameter("font");
      if (fontName == null)
	 fontName = "Courier";
      String fontSize = getParameter("size");
      if (fontSize == null)
	 fontSize = "18";
      int size = Integer.valueOf(fontSize).intValue();
      font = new Font(fontName, Font.BOLD, size);
      
      bgColor = readColor(getParameter("bgcolor"), getBackground());
      fgColor = readColor(getParameter("fgcolor"), Color.black);
      
      String delayString = getParameter("delay");
      if (delayString == null)
	 delayString = "1";
      delay = Integer.valueOf(delayString).intValue();

      String urlParam =  getParameter("URL");
      if (urlParam != null) {
	 dbg("url: " + urlParam);
	 try {
	    url = new URL(getDocumentBase(), urlParam);
	 }
	 catch (MalformedURLException e) {
	    url = null;
	 }
	 if (url == null) {
	    try {
	       url = new URL(urlParam);
	    }
	    catch (MalformedURLException e) {
	       url = null;
	       errMessage = "Cound't create URL for: " + urlParam;
	    }
	 }
      }
      else {
	 dbg("Parameter 'url' not specified.");
	 errMessage = "Parameter 'url' not specified.";
	 url = null;
      }
   }
   
   public Color readColor(String aColor, Color aDefault) {
      if ((aColor == null) ||
	  (aColor.charAt(0) != '#') ||
	  (aColor.length() != 7 )) {
	 return aDefault;
      }

      try {
	 Integer rgbValue = new Integer(0);
	 rgbValue = Integer.valueOf(aColor.substring(1, 7), 16);
	 return new Color(rgbValue.intValue());
      }
      catch (Exception e) {
	 return aDefault;
      }
   }

   public boolean loadUrl() {
      urlLoaded = true;	
      if (url == null)
	 return false;

      dbg("loadUrl()");
      
      try {
         countdown(); 
	 getAppletContext().showDocument(url);
      }
      catch (Exception e) {
	 if (url.getRef() == null) {
	    errMessage = "Couldn't load url. Try to re-start Netscape :-(";
	 }
	 else {
	    errMessage = "Couldn't load url: " + url.getRef();
	 }
	 return false;
      }
      return true;
   }

   public void initSize(Graphics g) {
      dbg("initSize()");
      int width = size().width;
      int height = size().height;

      lastMessageLen = initMessage.length();
      FontMetrics fm = getFontMetrics(font);
      int fh = fm.getHeight();
      int fw = fm.stringWidth(initMessage);
      if (fh > height) 
	 height = fh;
      if (fw > width)
	 width = fw;
      resize(width, height);

      lastSize.width = size().width;
      lastSize.height = size().height;
   }

   public void update(Graphics g)
   {
     paint(g);
   }
   
   
   public void paint(Graphics g) 
   {
      g.drawImage(offI,0,0,this);
   }

   public void start() {
      dbg("start()");
      thread = new Thread(this);
      thread.start();
   }

   public void stop() {
      dbg("stop()");
      thread = null;
   }

   public void run() {
      dbg("run()");

      while (!urlLoaded) {

	 repaint();

	 try {
	    Thread.sleep(delay*1000);
	 }
	 catch (InterruptedException e) {}
	 if (!urlLoaded && errMessage == null) {
	 //    countdown();  //Do countdown

	    if (loadUrl()) {

	       initMessage = loadedMessage;
	    }
	 }
      }
   }
   public boolean mouseUp(Event evt, int x, int y) {
      if (loadUrl()) {
	 initMessage = loadedMessage;
      }
      return true;
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -