[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help



 
    
Thus spake Hercule Li - TDC/BJ (hl108890@xxxxxxxxxxx):

> Hi Terry,
> 
> The below is a java program , and it is a applet,
> so can't be run directly.
> 
> Would you pls. tell me how to run it with netscape ?
> 
> Thanks
> Dong
> 
> 
> import java.awt.*;
> import java.applet.*;
> 
> public class Applet1 extends Applet {
>   public void paint(Graphics g) {
>     g.drawString("First applet", 10, 10);
>   }
> } ///:~

I haven't too many experience of Java. But I have an example
that could show how to implement Java applet on web. A java
based program called DigitalClock.java (source code), compile
it to bycodes file - DigitalClock.class by using JDK, write
your HTML file including this java applet. 

Source code is a little bit long. But it doesn't matter. 
You can outline which part you need.

Cheers,
  
-- 
Terrence Miao                       
ITS of University of Macquarie         	        ...   __o         
(03) 8344 0361 t.miao@xxxxxxxxxxxxxxxxxx      ...    -\<,  
http://igloo.its.unimacq.edu.au                ... (_)/(_) ..


DigitalClock.java 
-----------------


import java.util.*;
import java.awt.*;
import java.applet.*;
import java.net.URL;
import java.net.MalformedURLException;
// Steffen Thorsen 1996

public class DigitalClock extends Applet implements Runnable {
  Thread timer = null;

  int width, height, tz ;
  final int bars[]= {95,5,118,117,45,121,123,69,127,125,0};
  Color CBack, CFront, CBorder;
  int bwidth,bsize,bspace,bdsize,between,bulletsize,startdst,sluttdst, bordersize ;
  int xoffset, yoffset, clockw, clockh;
  Color far[];

  String lastdate = "00:00:00" ,today=lastdate;

public void init() {
  int x,y;

  far=new Color[7];

  bsize = readarg("barsize",8);
  bwidth = readarg("barwidth", 3 );
  bspace = readarg("barspace", 1 );
  bulletsize = readarg("bulletsize",(1 + bsize/4));
  between = readarg("between", (1 + bsize/4) );

  bordersize = readarg("bordersize", 0);

  startdst = readarg("dststart" ,0 );
  sluttdst = readarg("dstend", 0);

  if (bsize<2) {bsize=2;}
  if (bwidth>bsize/2) {bwidth=bsize/2;}
  bdsize=bsize-1;

  CFront=colorFromString(getParameter("text"),Color.black);
  CBack=colorFromString(getParameter("bgcolor"),Color.lightGray);
  CBorder=colorFromString(getParameter("bordercolor"),Color.blue);

  Date dat = new Date();  tz = -dat.getTimezoneOffset();
  if (((tz+1000) % 10)==9) {tz++;}

  String un = getParameter("tz"); if (un != null) {tz=(Integer.valueOf(un).intValue());};

  tz*=60;

  clockw=(int) 6*bsize+bulletsize*2+12*bspace+7*between; 
	/*=6*(bsize+bspace*2+between) + (between+bsize/4+1)*2;*/
  clockh=3*bspace+2*bsize;

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

  xoffset=(width-clockw)/2;
  yoffset=(height-clockh)/2;

  calcval();
}
  
  private int readarg(String argtext, int defverdi) {
    String argvalue = getParameter(argtext);
    return( (argvalue == null) ? defverdi : (Integer.valueOf(argvalue).intValue()));
  }

  private void calcval() {
    String ts, tm, tt;
    int s, m, h;
    Date dat = new Date();
    
    int tid=(int) (System.currentTimeMillis() /1000) + tz;

    if ((tid>=startdst)&&(tid<sluttdst)) {tid+=3600;}

    s=tid % 86400;
    h=s / 3600;
    m=(s % 3600)/60;
    s%=60;

    ts = String.valueOf(s);
    tm = String.valueOf(m);
    tt = String.valueOf(h);
    if(s<10) {ts = "0" + ts;}
    if(m<10) {tm = "0" + tm;}
    if(h<10) {tt = "0" + tt;}
    
    today = tt + ":" + tm + ":" + ts + "--------";
  }

  public void update(Graphics g) {
    calcval();
    tegnny(g,true);
  }

  public void paint(Graphics g) {
    if (bordersize>0) {
      g.setColor(CBorder);
      g.fillRect(0,0,width,height);
    }

    g.setColor(CBack);
    g.fillRect(bordersize,bordersize,width-bordersize*2,height-bordersize*2);
    tegnny(g,false);
  }

  public boolean mouseDown(java.awt.Event evt, int x, int y) {
    try {
      URL homepage=new URL("http://www.stud.unit.no/USERBIN/steffent/javaclock.pl";);
      getAppletContext().showDocument(homepage);
    }
    catch (MalformedURLException e){}
    return true;
  }

  private void tegnny(Graphics g,boolean ny) {
    
    int a, b, xh;
      
    xh = xoffset ;      
    for(int i=0; i<8 ; i++) {
      a=(int) today.charAt(i) ;
      b=(int) lastdate.charAt(i) ;
      
	if ((a>47) && (a<58)) {

	  if ((b!=a)|| (!ny)) {
	    tegndigit(g,true,xh,a-48);}

	  xh+=bsize+bspace*2+between;
	}
	else {
	    if (!ny) {	    
	      g.setColor(CFront);
	      g.fillOval(xh+(between-bulletsize)/2,bsize*2/3+yoffset,bulletsize,bulletsize);
	      g.fillOval(xh+(between-bulletsize)/2,bspace+bsize*4/3+yoffset,bulletsize,bulletsize);
	    }
	    xh+=between+bulletsize;
	  }
	
    }
    lastdate = today;

  }

  private void tegndigit(Graphics g,boolean ny,int x,int val) {
    
    int y=yoffset,x0,y0,bw2;
    int offy=1- bwidth % 2;
    int x2=bspace*2+bsize-1;
    int y2=bspace*2+bsize+offy+y-1;
    int y3=y2+bspace+bsize;
    int y1=bspace/2+bspace+bsize+y;
    
    val=bars[val];
    for(x0=0;x0<7;x0++) {
      far[x0]=(val>63) ? CFront : CBack;
      val=(val % 64)*2;
    }
    

    // Overste
    
    g.setColor(far[0]);
    for(x0=0;x0<bwidth;x0++) { g.drawLine(x+bspace+x0,y+x0,x+bspace+bdsize-x0,y+x0);}
    
    // Vertikale overst
    g.setColor(far[3]);
    for(x0=0;x0<bwidth;x0++) {g.drawLine(x+x0,y+bspace+x0,x+x0,y+bdsize+bspace-x0);}
    
    g.setColor(far[4]);
    for(x0=0;x0<bwidth;x0++) {g.drawLine(x+x2-x0,y+bspace+x0,x+x2-x0,y+bdsize+bspace-x0);}
    
    // Midt-planken
    
    g.setColor(far[1]);
    bw2=(bwidth+1)/2;
    for(x0=0;x0<bw2;x0++) {
      g.drawLine(x+bspace+x0,y1-x0,x+bspace+bdsize-x0,y1-x0);
      g.drawLine(x+bspace+x0,y1+offy+x0,x+bspace+bdsize-x0,y1+offy+x0);
    }
    
    // Nederste
    g.setColor(far[5]);
    for(x0=0;x0<bwidth;x0++) {g.drawLine(x+x0,y2+bspace+x0,x+x0,y2+bdsize+bspace-x0);}

    g.setColor(far[6]);
    for(x0=0;x0<bwidth;x0++) {g.drawLine(x+x2-x0,y2+bspace+x0,x+x2-x0,y2+bdsize+bspace-x0);}
    
    g.setColor(far[2]);
    for(x0=0;x0<bwidth;x0++) {g.drawLine(x+bspace+x0,y3-x0,x+bdsize+bspace-x0,y3-x0);}
    
  }
								 
  private Color colorFromString(String s,Color defaultColor) {
    Integer i;
    try {
      i=Integer.valueOf(s,16);
      return new Color(i.intValue());
    } catch (NumberFormatException e) {return defaultColor;}
  }

  public void start() {
    if(timer == null)
      {
	timer = new Thread(this);
	timer.start();
      }
  }
  
  public void stop() {
    timer = null;
  }
  
  public void run() {
    while (timer != null) {
      try {Thread.sleep((int) (1020 - System.currentTimeMillis() % 1000));} catch (InterruptedException e){}
      repaint();
    }
    timer = null;
  }

}



DigitalClock.html
-----------------

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage 2.0">
<title></title>
</head>

</body>

<P>
<APPLET WIDTH=88 HEIGHT=20 CODE="DigitalClock.class"></APPLET>
</P>

</body>
</html>

Google