JAVA范例 - Applet小应用程序

Applet小应用程序

实例337 不断变大的文字
 

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;

public class WordsToLarge extends Applet implements Runnable {
private Image myImage;// 定义图片对象
private Graphics myGraphic;// 定义Graphics对象
private Font font;// 定义Font对象
private String myString;// 定义一个字符串对象
private Thread mythread;// 定义一个Thread对象
private int fontSize;// 定义一个int型变量
public void init() {// Applet小程序初始化
this.setSize(400, 400);
myImage = createImage(400, 400); // /创建Image图像对像
myGraphic = myImage.getGraphics();
myString = "Applet Java小程序";// 需要变化的文本内容
font = new Font("TimesRoman", Font.BOLD, 8);
}
public void start() {// 启动多线程
if (mythread == null) {
mythread = new Thread(this);
mythread.start();// 启动线程
}
}
public void update(Graphics g) {// 重新调用paint方法
paint(g);
}
public void paint(Graphics g) {// 绘图像
myGraphic.setColor(Color.black);
myGraphic.fillRect(0, 0, getSize().width, getSize().height);
font = new Font("TimesRoman", Font.BOLD, fontSize);
myGraphic.setFont(font);
myGraphic.setColor(Color.white);
FontMetrics fm = myGraphic.getFontMetrics(font);
int fontHeight = fm.getHeight();
int w;
int baseLine = getSize().height / 2 + fontHeight / 2;
w = fm.stringWidth(myString);
w = (getSize().width - w) / 2;
myGraphic.drawString(myString, w, baseLine -= 20);
g.drawImage(myImage, 0, 0, this);
fontSize++;
}
public void run() {// 重写Thread类的run方法
while (true) {
repaint();
if (fontSize > getSize().height)
fontSize = 0;
try {
mythread.sleep(100);
} catch (InterruptedException e) {
}
}
}
}


实例338 灯光扫描的效果

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

public class LightScan extends Applet implements Runnable {
private String myText;// 声明文本对象
private Font font;// 声明字体对象
private int fontSize;// 声明字体的大小
private Thread mythread;// 声明线程对象
private int spotPosition = 100;// 声明光标点出现的位置
private int myTextSize = 20;// 文本的大小
private int myTextWidth = 0;// 文本的宽度
private int fontHeight, baseLine, w;
public void init()// 小程充的初始化
{
myText = "Applet小程序";// 在小程序中需要显示的文本对像
fontSize = 30;// 设置字体的大小
font = new Font("TimesRoman", Font.BOLD, fontSize); // 创建Font对像
FontMetrics fm = getFontMetrics(font);// 获取FontMetrics字体规格对象。
fontHeight = fm.getHeight();
baseLine = getSize().height / 2 + fontHeight / 3;
myTextWidth = fm.stringWidth(myText);
w = fm.stringWidth(myText);
w = (getSize().width - w) / 2;
spotPosition = w;
setBackground(Color.black); // 设置小程序的背景色为黑色
}
public void start() {
if (mythread == null) {
mythread = new Thread(this);// 创建多线程
mythread.start();// 启动多线程
}
}
public void stop() {
mythread.stop();// 停止运行线程
mythread = null;
}
public void run()// 运行线程
{
while (true) {
repaint();// 重绘此组件
try {
mythread.sleep(30);
}// 线程休眠
catch (InterruptedException e) {
}
}
}
public void update(Graphics g)// 更新组件
{
paint(g);
}
// 利用clipRect()方法,每次调用显示方法paint()时,
// 先用红色的笔画一遍文字,再用白色在裁剪区中画一遍文字
public void paint(Graphics g) {
g.setFont(font);
g.setColor(Color.red);
g.drawString(myText, w, baseLine); // 第一遍显示
g.clipRect(spotPosition, 0, myTextSize, getSize().height); // 设置裁剪区域
g.setColor(Color.white);
g.drawString(myText, w, baseLine); // 第二遍显示
spotPosition = (spotPosition + 1) % (myTextWidth + 100); // 移动光标位置
}
}

实例339 字体逐渐展开的效果

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;

public class WordsToExpand extends Applet implements Runnable {
private Image myImage;//声明Image图象对象
private Graphics myGraphic;//声明Graphics对象
private int width = 0, height = 0;//声明高和宽变量
private String words;//定义文本内容
private Thread mythread;//声明Thread多线程对象
private int xPosition = 0, yPosition = 0, myHeight;//定义x和y坐标点
private int times = 0;//表示线条出现的方向
private Font font;
public void init()//初始化Applet小程序
{
this.setSize(300, 200);
font = new Font("TimesRoman", Font.BOLD, 30);
width = 300;
height = 200;
myHeight = height / 3;
yPosition = myHeight;
words = "大家好";
myImage = createImage(width, height);
myGraphic = myImage.getGraphics();
}
public void start()//起动Applet小程序和多线程
{
if (mythread == null) {
mythread = new Thread(this);
mythread.start();
}
}
public void update(Graphics g)//更新组件
{
paint(g);
}
public void paint(Graphics g)//绘制组件
{
g.drawImage(myImage, 0, yPosition, width, myHeight, this);
}
public void run()//运行多线程
{
try {
while (true) {
yPosition = 0;
myHeight = height;
myGraphic.setColor(Color.pink);
myGraphic.fillRect(0, 0, width, height);
repaint();
mythread.sleep(100);
if (times == 0) {
myGraphic.setColor(Color.black);
for (int i = width; i >= 0; i--) {
myGraphic.fillRect(i, height / 3, width, height / 10);
repaint();
mythread.sleep(10);
}
} else if (times == 1) {
myGraphic.setColor(Color.blue);
for (int i = 0; i <= width; i++) {
myGraphic.fillRect(0, height / 3, i, height / 10);
repaint();
mythread.sleep(10);
}
}
yPosition = height / 3;
myHeight = height / 3;
for (int i = height / 3; i >= 0; i--) {
xPosition = 0;
yPosition--;
myHeight = myHeight + 2;
if (times == 0)//0表示从右向左移动
{
myGraphic.setColor(Color.black);
myGraphic.fillRect(0, 0, width, height);
myGraphic.setFont(font);
myGraphic.setColor(Color.yellow);
myGraphic.drawString(words, 10, 35);
times++;
} else if (times == 1)//1表示从左向右移动
{
myGraphic.setColor(Color.red);
myGraphic.fillRect(0, 0, width, height);
myGraphic.setFont(font);
myGraphic.setColor(Color.black);
myGraphic.drawString(words, 10, 35);
times = 0;
}
repaint();
mythread.sleep(100);
}
mythread.sleep(2500);
System.out.println(times);
}
} catch (InterruptedException e) {
}
}
}


实例340 飞舞的气球

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class FlyBall extends Applet implements Runnable {
Thread artist = null;
int bubble = 0, thisbubble = 0; // 气球数量,当前气球编号
int MAXBUBBLES = 25; // 最大气球数量
int stepper = 4; // Counter for which bubbles to move when
int record[][] = new int[MAXBUBBLES][5]; // 记录气球的二维数组
public void init() { // 初始化applet
resize(400, 400); // 设定applet尺寸
}
public void draw_bubble(int x, int y, int r, Color col, Graphics g) {
// 输出气球
int i;
// 输出一个圆形
for (i = x - r; i <= x + r; i++) {
g.setColor(col);
g.drawLine(i, y - (int) (Math.sqrt(r * r - ((i - x) * (i - x)))),
i, y + (int) (Math.sqrt(r * r - ((i - x) * (i - x)))));
}
}
public void move_bubble(int x, int y, int r, Color col, int step, Graphics g) {
// 移动气球
int i;
// 输出气球的上半部分
for (i = x - r; i <= x + r; i++) {
g.setColor(col);
g.drawLine(i, y - (int) (Math.sqrt(r * r - ((i - x) * (i - x)))),
i, y + step
- (int) (Math.sqrt(r * r - ((i - x) * (i - x)))));
}
// 输出气球的下半部分
for (i = x - r; i <= x + r; i++) {
g.setColor(Color.white);
g.drawLine(i, y + (int) (Math.sqrt(r * r - ((i - x) * (i - x)))),
i, y + step
+ (int) (Math.sqrt(r * r - ((i - x) * (i - x)))));
}
}
public void paint(Graphics g) {
int i, j, tmp;
if (bubble < MAXBUBBLES || thisbubble < MAXBUBBLES) {
record[thisbubble][0] = (int) (Math.random() * 300);
record[thisbubble][1] = 320;
record[thisbubble][2] = (int) (Math.random() * 400) / 20;
record[thisbubble][3] = (int) (Math.random() * 255);
record[thisbubble][4] = (int) (Math.random() * 255);
// 输出气球
draw_bubble(record[thisbubble][0], record[thisbubble][1],
record[thisbubble][2], new java.awt.Color(
record[thisbubble][3], record[thisbubble][4], 255),
g);
// 如气球数小于最大值,则总气球数自增1,当前气球编号自增1
if (bubble < MAXBUBBLES) {
bubble++;
thisbubble++;
} else
// 气球数等于最大值
thisbubble = MAXBUBBLES;
}
for (i = 0; i < bubble; i++) {
if (i % 5 <= stepper) {
record[i][1] -= 1;
// 移动气球
move_bubble(record[i][0], record[i][1], record[i][2],
new java.awt.Color(record[i][3], record[i][4], 255), 1,
g);
for (j = 0; j < i; j++) {
tmp = ((record[i][1] - record[j][1])
* (record[i][1] - record[j][1]) + (record[i][0] - record[j][0])
* (record[i][0] - record[j][0]));
if (j != i && Math.sqrt(tmp) < record[i][2] + record[j][2]) {
for (tmp = record[i][2]; tmp >= -1; tmp = tmp - 2)
draw_bubble(record[i][0], record[i][1],
record[i][2] - tmp, Color.white, g);
draw_bubble(record[j][0], record[j][1], record[j][2],
new java.awt.Color(record[j][3], record[j][4],
255), g);
record[i][1] = -1;
record[i][2] = 0;
}
}
}
if (record[i][1] + record[i][2] < 0 && bubble >= MAXBUBBLES) {
thisbubble = i;
}
stepper = (int) (Math.random() * 10);
}
}
public void update(Graphics g) {
paint(g);
}
public void start() {
// 启动applet,创建并启动线程
if (artist == null) {
artist = new Thread(this);
artist.start();
}
}
public void stop() {
// 结束applet
artist = null;
}
public void run() {
// 启动线程
while (artist != null) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
repaint();
}
artist = null;
}
}

