荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: icefire (as you wish), 信区: Java
标  题: JAVA编写的CGI
发信站: BBS 荔园晨风站 (Wed Dec  2 08:12:07 1998), 站内信件

发信人: wooce (残雪), 信区: CGI
标  题: JAVA编写的CGI--Logon Version1.0
发信站: 华南网木棉站 (Tue Dec  1 13:46:09 1998), 转信

//本applet可在给定的延迟时间后装入给定的URL地址的网页
//在URL装入前有欢迎信息和剩余时间显示
/*
 * Applet designed to do a Java-Pull (similar to server push)
 * Created by the Silver Surfer
 * stan@systex.com
 *
 */

import java.net.URL;
import java.net.MalformedURLException;
import java.lang.*;
import java.applet.*;
import java.awt.*;

/**
 * An applet used to load Stan's News Page
 *
 * author  Silver Surfer(stan@systex.com)
 */

public class link extends Applet implements Runnable
{
   private Thread    thread;            //** applet thread */
   private String    initMessage;       //** Message (before Java-Pull) */
   private String    loadedMessage;     //** Message (after Java-Pull) */
   private String    urlparam;          //** The Link */
   private String    fontString;        //** The Link */
   private String    errMessage;        //** Error Message to display */
   private String    news;
   private String    text;              //** text color */
   private String    back;              //** backgound color */
   private int       style;             //**
   private Dimension lastSize;          //** last know size */
   private int       lastMessageLen;    //** last length of message */
   private Font      font;                //** Font Name */

   private Color     bgColor;           //** applet bgcolor */
   private Color     fgColor;           //** applet fgcolor */
   private URL       url ;
   private boolean   urlLoaded = false; //**  URL loaded or not */
   private boolean   debug = false;     //**  debug flag */


   /**
    * Info.
    */
   public String getAppletInfo() {
      return "Link by Silver Surfer (stan@systex.com)";
   }

   /**
    * Parameter Info
    */
   public String[][] getParameterInfo() {
      String[][] info = {
         { "initmsg",    "string",  "Initial message to display"  },
         { "loadedmsg",  "string",      "Message to display"  },
         { "delay",      "integer",     "Delay in sec. before load (1)"  },
         { "font",       "string",      "Message font (Courier)"  },
         { "size",       "string",      "Message font size (18)"  },
         { "news",        "string",      "URL"  },
         { "loadedurl",  "string",      "Alternate URL to use if loaded"  },
         { "bgco",       "hexadecimal", "background color (getBackground)"  },
         { "fgco",       "hexadecimal", "foreground color (black)"  },

      };
      return info;
   }

   /**
    * Error message
    */
   public void errorMsg(String str) {
      showStatus("Error: " + str);
      System.err.println("Error: " + str);
      System.err.flush();
   }

   /**
    * Cheesy debug...
    */
   public void dbg(String str) {
      if (debug) {
         System.out.println("Debug: " + str);
         System.out.flush();
      }
   }

   /**
    * Init - process parameters.
    */
   public void init()
   {
      urlparam = "news.html";
// THE LINK
      initMessage = "Hold on . .";
// initial message
        loadedMessage = "Wait . Then Click";
        fontString = "Courier";                 // post load message
        int size = 12;
        int style = Font.BOLD;
// post font size
        fgColor = Color.white;
        // text color
        bgColor = Color.black;
        // back color
        dbg("init()");
// init private variables
      errMessage= null;

      lastSize = new Dimension(1,1);
      lastMessageLen = 0;
      String dbgString = getParameter("debug");
// check if debug, using "debug" parameter
      if (dbgString != null)
         debug = true;
}
   /**
    * Convert a Hexadecimal String with RGB-Values to Color
    * Uses aDefault, if no or not enough RGB-Values
    */
   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;
      }
   }


   /**
    * Load URL.
    */
   public boolean loadUrl() {
      urlLoaded = true;         // an attempt was made
      if (url == null)
         return false;

      dbg("loadUrl()");

      try {
         getAppletContext().showDocument(url);
      }
      catch (Exception e) {
         if (url.getRef() == null) {
            errMessage = "Couldn't load link.";
         }
         else {
            errMessage = "Couldn't load link: " + url.getRef();
         }
         return false;
      }
      return true;
   }

   /**
    * Init size and resize applet.
    */
   public void initSize(Graphics g) {
      dbg("initSize()");
      int width = size().width;
      int height = size().height;

      // check and save sizes
      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 applet
      resize(width, height);

      // save current size
      lastSize.width = size().width;
      lastSize.height = size().height;
   }


/*
  public void update(Graphics g) {
      dbg("update()");
      g.setColor(bgColor);
      g.fillRect(0, 0, size().width, size().height);
      paint(g);
   }
   */

   /**
    * Paint the applet
    */
   public void paint(Graphics g) {
      dbg("paint()");
      if ((size().height != lastSize.height) ||
          (size().width != lastSize.width) ||
          (lastMessageLen != initMessage.length()))
         // init (or re-init) sizes if changed
         initSize(g);

      // set background
      g.setColor(bgColor);
      g.fillRect(0, 0, size().width, size().height);
      // set font
      g.setFont(font);

      if (errMessage != null) {
         // Error!
         initSize(g);
         // draw error message
         g.setColor(Color.red);
         String msg = "Wow an Error: " + errMessage;
         g.drawString(msg, 2, size().height/2+font.getSize()/2);
      }
      else {
         // draw message
         g.setColor(fgColor);
         g.drawString(initMessage, 2, size().height/2+font.getSize()/2);
      }
   }

   /**
    * Start the applet.
    */
   public void start() {
      dbg("start()");
      thread = new Thread(this);
      thread.start();
   }

   /**
    * Stop the applet.
    */
   public void stop() {
      dbg("stop()");
      thread = null;
   }

   /**
    * run - main
    */
   public void run() {
      dbg("run()");
      // we only loop until loaded
      while (!urlLoaded) {
         // repaint message
         repaint();
         // and delay before load of URL
         try {
            Thread.sleep(3000);                                         // get
delay in ms.
         }
         catch (InterruptedException e) {}
         // load URL
         if (!urlLoaded && errMessage == null) {
            // Not currently loaded and no error message; load!
            if (loadUrl()) {
               // loaded; reset message
               initMessage = loadedMessage;
            }
         }
      }
   }

   /**
    * Mouse up - load URL
    */
   public boolean mouseUp(Event evt, int x, int y) {
      if (loadUrl()) {
         // loaded; reset message
         initMessage = loadedMessage;
      }
      return true;
   }
}


--
来去如风

※ 来源:.BBS 荔园晨风站 bbs.szu.edu.cn.[FROM: 192.168.0.5]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店