java applet 教程,一个经典的JAVA APPLET程序(二)-JSP教程,Java技巧及代码

接着上面的部分,下面是applet主程序部分:

public class myclock extends applet implements runnable

{

static final int background=0; //背景图片的序号,供数组使用

static final int logo=1; //logo图片的序号,供数组使用

static final string javex="bear"; //表盘上显示的文字

static final double minsec=0.104719755; //分针和秒针在表盘上的刻度(60个)间的弧度

static final double hour=0.523598776; //时针在表盘上的刻度(24个)间的弧度

thread clockthread = null; //使用多线程机制,用另一个线程不断显示图片

//提供的默认参数,如果html文件里面没有给参数就使用

int width = 100;

int height = 100;

color bgcolor = new color(0,0,0);

color facecolor = new color(0,0,0);

color sweepcolor = new color(255,0,0);

color minutecolor = new color (192,192,192);

color hourcolor = new color (255,255,255);

color textcolor = new color (255,255,255);

color casecolor = new color (0,0,0);

color trimcolor = new color (192,192,192);

string logostring=null;

image images[] = new image[2]; //背景和logo的图片

boolean ispainted=false; //如果第一次载入时绘制背景及表盘,其他时候重绘则只绘制指针

//时钟圆心的位置

int x1,y1;

//最上面那个三角形的刻度图形的坐标

int xpoints[]=new int[3], ypoints[]=new int[3];

//保存当前时间,转换成(double)(hours + minutes/60)

hms cur_time;

//秒针、分针、时针

sweephand sweep;

hmhand minutehand,

hourhand;

//用于绘制的时、分、秒

double lasthour;

int lastminute,lastsecond;

//显示日期和表盘上的字母的字体

font font;

//图片显示使用了缓冲机制,offscrimage和offscrgc存放缓冲区内图片的信息

image offscrimage;

graphics offscrgc;

//用于测试背景图片和logo图片

mediatracker tracker;

int mindimension; // 如果背景区域不是正方形的话,保证时钟在中间显示

int originx; // 时钟图形所在正方形区域的左上角x坐标

int originy; // 时钟图形所在正方形区域的左上角y坐标

double tzdifference=0; //时区间的差,向西为负数,向东为正数

boolean localonly=false; //是否只使用本地时间,如果为false则可以根据传入的时区显示该时区时间

//保存参数的类型说明

public string[][] getparameterinfo()

{

string[][] info = {

{"width", "int", "applet的长度,以象素为单位"},

{"height", "int", "applet的宽度,以象素为单位"},

{"bgcolor", "string", "背景颜色,e.g ff0000"},

{"facecolor", "string", "表盘颜色"},

{"sweepcolor", "string", "秒针颜色"},

{"minutecolor", "string", "分针颜色"},

{"hourcolor", "string", "时针颜色"},

{"textcolor", "string", "文本颜色"},

{"casecolor", "string", "框内颜色"},

{"trimcolor", "string", "空白区颜色"},

{"bgimageurl", "string", "背景图片地址"},

{"logostring", "string", "logo字符"},

{"logoimageurl","string", "logo图片地址"},

{"timezone", "real", "时区间的差"},

{"localonly", "int", "是否考虑到时区差别"}

};

return info;

}

//显示信息

public string getappletinfo()

{

return "版权所有,copy必究,保护正版,人人有责";

}

void showurlerror(exception e)

{

string errormsg = "url错误:"+e;

showstatus(errormsg);

system.err.println(errormsg);

}

//相当于把时钟变成100×100的大小,percent即相对坐标

private int size(int percent)

{

return (int)((double)percent/100.0 * (double)mindimension);

}

public void init()

{

url imagesurl[] = new url[2];

string szimagesurl[] = new string[2];

tracker = new mediatracker(this);

//得到html页面提供的参数,并把它转换相应的格式

string paramstring = getparameter( "width" );

if( paramstring != null )

width = integer.valueof(paramstring).intvalue();

paramstring = getparameter( "height" );

if( paramstring != null )

height = integer.valueof(paramstring).intvalue();

paramstring = getparameter( "timezone" );

if( paramstring != null )

tzdifference = double.valueof(paramstring).doublevalue();

paramstring = getparameter( "localonly" );

if( paramstring != null && integer.valueof(paramstring).intvalue() != 0){

localonly=true;

tzdifference=0.;

}

paramstring = getparameter( "bgcolor");

if( paramstring != null )

bgcolor=parsecolorstring(paramstring);

paramstring = getparameter( "facecolor");

if( paramstring != null )

facecolor=parsecolorstring(paramstring);

paramstring = getparameter( "sweepcolor");

if( paramstring != null )

sweepcolor=parsecolorstring(paramstring);

paramstring = getparameter( "minutecolor");

if( paramstring != null )

minutecolor=parsecolorstring(paramstring);

paramstring = getparameter( "hourcolor");

if( paramstring != null )

hourcolor=parsecolorstring(paramstring);

paramstring = getparameter( "textcolor");

if( paramstring != null )

textcolor=parsecolorstring(paramstring);

paramstring = getparameter( "casecolor");

if( paramstring != null )

casecolor=parsecolorstring(paramstring);

paramstring = getparameter( "trimcolor");

if( paramstring != null )

trimcolor=parsecolorstring(paramstring);

logostring = getparameter( "logostring");

if( logostring == null )

logostring=javex;

else if(logostring.length() > 8)

logostring= logostring.substring(0,8); //限制8个字母,否则显示不下!

//szimagesurl数组根据html传入参数存放图片的文件名

szimagesurl[background] = getparameter("bgimageurl");

szimagesurl[logo] = getparameter("logoimageurl");

//测试图片

for(int i=0; i<2; i++){

if(szimagesurl[i] != null){

//根据codebase参数与image文件名得到image的路径,测试是否存在,如果不存在就不用图片

try{

imagesurl[i]=new url(getcodebase(),szimagesurl[i]);

} catch (malformedurlexception e)

{

showurlerror(e);

imagesurl[i]=null;

images[i]=null;

}

if(imagesurl[i] != null){

showstatus("加载图片: " + imagesurl[i].tostring());

images[i]=getimage(imagesurl[i]);

if(images[i] != null)

tracker.addimage(images[i],i);

showstatus("");

}

try{

tracker.waitforall(i);

}catch (interruptedexception e){}

}

else images[i]=null;

}

//得到相应时区的时间

cur_time=(localonly)? new hms() : new hms(tzdifference);

lasthour=-1.0;

lastminute=-1;

lastsecond=-1;

x1=width/2;

y1=height/2;

//换算出圆形时钟在背景里的左上角坐标

mindimension= math.min(width, height);

originx=(width-mindimension)/2;

originy=(height-mindimension)/2;

//计算出上面三角形的三个点的坐标

xpoints[1]=x1-size(3); xpoints[2]=x1+size(3); xpoints[0]=x1;

ypoints[1]=y1-size(38);ypoints[2]=y1-size(38); ypoints[0]=y1-size(27);

//计算出秒针、分针、时针图形的各个点坐标

sweep=new sweephand(x1,y1,size(40),3);

minutehand=new hmhand(x1,y1,size(40),size(6),6);

hourhand=new hmhand(x1,y1,size(25),size(8),6);

//构造字体

font=new font("txt",font.bold,size(10));

//构造缓冲区内图形

offscrimage = createimage(width,height);

offscrgc = offscrimage.getgraphics();

system.out.println(getappletinfo());

}

public void start() //开始启动显示线程

{

if(clockthread == null){

clockthread = new thread(this);

}

clockthread.start();

}

public void stop() //停止显示

{

clockthread = null;

}

private void drawhands(graphics g)

{

double angle;

int i,j;

int x,y;

angle=minsec * lastsecond; //根据lastsecond算出秒针相对于12点刻度的角度

sweep.draw(facecolor, angle, g);//画出秒针

//如果时与分改变则重绘时针分针

if(cur_time.getminutes() != lastminute){

minutehand.draw(facecolor,minsec*lastminute,g);

if(cur_time.get_hours() != lasthour)

hourhand.draw(facecolor,hour*lasthour,g);

}

g.setcolor(textcolor);

g.fillrect(originx+size(12),y1-size(2),size(10),size(4)); //绘制左侧横条

g.fillrect(x1-size(2),originy + mindimension-size(22),size(4),size(10)); //绘制下面的横条

g.fillpolygon( xpoints, ypoints, 3); //绘制顶部的三角形

for(i=1;i<12;i+=3) //在表盘的2、3、5、6、8、9、11时针刻度区绘制圆

for(j=i;j

x=(int)(x1+math.sin(hour*j)*size(35));

y=(int)(y1-math.cos(hour*j)*size(35));

g.filloval(x-size(3),y-size(3),size(6),size(6));

}

//设置字体

g.setfont(font);

fontmetrics fm=g.getfontmetrics();

//画顶部的logo字符串

g.drawstring(logostring,x1-fm.stringwidth(logostring)/2,y1-size(12));

//得到日期

string day=integer.tostring(cur_time.getdate(),10);

//将日期绘制在表盘右侧

g.drawstring( day,

originx + mindimension-size(14)-fm.stringwidth(day),

y1+size(5));

//外面画上框子

g.drawrect( originx + mindimension-size(14)-fm.stringwidth(day)-size(2),

y1-size(5)-size(2),

fm.stringwidth(day)+size(4),

size(10)+size(4));

if(images[logo] != null){ //如果logo图片存在,在底部画出来

x = originx + (mindimension-images[logo].getwidth(this))/2;

y = y1 + (mindimension/2 – size(22) – images[logo].getheight(this))/2;

if(x > 0 && y > 0)

offscrgc.drawimage(images[logo], x, y, this);

}

lasthour=cur_time.get_hours();

hourhand.draw(hourcolor,hour*lasthour,g); //画时针

lastminute=cur_time.getminutes();

minutehand.draw(minutecolor,minsec*lastminute,g); //画分针

g.setcolor(minutecolor); //绘制分针尾部的圆形

g.filloval(x1-size(4),y1-size(4),size(8),size(8));

g.setcolor(sweepcolor); //绘制秒针尾部的圆形

g.filloval(x1-size(3),y1-size(3),size(6),size(6));

lastsecond=cur_time.getseconds(); //得到新的秒数,重画

angle=minsec*lastsecond;

sweep.draw(sweepcolor, angle,g);

g.setcolor(trimcolor);

g.filloval(x1-size(1),y1-size(1),size(2),size(2)); //秒针尾部圆心部分应该是螺丝,挖空处理^_^

}

private color parsecolorstring(string colorstring) //参数传入为字符串形(规定为16进制6位字符串)

{

if(colorstring.length()==6){

int r = integer.valueof(colorstring.substring(0,2),16).intvalue(); //前两位为r值

int g = integer.valueof(colorstring.substring(2,4),16).intvalue(); //中间为g值

int b = integer.valueof(colorstring.substring(4,6),16).intvalue(); //最后为b值

return new color(r,g,b); //得到color

}

else return color.lightgray; //字符串不符合规范,默认为灰色

}

public void run()

{

repaint(); //每次启动时首先重绘一次

//每隔500ms获得现在时间并重绘一次

while(null != clockthread){

cur_time= (localonly)? new hms() :new hms(tzdifference);

repaint();

try{

thread.sleep(500);

} catch (interruptedexception e) {}

}

}

public void paint(graphics g) //首先绘制缓冲区内图片,再显示出来

{

int i,x0,y0,x2,y2;

//如果没有提供背景图片,则用bgcolor绘制背景

if(images[background] == null){

offscrgc.setcolor(bgcolor);

offscrgc.fillrect(0,0,width,height);

}

else //否则直接使用背景图片

offscrgc.drawimage(images[background], 0, 0, this);

//绘制外框到表盘间的部分

offscrgc.setcolor(casecolor);

//将园形的范围适量缩减(不充满整个区域),防止有些地方被截取

offscrgc.filloval( originx+1,

originy+1,

mindimension-2,

mindimension-2);

//绘制表盘

offscrgc.setcolor(facecolor);

offscrgc.filloval( originx + size(5),

originy + size(5),

mindimension – size(10),

mindimension – size(10));

//绘制外框线

offscrgc.setcolor(trimcolor);

offscrgc.drawoval( originx+1,

originy+1,

mindimension-2,

mindimension-2);

//绘制内框线

offscrgc.drawoval( originx + size(5),

originy + size(5),

mindimension – size(10),

mindimension – size(10));

offscrgc.setcolor(textcolor);

//画刻度,一共有60个刻度,x0、y0为刻度起始的位置,x1、y1圆心位置,x2、y2为刻度终止位置(x0

for(i=0;i<60;i++){

if(i==0 || (i>=5 && i%5 == 0)){ //每5格绘制一条长线(相对圆心)

x0=(int)(x1+size(40)*math.sin(minsec*i));

y0=(int)(y1+size(40)*math.cos(minsec*i));

}

else{ //其他部分绘制短线

x0=(int)(x1+size(42)*math.sin(minsec*i));

y0=(int)(y1+size(42)*math.cos(minsec*i));

}

x2=(int)(x1+size(44)*math.sin(minsec*i));

y2=(int)(y1+size(44)*math.cos(minsec*i));

offscrgc.drawline(x0,y0,x2,y2);

}

drawhands(offscrgc); //绘制指针

g.drawimage(offscrimage,0,0,this); //把生成的缓冲区图形绘制到页面上

ispainted=true; //使下次update时不绘制表盘

}

public synchronized void update(graphics g)

{

if(!ispainted) //绘制表盘

paint(g);

else{ //已经绘制了表盘,只用绘制指针,首先在缓冲区内绘制,然后显示出来

drawhands(offscrgc);

g.drawimage(offscrimage,0,0,this);

}

}

}

一个测试用的html的例子:

我的时钟程序

显示东8区的时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值