实例341 逐渐浮现的图片

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.image.PixelGrabber;
import java.util.Random;

public class FloatsPicture extends Applet {
private Image image;// 声明图片对象
private int x = 25, y = 25;// 定义int型变量 分别表示x和y坐标点的值
private Random random;// 声明随机数类Random对象
private int width, height, w, h, image_size, pixels[];// 定义int型变量和数组
public void init() { // 初始化applet
this.setSize(400, 400);
random = new Random();
// 从网页获取图片文件名参数
String imageName = "7.jpg";
// 加载图片
image = getImage(getDocumentBase(), imageName);
MediaTracker imageTracker = new MediaTracker(this);
imageTracker.addImage(image, 0);
try {
imageTracker.waitForID(0);
} catch (InterruptedException e) {
}
}
public void start() {
// 启动applet,然后调用paint()方法
width = getSize().width; // applet宽度
height = getSize().height; // applet高度
w = image.getWidth(this); // 图片宽度
h = image.getHeight(this); // 图片高度
// 图片输出位置
x = (width - w) / 2;
y = (height - h) / 2;
// 图片大小
image_size = w * h;
// 创建图片的像素数组
pixels = new int[image_size];
// 创建一个像素获取器的实例,并将其与像素数组关联
PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
try {
// 解析图片的像素信息
pg.grabPixels();
} catch (InterruptedException e) {
}
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, getSize().width, getSize().height);
// 调用drawImage()方法,在相应的位置输出图片
drawImage(g, image, x, y);
}
private void drawImage(Graphics g, Image image, int x, int y) {
// 输出图片
while (true) {
g.setColor(Color.white);
g.fillRect(0, 0, getSize().width, getSize().height);
try {
int one_time = w; // 图片宽度
int S_x = 0, S_y = 0;
S_x = (int) (random.nextFloat() * width);
S_y = (int) (random.nextFloat() * height);
Laser[] nextlot = new Laser[one_time];
int k = 0, l = 0;
int step = 1, start = 0;
float f = random.nextFloat();
step = (f < 0.8) ? 34759 : (f < 0.9 ? 1 : image_size - 1); // 步长
// start=(int)(random.nextFloat()*image_size); //起始位置
f = random.nextFloat();
start = (f < 0.5) ? image_size : 0; // 如果f小于0.5,则起始位置为图片大小,否则为0
int sofar = 0;
// 初始化nextlot数组
for (k = start; l < image_size; l++, k += step) {
Thread.sleep(2);
if (k < 0)
k += image_size;
k %= image_size;
int row = k / w;
int col = k % w;
Color colr = new Color(pixels[k]);
int finishx = x + col;
int y1 = y + row;
nextlot[sofar] = new Laser(colr, new Point(S_x, S_y),
new Point(finishx, y1));
sofar++;
if (sofar == one_time) {
Track(g, nextlot);
sofar = 0;
}
}
} catch (Exception e) {
}
g.setPaintMode();
g.drawImage(image, x, y, this);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
}
private synchronized void Track(Graphics g, Laser[] nextlot) {
Color back = Color.white;
g.setXORMode(back);
for (int pass = 0; pass < 2; pass++) {
for (int pixnr = 0; pixnr < nextlot.length; pixnr++) {
Laser p = nextlot[pixnr];
if (!close(p.c, back)) {
g.setColor(p.c);
g.drawLine(p.start.x, p.start.y, p.finish.x, p.finish.y);
}
if (pass == 1) {
g.setColor(p.c);
g.drawLine(p.finish.x, p.finish.y, p.finish.x, p.finish.y);
}
}
}
Thread.yield();
}
private boolean close(Color c1, Color c2) {
return (Math.abs(c1.getRed() - c2.getRed())
+ Math.abs(c1.getGreen() - c2.getGreen()) + Math.abs(c1
.getBlue()
- c2.getBlue())) < 0xff;
}
}
class Laser {
public Color c;
public Point start, finish;
public Laser(Color c, Point start, Point finish) {
this.c = c;
this.start = start;
this.finish = finish;
}
}


实例342 火焰边框的特效

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;

public class FireBorder extends Applet implements Runnable
// 在applet中支持线程,需要实现Runnable接口
{
private Image bimg, fimg, offI, virtualI;// 声明Image图象对象
private Graphics offG, virtualG;// 声明Graphics对象
private Thread thread = null;// 声明Thread多线程对象
private MediaTracker imageTracker;// 声明MediaTracker对象
private int height, width, X, Y;// 声明int型变量
public void init() {
// 初始化applet,加载背景图片、前景图片
this.setSize(300, 200);
String imageName = "01.jpg";
String imageName2 = "5.gif";
bimg = getImage(getDocumentBase(), imageName);// 获取背景图片
fimg = getImage(getDocumentBase(), imageName2);// 获取前景图片
imageTracker = new MediaTracker(this); // 创建一个媒体跟踪器的实例
// 将图片加入到 MedialTracker 的监视队列中去, image 为要被监视的图像对象,
// 0 为监视图像在监视队列中的标识号
imageTracker.addImage(bimg, 0);
imageTracker.addImage(fimg, 0);
width = this.getSize().width; // 设置applet宽度
height = this.getSize().height; // 设置applet高度
try {
imageTracker.waitForID(0); // 加载图片
} catch (InterruptedException e) {
}
offI = createImage(width, height);
offG = offI.getGraphics();
virtualI = createImage(width * 2, height * 2);
virtualG = virtualI.getGraphics();
}
public void start() {
// 启动applet,创建并启动一个线程
if (thread == null) {
thread = new Thread(this); // 以applet初始化线程
thread.start(); // 启动线程,调用run()方法
}
}
public void run() {
// 线程调用开始
int x = 0, y = 0;
int tileWidth = bimg.getWidth(this); // 设置招牌宽度
int tileHeight = bimg.getHeight(this); // 设置招牌高度
while (thread != null) {
try {
Thread.sleep(10);
x = virtualI.getWidth(this) - width;
y = virtualI.getHeight(this) - height;
// 在不同的坐标位置输出图像以产生燃烧效果
for (; (x > 0) && (y > 0); x--, y--) {
if ((x == 0) || (y == 0)) {
x = virtualI.getWidth(this) - width;
y = virtualI.getHeight(this) - height;
}
// 输出图像,产生燃烧特效
for (int j = 0; j < virtualI.getHeight(this); j = j
+ tileHeight)
for (int i = 0; i < virtualI.getWidth(this); i = i
+ tileWidth)
virtualG.drawImage(bimg, i, j, this);
virtualG.drawImage(fimg, x, y, width, height, this);
offG.drawImage(virtualI, -x, -y, this);
// 输出applet,调用update()方法
repaint();
}
} catch (InterruptedException e) {
}
}
}
public void update(Graphics g) {
// 调用paint()方法
paint(g);
}
public void paint(Graphics g) {
// 输出applet
g.drawImage(offI, 0, 0, this);
}
}


实例343 局部放大效果  

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class ZoomWords extends Applet implements MouseMotionListener {
Graphics g;// 声明Graphics对象
Image image;// 声明Image对象
Image back_Image;// 声明Image对象
String name;// 声明String对象
MediaTracker tracker; // 声明媒体跟踪器tracker
int Glass_X = 0, Glass_Y = 0; // 放大镜初始位置
int Glass_W = 100, Glass_H = 100; // 放大镜宽度、高度
int width, height; // 声明背景图片的宽度和高度
public void init() { // 初始化applet
// 加载图片
this.setSize(370, 500);
g = getGraphics();
name = "01.jpg";
tracker = new MediaTracker(this);
back_Image = getImage(getDocumentBase(), name);
image = createImage(250, 100); // 设置放大后的图象的大小
Graphics offg = image.getGraphics();
offg.drawImage(back_Image, 0, 0, this);
addMouseMotionListener(this); // 添加鼠标事件侦听
}
public void mouseDragged(MouseEvent e) { // 鼠标拖拽事件处理
}
public void mouseMoved(MouseEvent e) { // 处理鼠标移动事件
reprintGlass(Glass_X, Glass_Y, e.getX(), e.getY()); // 通过鼠标位置设置放大镜的位置
// 设置放大镜的当前位置
Glass_X = e.getX();
Glass_Y = e.getY();
// 若放大镜溢出applet则进行调整
if (Glass_X > (width - Glass_W / 2))
Glass_X = width - Glass_W / 2;
if (Glass_Y > (height - Glass_H / 2))
Glass_Y = height - Glass_H / 2;
printGlass(); // 调用自定义方法—输出放大镜
}
void printGlass() {
Graphics temp = g.create(); // 复制g的一个实例
temp.clipRect(Glass_X, Glass_Y, Glass_W, Glass_H); // 为temp限制一个矩形区域
temp.drawImage(back_Image, -Glass_X, -Glass_Y, width * 2, height * 2,
null); // 输出放大后的图象
g.setColor(Color.black);// 设置放大镜边框的颜色
g.drawRect(Glass_X, Glass_Y, Glass_W - 1, Glass_H - 1);// 输出放大镜边框
}
void reprintGlass(int X, int Y, int new_X, int new_Y) { // 清除已经画过的矩形框和放大的图象
Graphics temp = g.create(); // 同上
if (new_X <= X && new_Y <= Y) {
temp.clipRect(new_X, new_Y + Glass_H, Glass_W + X - new_X, Y
- new_Y);
temp.drawImage(image, 0, 0, null);
temp = g.create();
temp.clipRect(new_X + Glass_W, new_Y, X - new_X, Glass_H + Y
- new_Y);
temp.drawImage(image, 0, 0, null);
} else if (new_X > X && new_Y <= Y) {
temp.clipRect(X, new_Y + Glass_H, Glass_W + new_X - X, Y - new_Y);
temp.drawImage(image, 0, 0, null);
temp = g.create();
temp.clipRect(X, new_Y, new_X - X, Glass_H + Y - new_Y);
temp.drawImage(image, 0, 0, null);
} else if (new_X > X && new_Y > Y) {
temp.clipRect(X, Y, Glass_W + new_X - X, new_Y - Y);
temp.drawImage(image, 0, 0, null);
temp = g.create();
temp.clipRect(X, Y, new_X - X, Glass_H + new_Y - Y);
temp.drawImage(image, 0, 0, null);
} else {
temp.clipRect(new_X, Y, Glass_W + X - new_X, new_Y - Y);
temp.drawImage(image, 0, 0, null);
temp = g.create();
temp.clipRect(new_X + Glass_W, Y, X - new_X, Glass_H + new_Y - Y);
temp.drawImage(image, 0, 0, null);
}
}
public boolean imageUpdate(Image img, int infoflags, int x, int y, int w,
int h) { // 判断infoflags参数是否已完全加载了图像,是则返回false;否则返回true
if (infoflags == ALLBITS) { // ALLBITS指示现在已完成了一幅以前绘制的静态图像,并且可以其最终形式再次绘制它。
width = back_Image.getWidth(this);
height = back_Image.getHeight(this);
image = createImage(width + Glass_W / 2, height + Glass_H / 2);
Graphics offg = image.getGraphics();
offg.setColor(Color.white);
offg.fillRect(0, 0, width + Glass_W / 2, height + Glass_H / 2);
offg.drawImage(back_Image, 0, 0, this);
repaint();
return false;
} else
return true;
}
public void paint(Graphics g) {
g.drawImage(back_Image, 0, 0, this); // 输出背景图片
printGlass(); // 画放大镜
}
}


实例344 水波荡漾的效果


import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
public class Reflection extends Applet implements Runnable {
Thread thread = null; // 声明线程
private Graphics g, inv_g; // 定义绘制正常图像和倒立后图像的Graphics对象
private Image image, invimage; // image用于载入正常图像,invimage用于载入倒立后图像
private int inv; // 应用于形成倒立影象的变量
private int image_W = 2, image_H = 2; // 定义装载图片宽和高的变量
private boolean load_Flag = false; // 定义标志,其作用是标志加载图片是否完毕
private final int fre = 14; // 定义水纹波动的频率,数值越大,波动越慢。
private String picture_name = ""; // 定义图片名字
public void init() { // 初始化applet
picture_name = "5.jpg";
}
public void paint(Graphics g) {
if (!load_Flag) // 如果已经载入图片,则返回
return;
if (invimage != null) { // 输出倒影图片
g.drawImage(invimage, (-inv * image_W), image_H, this);
g.drawImage(invimage, ((fre - inv) * image_W), image_H, this);
}
g.drawImage(image, 0, -1, this); // 输出正向图片
}
public void start() { // 启动applet,创建并启动线程
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}
public void run() {// 启动线程
// 加载图片
inv = 0;
g = getGraphics();
MediaTracker imageTracker = new MediaTracker(this);
image = getImage(this.getCodeBase(), picture_name);
imageTracker.addImage(image, 0);
try {
imageTracker.waitForAll();
load_Flag = !imageTracker.isErrorAny(); // 检查媒体跟踪器跟踪的所有图像的错误状态
} catch (InterruptedException e) {
}
// 图片宽度、图片高度
image_W = image.getWidth(this);
image_H = image.getHeight(this);
this.setSize(image_W+1, image_H * 2 - 19);
creatWater(); // 生成倒影
repaint(); // 重新输出applet
while (true) {
try {
if (!load_Flag)
return;
if (invimage != null) {
g.drawImage(invimage, (-inv * image_W), image_H, this);
g.drawImage(invimage, ((fre - inv) * image_W), image_H,
this);
}
g.drawImage(image, 0, -1, this);
if (++inv == fre)
inv = 0;
Thread.sleep(50);
} catch (InterruptedException e) {
stop();
}
}
}
public void creatWater() { //产生水波特效
Image back = createImage(image_W+3, image_H + 2);
Graphics graphics = back.getGraphics();
int phase = 0;
int x, y;
double p1;
graphics.drawImage(image, 0, 1, this);
for (int i = 0; i < (image_H >> 1); i++) {
graphics.copyArea(0, i, image_W, 1, 0, image_H - i);
graphics.copyArea(0, image_H - 1 - i, image_W, 1, 0, -image_H + 1
+ (i << 1));
graphics.copyArea(0, image_H, image_W, 1, 0, -1 - i);
}
invimage = createImage((fre + 1) * image_W+20, image_H+2);
inv_g = invimage.getGraphics();
inv_g.drawImage(back, fre * image_W, 0, this);
for (phase = 0; phase < fre; phase++) {
p1 = 2 * Math.PI * (double) phase / (double) fre;
x = (fre - phase) * image_W;
for (int i = 0; i < image_H; i++) {
y = (int) ((image_H / 14)
* ((double) i + 28.0)
* Math.sin((double) ((image_H / 14) * (image_H - i))
/ (double) (i + 1) + p1) / (double) image_H);
if (i < -y)
inv_g.copyArea(fre * image_W, i, image_W, 1, -x, 0);
else
inv_g.copyArea(fre * image_W, i + y, image_W, 1, -x,
-y);
}
}
graphics.drawImage(image, 0, 1, this);
image = back;
}
}


实例345 漫天飞花   


import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.net.URL;
import java.util.Random;
public class MissileDemo extends Applet implements Runnable {
public int speed, variability, Max_Number, Max_Energy, Max_Patch,
Max_Length, G;
public String sound;
private int width, height; // 获取当前容器边界的宽和高
private Thread thread = null; // 设置线程
private BeaClassDemo bcd[]; // 创建BeaClassDemo类数组bcd
public void init() { // Applet初始化
int i;
this.setSize(400, 400); // 设置当前容器的宽和高
width = getSize().width - 1;
height = getSize().height - 1;
speed = 30; // 烟花绽放的速度
variability = 10;
Max_Number = 100; // 可发出烟花的最大数目
Max_Energy = width + 50;
Max_Patch = 80; // 最大的斑点数
Max_Length = 200; // 斑点的最大距离
G = 50; // 向地面弯曲的力度
bcd = new BeaClassDemo[Max_Number]; // 初始化BeaClassDemo数组
for (i = 0; i < Max_Number; i++)
bcd[i] = new BeaClassDemo(width, height, G); // 创建BeaClassDemo类对象
}
public void start() { // 启动线程
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}
public void stop() { // 停止线程
if (thread != null) {
thread.stop();
thread = null;
}
}
public void run() {
int i;
int E = (int) (Math.random() * Max_Energy * 3 / 4) + Max_Energy / 4 + 1;
int P = (int) (Math.random() * Max_Patch * 3 / 4) // 烟花的斑点数
+ Max_Patch / 4 + 1;
int L = (int) (Math.random() * Max_Length * 3 / 4) // 烟花可发射出的距离
+ Max_Length / 4 + 1;
long S = (long) (Math.random() * 10000); // 产生的随机数
boolean sleep; // 体眠的标志
Graphics g = getGraphics();
URL u = null;
while (true) {
try {
thread.sleep(1000 / speed);
} catch (InterruptedException x) {
}
sleep = true;
for (i = 0; i < Max_Number; i++)
sleep = sleep && bcd[i].sleep;
if (sleep && Math.random() * 100 < variability) {
E = (int) (Math.random() * Max_Energy * 3 / 4) + Max_Energy / 4
+ 1;
P = (int) (Math.random() * Max_Patch * 3 / 4) + Max_Patch / 4
+ 1;
L = (int) (Math.random() * Max_Length * 3 / 4) + Max_Length / 4
+ 1;
S = (long) (Math.random() * 10000);
}
for (i = 0; i < Max_Number; i++) {
if (bcd[i].sleep && Math.random() * Max_Number * L < 1) {
bcd[i].init(E, P, L, S);
bcd[i].start();
}
bcd[i].show(g);
}
}
}
public void paint(Graphics g) { // 绘制组件
g.setColor(Color.black); // 设置背景颜色为黑
g.fillRect(0, 0, width + 1, height + 1); // 根据参数画矩形
}
}
class BeaClassDemo {
public boolean sleep = true;
private int energy, patch, length, width, height, G, Xx, Xy, Ex[], Ey[], x,
y, Red, Blue, Green, t;
private Random random; // 声明Random类对象
public BeaClassDemo(int a, int b, int g) { // 类BeaClassDemo的构造方法
width = a;
height = b;
G = g;
}
public void init(int e, int p, int l, long seed) {// 初始化
int i;
// 赋值运算
energy = e;
patch = p;
length = l;
// 创建一个带种子的随机数生成器
random = new Random(seed);
Ex = new int[patch]; // 初始化int数组Ex,其长度为patch
Ey = new int[patch]; // 初始化int数组Ey,其长度为patch
// 随机生成不透明的sRGB颜色值
Red = (int) (random.nextDouble() * 128) + 128;
Blue = (int) (random.nextDouble() * 128) + 128;
Green = (int) (random.nextDouble() * 128) + 128;
Xx = (int) (Math.random() * width / 2) + width / 4;
Xy = (int) (Math.random() * height / 2) + height / 4;
for (i = 0; i < patch; i++) {
Ex[i] = (int) (Math.random() * energy) - energy / 2;
Ey[i] = (int) (Math.random() * energy * 7 / 8) - energy / 8;
}
}
public void start() {
t = 0;
sleep = false;
}
public void show(Graphics g) { // 输出烟花
if (!sleep) // 如果休眠状态为false
if (t < length) {
int i, c;
double s;
Color color;
c = (int) (random.nextDouble() * 64) - 32 + Red;
if (c >= 0 && c < 256)
Red = c;
c = (int) (random.nextDouble() * 64) - 32 + Blue;
if (c >= 0 && c < 256)
Blue = c;
c = (int) (random.nextDouble() * 64) - 32 + Green;
if (c >= 0 && c < 256)
Green = c;
color = new Color(Red, Blue, Green);
for (i = 0; i < patch; i++) {
s = (double) t / 100;
x = (int) (Ex[i] * s);
y = (int) (Ey[i] * s - G * s * s);
g.setColor(color);
g.drawLine(Xx + x, Xy - y, Xx + x, Xy - y);
if (t >= length / 2) {
int j;
for (j = 0; j < 2; j++) {
s = (double) ((t - length / 2) * 2 + j) / 100;
x = (int) (Ex[i] * s);
y = (int) (Ey[i] * s - G * s * s);
g.setColor(Color.black);
g.drawLine(Xx + x, Xy - y, Xx + x, Xy - y);
}
}
}
t++;
} else {
sleep = true;
}
}
}

实例346 动感影集   

import java.applet.Applet;
import java.awt.Choice;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;

public class PhotoAlbum extends Applet {
private Choice myChoice;// 声明Choice对象
private String[] myString1, myString2;// 声明String类型的数组
private int totalPics;// 声明一个int型变量
private Image offI;// 声明Image对象
private Image[] img;// 声明一组Image对象数组
private Graphics offG;// 声明一个Graphics对象
private MediaTracker imagetracker;// 声明一个MediaTracker对象
public void init() {// 对Applet小程序进行初始化
this.setSize(600, 450);
this.setLayout(null);
myChoice = new Choice();
myChoice.setBounds(10, 10, 290, 20);
totalPics = 8;
myString1 = new String[totalPics];
myString2 = new String[totalPics];
img = new Image[totalPics];
for (int i = 0; i < totalPics; i++) {
myString1[i] = new String("");
myString2[i] = new String("");
}
String s = new String("");
imagetracker = new MediaTracker(this);// 加载图片
for (int i = 0; i < totalPics; i++) {
s = "第 " + (i + 1) + " 张照片";
myString1[i] = s;
System.out.println(myString1[i]);
myChoice.addItem(s);// 向下拉列表中增加选项
s = (i + 1) + ".jpg";
myString2[i] = s;
img[i] = getImage(getDocumentBase(), s);
imagetracker.addImage(img[i], 0);
}
try {
imagetracker.waitForID(0);
} catch (InterruptedException e) {
}
add(myChoice);
offI = createImage(getSize().width, getSize().height - 40);// 创建图象
offG = offI.getGraphics();
offI = img[0];
offG.drawImage(offI, 0, 0, this);
repaint();
}
public void paint(Graphics g) {
g.drawImage(offI, 10, 40, this);
}
// 使用action()方法来获得Java Applet小程序运行时所发生的事件
public boolean action(Event e, Object o) {
if (e.target == myChoice) {
// String s = new String("");
offG.setColor(this.getBackground());
offG.fillRect(0, 40, getSize().width, getSize().height - 40);
offI = img[myChoice.getSelectedIndex()];
offG.drawImage(offI, 0, 0, this);
repaint();
}
return true;
}
}


实例347 彩虹字   

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;

public class RainbowWords extends Applet implements Runnable {
String str = null;
Thread thread = null; // 设置一个线程
char[] ch;
int p = 0;
Image image;
Graphics gphics;
Color[] color;
int one = 1;
int two = 18;
int three = 18;
private Font f; // 字体
private FontMetrics fm; // 字模
public void init() {
str = "Java Applet小程序"; // 设置七彩文字内容
this.setSize(520, 200); // 设置Applet的大小
setBackground(Color.white); // 设置背景颜色
ch = new char[str.length()];
ch = str.toCharArray(); // 将字符串中的各个字符保存到数组中
image = createImage(getSize().width, getSize().height);
gphics = image.getGraphics();
f = new Font("", Font.BOLD, 18);
fm = getFontMetrics(f); // 获得指定字体的字体规格
gphics.setFont(f); // 设置组件的字体
float hue;
color = new Color[str.length()]; // 颜色的色元
for (int i = 0; i < str.length(); i++) {
hue = ((float) i) / ((float) str.length());
color[i] = new Color(Color.HSBtoRGB(hue, 0.8f, 1.0f)); // 颜色分配
}
}
public void start() { // 线程开始的类
if (thread == null) { // 如果线程为空,则
thread = new Thread(this);
// 开始新的线程
thread.start(); // 开始
}
}
// 终止线程
public void stop() {
if (thread != null) { // 如果线程不为空,则
thread.stop(); // 终止线程,使它
thread = null; // 为空
}
}
// 运行线程
public void run() {
while (thread != null) {
try {
thread.sleep(200); // 让线程沉睡200毫秒
} catch (InterruptedException e) {
}
repaint(); // 重新绘制界面
}
}
public void update(Graphics g) { // 重写update方法,解决闪烁问题
int x, y;
double a;
gphics.setColor(Color.black);
gphics.fillRect(0, 0, getSize().width, getSize().height);
p += one;
p %= 7; // 主要控制字的速度,被除数越小,速度越快
// System.out.println(p+” p1”);
for (int i = 0; i < str.length(); i++) {
a = ((p - i * one) % 7) / 4.0 * Math.PI; // 主要控制弧度的,被除数越小,弧度越大
x = 30 + fm.getMaxAdvance() * i + (int) (Math.cos(a) * two); // 求x坐标值
y = 80 + (int) (Math.sin(a) * three); // 求y坐标值
gphics.setColor(color[(p + i) % str.length()]);
gphics.drawChars(ch, i, 1, x, y);
}
paint(g);
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
}

实例348 多功能按键

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.MediaTracker;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class StrongButton extends Applet implements MouseListener {
private Graphics grap; // 声明Graphics变量
private MediaTracker media; // 声明MediaTracker变量
private AudioClip audioA, audioB; // 声明AudioClip变量
private int width, height; // 声明int类变量
private Image image, img1, img2, img3; // 声明Image类型变量
private Label la = new Label("多功能按键"); // 创建一个带初始值的Label对象
public void init() { // 为Applet初始化
audioA = getAudioClip(this.getDocumentBase(), "aa.wav"); // 创建audioA对象
audioB = getAudioClip(getDocumentBase(), "hh.wav"); // //创建audioB对象
width = getSize().width; // 返回Applet的宽度
height = getSize().height; // 返回Applet的高度
image = createImage(width, height); // 根据参数创建一个Image对象
grap = image.getGraphics(); // 根据图像创建Graphics对像
media = new MediaTracker(this); // MediaTracker对像实例化
img1 = getImage(getDocumentBase(), "2.jpg"); // 根据参数创建Image对象
media.addImage(img1, 0); // 将img1放入media对象中
img2 = getImage(getDocumentBase(), "3.jpg");
media.addImage(img2, 1);
img3 = getImage(getDocumentBase(), "4.jpg");
media.addImage(img3, 2);
try {
media.waitForAll(); // 等待media加载所有的图像
} catch (InterruptedException e) {
}
la.setSize(100, 20);
la.setForeground(Color.blue); // 设置标签的前景颜色
this.add(la, BorderLayout.NORTH); // 将标签组件加载到Applet中
addMouseListener(this); // 为Applet添加鼠标侦听事件
}
public void start() { // 开始Applet程序
grap.drawImage(img1, 0, 0, width + 5, height, this); // 根据给定的参数绘制图像
repaint();
}
public void mouseClicked(MouseEvent e) { // 鼠标单击事件
}
public void mousePressed(MouseEvent e) { // 鼠标按下事件
grap.drawImage(img3, 0, 0, width + 5, height, this); // 当鼠标被按下时所绘制的图像
audioA.stop(); // audioA停止播放声音
audioB.play(); // audioB开始播放声音
la.setBackground(Color.black); // 设置标签的背景颜色
la.setForeground(Color.red); // 设置标签的前景颜色
la.setText("audioB is playing"); // 设置标签中要显示的内容
this.add(la, BorderLayout.NORTH); // 添加标签组件
repaint(); // 重新绘制组件
}
public void mouseReleased(MouseEvent e) { // 鼠标释放事件
grap.drawImage(img2, 0, 0, width + 5, height, this);
repaint();
audioB.stop();
audioA.play();
la.setBackground(Color.red);
la.setForeground(Color.black);
la.setText("audioA is playing");
this.add(la, BorderLayout.NORTH);
}
public void mouseEntered(MouseEvent e) { // 鼠标进入Applet所触发的事件
grap.drawImage(img2, 0, 0, width, height, this);
repaint();
}
public void mouseExited(MouseEvent e) { // 鼠标离开Applet所触发的事件
grap.drawImage(img1, 0, 0, width, height, this);
repaint();
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, width, height, this);
}
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值