清华主楼
雪花飘飘 1969.1.1.拍照
//类文件中嵌入图像 *.JPG
<applet
code=xhpp1.class
name=Applet1
width=571
height=393 >
<param name="graphic" value="QHXJ.jpg"> <param name=label value="This string was passed from the HTML host."> <param name=background value="008080"> <param name=foreground value="FFFFFF"> </applet>
//雪花飘飘1 xhpp1.java
import java.applet.Applet;
import java.awt.*;
import java.util.Random; //随机函数
public class xhpp1 extends Applet
implements Runnable //线程可运行状态
{
//变量声明
Thread mainThread; //主线程
Image offScreen; //屏幕填充区域
Image gAlc[]; //获取图像
Random rand; //随机函数
int stopFlag;
int snows; //雪花数量
int wind; //风速
int threadSleep; //线程休眠时间 ms
int snowSize; //雪花尺寸
int snowX[]; //雪花 X 坐标
int snowY[]; //雪花 Y 坐标
long stopTime;
Dimension dim; //屏幕面积
MediaTracker mt; //图片队列
//构造函数
public xhpp1()
{
stopTime = 0L;
}
//得到一个字符串和一个整数类型的参数
int getParameter(String s, int i)
{
String s1 = getParameter(s);
return s1 == null ? i : Integer.parseInt(s1);
}
//得到一个字符串和三个整数类型的参数
int getParameter(String s, int i, int j, int k)
{
String s1 = getParameter(s);
if(s1 != null)
{
if((i = Integer.parseInt(s1)) > j)
return j;
if(i < k)
return k;
else
return i;
} else
{
return i;
}
}
//得到两个字符串的参数
String getParameter(String s, String s1)
{
String s2 = getParameter(s);
return s2 == null ? s1 : s2;
}
//初始化小程序
public void init()
{
rand = new Random();
dim = getSize();
//屏幕尺寸
offScreen = createImage(dim.width, dim.height);
//雪花数量:10—100个
snows = getParameter("snows", 100, 500, 0);
//雪花尺寸:3—15
snowSize = getParameter("snowSize", 8, 50, 13);
//雪花速度:线程休眠时间 10—100ms
threadSleep = getParameter("threadsleep", 50, 1000, 10);
snowX = new int[snows];
snowY = new int[snows];
//产生雪花随机坐标点(X,Y)
for(int i = 0; i < snows; i++)
{
snowX[i] = rand.nextInt() % (dim.width / 2) + dim.width / 2;
snowY[i] = rand.nextInt() % (dim.height / 2) + dim.height / 2;
}
mt = new MediaTracker(this); //图片队列
gAlc = new Image[1];
//取得图片的路径和名称
gAlc[0] = getImage(getDocumentBase(), getParameter("graphic", "test.gif"));
mt.addImage(gAlc[0], 0);
try
{
mt.waitForID(0);
}
catch(InterruptedException interruptedexception)
{
return;
}
stopFlag = 0;
}
//启动小程序
public void start()
{
if(mainThread == null)
{
mainThread = new Thread(this);
mainThread.start();
}
}
//停止小程序
public void stop()
{
mainThread = null;
}
//运行小程序
public void run()
{
while(mainThread != null)
{
try
{
Thread.sleep(threadSleep);
}
catch(InterruptedException interruptedexception)
{
return;
}
repaint();
}
}
//生成雪花
public void drawBackSnow(Graphics g)
{
//设置雪花颜色:红、绿、黄、蓝、白色
g.setColor(Color.white);
for(int i = 0; i < snows; i++)
{
//画圆形填充雪花 fillOval,矩形填充方块 fillRect
g.fillOval(snowX[i], snowY[i], snowSize, snowSize);
snowX[i] += rand.nextInt() % 2 + wind;
snowY[i] += (rand.nextInt() % 6 + 5) / 5 + 1;
//控制雪花的左右界
if(snowX[i] >= dim.width)
snowX[i] = 0;
if(snowX[i] < 0)
snowX[i] = dim.width - 1;
//控制雪花的上下界
if(snowY[i] >= dim.height || snowY[i] < 0)
{
snowX[i] = Math.abs(rand.nextInt() % dim.width);
snowY[i] = 0;
}
}
wind = rand.nextInt() % 5 - 2;
}
//画屏函数
public void paint(Graphics g)
{
//设置屏幕背景黑色
offScreen.getGraphics().setColor(Color.black);
//填充矩形屏幕 fillRect,填充圆形屏幕 fillOval
offScreen.getGraphics().fillRect(0,0,dim.width,dim.height);
//设置图像左上角坐标:(50,50)或(dim.width/10,dim.height/10)
offScreen.getGraphics().drawImage(gAlc[0],50,50,this);
//绘制雪花
drawBackSnow(offScreen.getGraphics());
g.drawImage(offScreen, 0, 0, null);
}
//更新屏幕函数
public void update(Graphics g)
{
paint(g);
}
}