|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
在性能方面,在windows平台下,.net可能是占强项,要是把.net放在sun开发的操作系统上去运行呢?根本就运行不了,.net对其它操作系统的支持也很弱,性能也可能比不上java。程序接着下面的部分,上面是APPLET主程序部分:
publicclassMyClockextendsAppletimplementsRunnable
{
staticfinalintBACKGROUND=0;//背景图片的序号,供数组利用
staticfinalintLOGO=1;//LOGO图片的序号,供数组利用
staticfinalStringJAVEX="bear";//表盘上显现的笔墨
staticfinaldoubleMINSEC=0.104719755;//分针和秒针在表盘上的刻度(60个)间的弧度
staticfinaldoubleHOUR=0.523598776;//时针在表盘上的刻度(24个)间的弧度
ThreadclockThread=null;//利用多线程机制,用另外一个线程不休显现图片
//供应的默许参数,假如HTML文件内里没有给参数就利用
intwidth=100;
intheight=100;
ColorbgColor=newColor(0,0,0);
ColorfaceColor=newColor(0,0,0);
ColorsweepColor=newColor(255,0,0);
ColorminuteColor=newColor(192,192,192);
ColorhourColor=newColor(255,255,255);
ColortextColor=newColor(255,255,255);
ColorcaseColor=newColor(0,0,0);
ColortrimColor=newColor(192,192,192);
StringlogoString=null;
Imageimages[]=newImage[2];//背景和LOGO的图片
booleanisPainted=false;//假如第一次载进时绘制背景及表盘,其他时分重绘则只绘制指针
//时钟圆心的地位
intx1,y1;
//最下面谁人三角形的刻度图形的坐标
intxPoints[]=newint[3],yPoints[]=newint[3];
//保留以后工夫,转换成(double)(hours+minutes/60)
Hmscur_time;
//秒针、分针、时针
SweepHandsweep;
HmHandminuteHand,
hourHand;
//用于绘制的时、分、秒
doublelastHour;
intlastMinute,lastSecond;
//显现日期和表盘上的字母的字体
Fontfont;
//图片显现利用了缓冲机制,offScrImage和offScrGC寄存缓冲区内图片的信息
ImageoffScrImage;
GraphicsoffScrGC;
//用于测试背景图片和LOGO图片
MediaTrackertracker;
intminDimension;//假如背景地区不是正方形的话,包管时钟在两头显现
intoriginX;//时钟图形地点正方形地区的左上角X坐标
intoriginY;//时钟图形地点正方形地区的左上角Y坐标
doubletzDifference=0;//时区间的差,向西为正数,向东为负数
booleanlocalOnly=false;//是不是只利用当地工夫,假如为FALSE则能够依据传进的时区显现该时区工夫
//保留参数的范例申明
publicString[][]getParameterInfo()
{
String[][]info={
{"width","int","APPLET的长度,以象素为单元"},
{"height","int","APPLET的宽度,以象素为单元"},
{"bgColor","string","背景色彩,e.gFF0000"},
{"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","是不是思索到时区不同"}
};
returninfo;
}
//显现信息
publicStringgetAppletInfo()
{
return"版权一切,COPY必究,回护正版,大家有责";
}
voidshowURLerror(Exceptione)
{
StringerrorMsg="URL毛病:"+e;
showStatus(errorMsg);
System.err.println(errorMsg);
}
//相称于把时钟酿成100×100的巨细,percent即绝对坐标
privateintsize(intpercent)
{
return(int)((double)percent/100.0*(double)minDimension);
}
publicvoidinit()
{
URLimagesURL[]=newURL[2];
StringszImagesURL[]=newString[2];
tracker=newMediaTracker(this);
//失掉HTML页面供应的参数,并把它转换响应的格局
StringparamString=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;
elseif(logoString.length()>8)
logoString=logoString.substring(0,8);//限定8个字母,不然显现不下!
//szImagesURL数组依据HTML传进参数寄存图片的文件名
szImagesURL[BACKGROUND]=getParameter("BGIMAGEURL");
szImagesURL[LOGO]=getParameter("LOGOIMAGEURL");
//测试图片
for(inti=0;i<2;i++){
if(szImagesURL[i]!=null){
//依据CodeBase参数与Image文件名失掉image的路径,测试是不是存在,假如不存在就不必图片
try{
imagesURL[i]=newURL(getCodeBase(),szImagesURL[i]);
}catch(MalformedURLExceptione)
{
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(InterruptedExceptione){}
}
elseimages[i]=null;
}
//失掉响应时区的工夫
cur_time=(localOnly)?newHms():newHms(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=newSweepHand(x1,y1,size(40),3);
minuteHand=newHmHand(x1,y1,size(40),size(6),6);
hourHand=newHmHand(x1,y1,size(25),size(8),6);
//机关字体
font=newFont("TXT",Font.BOLD,size(10));
//机关缓冲区内图形
offScrImage=createImage(width,height);
offScrGC=offScrImage.getGraphics();
System.out.println(getAppletInfo());
}
publicvoidstart()//入手下手启动显现线程
{
if(clockThread==null){
clockThread=newThread(this);
}
clockThread.start();
}
publicvoidstop()//中断显现
{
clockThread=null;
}
privatevoiddrawHands(Graphicsg)
{
doubleangle;
inti,j;
intx,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<i+2;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);
FontMetricsfm=g.getFontMetrics();
//画顶部的LOGO字符串
g.drawString(logoString,x1-fm.stringWidth(logoString)/2,y1-size(12));
//失掉日期
Stringday=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));//秒针尾部圆心部分应当是螺丝,挖空处置^_^
}
privateColorparseColorString(StringcolorString)//参数传进为字符串形(划定为16进制6位字符串)
{
if(colorString.length()==6){
intR=Integer.valueOf(colorString.substring(0,2),16).intValue();//前两位为R值
intG=Integer.valueOf(colorString.substring(2,4),16).intValue();//两头为G值
intB=Integer.valueOf(colorString.substring(4,6),16).intValue();//最初为B值
returnnewColor(R,G,B);//失掉COLOR
}
elsereturnColor.lightGray;//字符串不切合标准,默许为灰色
}
publicvoidrun()
{
repaint();//每次启动时起首重绘一次
//每隔500ms取得如今工夫偏重绘一次
while(null!=clockThread){
cur_time=(localOnly)?newHms():newHms(tzDifference);
repaint();
try{
Thread.sleep(500);
}catch(InterruptedExceptione){}
}
}
publicvoidpaint(Graphicsg)//起首绘制缓冲区内图片,再显现出来
{
inti,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<x2,y0<y2)
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时不绘制表盘
}
publicsynchronizedvoidupdate(Graphicsg)
{
if(!isPainted)//绘制表盘
paint(g);
else{//已绘制了表盘,只用绘制指针,起首在缓冲区内绘制,然后显现出来
drawHands(offScrGC);
g.drawImage(offScrImage,0,0,this);
}
}
}
一个测试用的HTML的例子:
<HTML>
<TITLE>我的时钟程序</TITLE>
<BODY>
<h2>显现东8区的工夫</h2>
<p>
<appletcode="MyClock.class"width="300"height="300">
<paramname="BGCOLOR"value="FFFFFF">
<paramname="FACECOLOR"value="FFFFFF">
<paramname="SWEEPCOLOR"value="FF0000">
<paramname="MINUTECOLOR"value="008080">
<paramname="HOURCOLOR"value="000080">
<paramname="TEXTCOLOR"value="000000">
<paramname="CASECOLOR"value="000080">
<paramname="TRIMCOLOR"value="C0C0C0">
<paramname="LOGOIMAGEURL"value="java.gif">
<paramname="TIMEZONE"value="8">
</p>
</BODY>
</HTML>
用java开发web只要两本书:一本是关于java基础的,一本是关于jsp、servlet的就可以了。开发周期长,我就来讲句题外话,现在有很多思想都是通过java来展现。 |
|