?? applet1.java~433~
字號:
package hourse;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.lang.*;
import java.util.*;
import java.text.*;
class snow{
int x;
int y;
int size;
Random random = new Random();
void initiate(snow snowball)
{ snowball.x=Math.abs(random.nextInt()) % 800-200;
snowball.y=Math.abs(random.nextInt()) % 1000-1000;
snowball.size=1+Math.abs(random.nextInt()) %4;
}
void nextlocation(snow snowball,int mousex)
{
if(mousex>=250)
snowball.x=snowball.x+(Math.abs(random.nextInt())%2)+1;
if(mousex<250&&mousex>=150)
snowball.x=snowball.x+(Math.abs(random.nextInt())%2)-1;
if(mousex<150)
snowball.x=snowball.x+(Math.abs(random.nextInt())%2)-2;
snowball.y= snowball.y+1;
if(snowball.y>340){snowball.x=Math.abs(random.nextInt()) % 800-200;snowball.y=Math.abs(random.nextInt()) % 400-400;
}
}
}
public class Applet1 extends Applet implements Runnable{
private boolean isStandalone = false;
private Thread animate= null ;
Image offScreenImage,pic,bike,fish;
int x1=398,x2;
Font f; String day="";
int sec,min,hour,year;String strsec,strmin,strhour,stryear;
Color color; int a=255,b=255,c=0;
int direct=0;
int y0=260;
int xcenter=195,ycenter=100,radius=90;
int lastxsec=0,lastysec=0,lastxmin=0,lastymin=0,lastxhour=0,lastyhour=0;
AudioClip sound;
int count=0;
Random random = new Random();
snow[] snowball=new snow[200];//新建200個雪球
snow snowtemp=new snow();
int mousex=0;
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
public Applet1() {
}
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
pic=getImage( getCodeBase(), "pic.jpg");
bike=getImage( getCodeBase(), "bike.gif");
fish=getImage( getCodeBase(), "fish.gif");
sound=getAudioClip( getCodeBase(), "drip.au");
for(int i=0;i<200;i++)
{snowball[i]=new snow();
snowtemp.initiate(snowball[i]);
}
if(animate == null || !animate.isAlive())//創建一個線程
{ animate = new Thread(this);
animate.start();}
}
private void jbInit() throws Exception {
this.addMouseMotionListener(new Applet1_this_mouseMotionAdapter(this));
this.addKeyListener(new Applet1_this_keyAdapter(this));
this.addMouseListener(new Applet1_this_mouseAdapter(this));
}
public String getAppletInfo() {
return "Applet Information";
}
public String[][] getParameterInfo() {
return null;
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
int xhour,yhour,xmin,ymin,xsec,ysec,sec,min,hour;
String strsec,strmin,strhour;
Font f;
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);//抗鋸齒
Graphics offScreen;//通過雙緩存消除閃爍
offScreenImage=createImage(500,400);//創建屏外繪圖區
offScreen=offScreenImage.getGraphics();//取得繪圖環境
offScreen.drawImage(pic,0,0,this);
color = new Color(255, 255, 255);
offScreen.setColor(color);
for(int i=0;i<200;i++)
{snowtemp.nextlocation(snowball[i],mousex);
offScreen.fillOval(snowball[i].x,snowball[i].y,snowball[i].size,snowball[i].size);}
color = new Color(255, 255, 255);
offScreen.setColor(color);
for(int j=0;j<60;j++)
{xmin=(int)(Math.cos(j*3.14f/30-3.14f/2)*radius+xcenter);
ymin=(int)(Math.sin(j*3.14f/30-3.14f/2)*radius+ycenter);
offScreen.fill3DRect(xmin-1,ymin-1,3,3,true);}
color = new Color(88, 120, 214);
offScreen.setColor(color);
for(int i=0;i<12;i++)
{xhour=(int)(Math.cos((i*30)*3.14f/180-3.14f/2)*radius+xcenter);
yhour=(int)(Math.sin((i*30)*3.14f/180-3.14f/2)*radius+ycenter);
offScreen.fill3DRect(xhour-2,yhour-2,7,7,true);}
Date date=new Date();
sec=date.getSeconds();
min=date.getMinutes();
hour=date.getHours();
xsec=(int)(Math.cos(sec*3.14f/30-3.14f/2)*(radius-5)+xcenter);
ysec=(int)(Math.sin(sec*3.14f/30-3.14f/2)*(radius-5)+ycenter);
xmin=(int)(Math.cos(min*3.14f/30-3.14f/2)*(radius-15)+xcenter);
ymin=(int)(Math.sin(min*3.14f/30-3.14f/2)*(radius-15)+ycenter);
xhour=(int)(Math.cos((hour*30+min/2)*3.14f/180-3.14f/2)*(radius-40)+xcenter);
yhour=(int)(Math.sin((hour*30+min/2)*3.14f/180-3.14f/2)*(radius-40)+ycenter);
double n,hxt,hyt,hxl,hyl,hxr,hyr,hxbl,hybl,hxbr,hybr;
n=Math.atan2((yhour-ycenter),(xhour-xcenter));
hxt=50;hyt=0;
hxl=1;hyl=-8.5;
hxbl=-7;hybl=-4;
hxbr=-7;hybr=4;
hxr=1;hyr=8.5;
Polygon filledPolygon=new Polygon();
filledPolygon.addPoint(
(int)((hxt*Math.cos(n))-(hyt*Math.sin(n)))+xcenter,
(int)((hxt*Math.sin(n))+(hyt*Math.cos(n)))+ycenter
);
filledPolygon.addPoint(
(int)((hxl*Math.cos(n))-(hyl*Math.sin(n)))+xcenter,
(int)((hxl*Math.sin(n))+(hyl*Math.cos(n)))+ycenter
);
filledPolygon.addPoint(
(int)((hxbl*Math.cos(n))-(hybl*Math.sin(n)))+xcenter,
(int)((hxbl*Math.sin(n))+(hybl*Math.cos(n)))+ycenter
);
filledPolygon.addPoint(
(int)((hxbr*Math.cos(n))-(hybr*Math.sin(n)))+xcenter,
(int)((hxbr*Math.sin(n))+(hybr*Math.cos(n)))+ycenter
);
filledPolygon.addPoint(
(int)((hxr*Math.cos(n))-(hyr*Math.sin(n)))+xcenter,
(int)((hxr*Math.sin(n))+(hyr*Math.cos(n)))+ycenter
);
color = new Color(12, 60,139);
offScreen.setColor(color);
offScreen.fillPolygon(filledPolygon);
color = new Color(0,0,0);
offScreen.setColor(color);
offScreen.drawPolygon(filledPolygon);//畫時針
double m,mxt,myt,mxl,myl,mxr,myr,mxbl,mybl,mxbr,mybr;
m=Math.atan2((ymin-ycenter),(xmin-xcenter));
mxt=75;myt=0;
mxl=8;myl=-6;
mxbl=-8;mybl=-3;
mxbr=-8;mybr=3;
mxr=8;myr=6;
Polygon minPolygon=new Polygon();
minPolygon.addPoint(
(int)((mxt*Math.cos(m))-(myt*Math.sin(m)))+xcenter,
(int)((mxt*Math.sin(m))+(myt*Math.cos(m)))+ycenter
);
minPolygon.addPoint(
(int)((mxl*Math.cos(m))-(myl*Math.sin(m)))+xcenter,
(int)((mxl*Math.sin(m))+(myl*Math.cos(m)))+ycenter
);
minPolygon.addPoint(
(int)((mxbl*Math.cos(m))-(mybl*Math.sin(m)))+xcenter,
(int)((mxbl*Math.sin(m))+(mybl*Math.cos(m)))+ycenter
);
minPolygon.addPoint(
(int)((mxbr*Math.cos(m))-(mybr*Math.sin(m)))+xcenter,
(int)((mxbr*Math.sin(m))+(mybr*Math.cos(m)))+ycenter
);
minPolygon.addPoint(
(int)((mxr*Math.cos(m))-(myr*Math.sin(m)))+xcenter,
(int)((mxr*Math.sin(m))+(myr*Math.cos(m)))+ycenter
);
color = new Color(0, 128,255);
offScreen.setColor(color);
offScreen.fillPolygon(minPolygon);
color = new Color(0, 81,162);
offScreen.setColor(color);
offScreen.drawPolygon(minPolygon); //畫分針
color = new Color(255, 0, 0);
offScreen.setColor(color);
offScreen.fillOval(xcenter-2,ycenter-2,4,4);
offScreen.drawLine(xcenter,ycenter,xsec,ysec);//畫秒針
color = new Color(a, b,c);
sec=date.getSeconds();
min=date.getMinutes();
hour=date.getHours();
year= date.getYear()-100 ;
if(year>=0)stryear="200"+year;
else stryear="19"+(100+year);
if(hour<10)strhour="0"+hour;
else strhour=""+hour;
if(min<10)strmin="0"+min;
else strmin=""+min;
if(sec<10)strsec="0"+sec;
else strsec=""+sec;
switch(date.getDay())
{case 0:day="日";break;
case 1:day="一";break;
case 2:day="二";break;
case 3:day="三";break;
case 4:day="四";break;
case 5:day="五";break;
case 6:day="六";break;
}
f=new Font("星期"+day+" "+(date.getMonth()+1)+"月"+date.getDate()+"日"+" "+stryear+"年 "+strhour+":"+strmin+":"+strsec,Font.BOLD,22);
offScreen.setFont(f);offScreen.setColor(color);
if(direct==0)
{ {if(x1<0){offScreen.drawImage(fish,x1-15,y0-55,this);
offScreen.drawString("星期"+day+" "+(date.getMonth()+1)+"月"+date.getDate()+"日"+" "+stryear+"年 "+strhour+":"+strmin+":"+strsec,x1,y0);
x2=x1+398;
offScreen.drawImage(fish,x2-15,y0-55,this);
offScreen.drawString("星期"+day+" "+(date.getMonth()+1)+"月"+date.getDate()+"日"+" "+stryear+"年 "+strhour+":"+strmin+":"+strsec,x2,y0);
}
else
{ offScreen.drawImage(fish,x1-15,y0-55,this);
offScreen.drawString("星期"+day+" "+(date.getMonth()+1)+"月"+date.getDate()+"日"+" "+stryear+"年 "+strhour+":"+strmin+":"+strsec,x1,y0);}
if(x2<0){x1=x2;x2=x1+398;}
x1=x1-1;
}}
if(direct==1)
{ {if(x1+398>398){offScreen.drawImage(bike,x1+100,y0+20,this);
offScreen.drawString("星期"+day+" "+(date.getMonth()+1)+"月"+date.getDate()+"日"+" "+stryear+"年 "+strhour+":"+strmin+":"+strsec,x1,y0);
x2=x1-398;
offScreen.drawImage(bike,x2+100,y0+20,this);
offScreen.drawString("星期"+day+" "+(date.getMonth()+1)+"月"+date.getDate()+"日"+" "+stryear+"年 "+strhour+":"+strmin+":"+strsec,x2,y0);
}
else
{ offScreen.drawImage(bike,x1+100,y0+20,this);
offScreen.drawString("星期"+day+" "+(date.getMonth()+1)+"月"+date.getDate()+"日"+" "+stryear+"年 "+strhour+":"+strmin+":"+strsec,x1,y0);}
if(x2+398>398){x1=x2;x2=x1-398;}
x1=x1+1;}
}
g2d.drawImage(offScreenImage,0,0,this);
}
public void run()
{ while(animate!=null)
{try{Thread.sleep(50);}
catch(InterruptedException e){}
repaint();}
}
void this_mouseClicked(MouseEvent e) {
int x,y;
y=e.getY();x=e.getX();
if(y>0&&y<=195)
{a=Math.abs(random.nextInt()) % 256;
b=Math.abs(random.nextInt()) % 256;
c=Math.abs(random.nextInt()) % 256;}
if(y>195)
direct=1-direct;}
void this_keyPressed(KeyEvent e) {
int j;
j = e.getKeyCode();
if(j==e.VK_SPACE){a=255;b=255;c=0;direct=0;}
if(j==e.VK_LEFT){direct=0;}
if(j==e.VK_RIGHT){direct=1;}
if(j==e.VK_NUMPAD1){a=255;b=0;c=0;}
if(j==e.VK_NUMPAD2){a=0;b=0;c=255;}
if(j==e.VK_NUMPAD3){a=0;b=204;c=0;}
if(j==e.VK_NUMPAD9){a=255;b=255;c=0;}
if(j==e.VK_NUMPAD5){a=255;b=128;c=255;}
if(j==e.VK_NUMPAD6){a=255;b=128;c=64;}
if(j==e.VK_NUMPAD7){a=192;b=192;c=192;}
if(j==e.VK_NUMPAD8){a=0;b=0;c=0;}
if(j==e.VK_NUMPAD4){a=255;b=255;c=255;}
}
void this_mouseMoved(MouseEvent e) {
mousex=e.getX();}
}
class Applet1_this_mouseAdapter extends java.awt.event.MouseAdapter {
Applet1 adaptee;
Applet1_this_mouseAdapter(Applet1 adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.this_mouseClicked(e);
}
}
class Applet1_this_keyAdapter extends java.awt.event.KeyAdapter {
Applet1 adaptee;
Applet1_this_keyAdapter(Applet1 adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.this_keyPressed(e);
}
}
class Applet1_this_mouseMotionAdapter extends java.awt.event.MouseMotionAdapter {
Applet1 adaptee;
Applet1_this_mouseMotionAdapter(Applet1 adaptee) {
this.adaptee = adaptee;
}
public void mouseMoved(MouseEvent e) {
adaptee.this_mouseMoved(e);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -