⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shapesapplet.java

📁 主要是用Applet编写的一个简单的动画
💻 JAVA
字号:
import java.awt.*;
import java.lang.Math;
import java.util.Random;
//定义ShapesApplet类
public class ShapesApplet extends java.applet.Applet {
    public int W = 400;
    public int H = 100;
    public int N = 15;
    public final int minSize = 10;
    public final int maxSize = 50;
    public final int minNaptime = 1;
    public final int maxNaptime = 500;
    public final Color colors[] = {
	Color.red,
	Color.pink,
	Color.orange,
	Color.yellow,
	Color.green,
	Color.magenta,
	Color.blue,
	Color.cyan,
	Color.white,
	Color.gray,
	Color.lightGray,
	Color.darkGray
    };
    public final Color bg = Color.black;
    boolean threadSuspended = false;
    Image im;
    Graphics offscreen;
    Random rand;
    Shape shapes[] = new Shape[N];
    //初始化 	
    public void init() {
	resize(W, H);
	rand = new Random((long)System.currentTimeMillis());
	try {
	    im = createImage(W, H);
	    offscreen = im.getGraphics();
	} catch (Exception e) {
	    offscreen = null;
	}
    }
    //使用随机参数创建每个形状,并且启动其线程
    public void start() {
	for (int i=0; i<N; i++) {
	    if (shapes[i] == null) {
		shapes[i] = new Shape(this,
		    (int)((double)W*rand.nextDouble()),
		    (int)((double)H*rand.nextDouble()),
		    minSize + (int)((double)(maxSize-minSize)*rand.nextDouble()),
		    minNaptime + (int)((double)(maxNaptime-minNaptime)*rand.nextDouble()),
		    colors[(int)((double)colors.length*rand.nextDouble())],
		    bg);
		shapes[i].start();
	    }
	}
	repaint();
    }
    //当applet停止时,使每个单独的线程都停止
    public void stop() {
	for (int i=0; i<N; i++) {
	    shapes[i].stop();
	}
    }
    //覆盖update()方法,以便消除闪烁
    public void update(Graphics g) {
	paint(g);
    }
   //使用双缓冲技术,消除闪烁
    public void paint(Graphics g) {
	if (offscreen != null) {
	    paintApplet(offscreen);
	    g.drawImage(im, 0, 0, this);
	} else {
	    paintApplet(g);
	}
    }
    //调用每个形状的paint()方法
    public void paintApplet(Graphics g) {
	boolean changed = false;

	g.setColor(bg);
	g.fillRect(0, 0, W, H);
	for (int i=0; i<N; i++) {
	    shapes[i].paint(g);
	}
    }
    //当出现鼠标事件时,管理每个线程
    public boolean mouseDown(Event e, int x, int y) {
	if (threadSuspended) {
	    for (int i=0; i<N; i++)
		shapes[i].thread.resume();
	} else {
	    for (int i=0; i<N; i++)
		shapes[i].thread.suspend();
	}
	threadSuspended = !threadSuspended;
	return(true);
    }
}
//实现一个形状,每个形状都将调用自己的paint()方法
class Shape implements Runnable {
    static int threadNum = 1;
    private Color color = null;
    private Color bg = Color.black;
    private int naptime = 500;
    private int size = 50;
    private int growthFactor = 1;
    private int ox = 0;
    private int oy = 0;
    private Graphics graphics;
    private int current;
    private int previous;
    private boolean growing = false;

    public Thread thread = null;
    public boolean changed = true;

    ShapesApplet applet;

    public Shape(ShapesApplet applet, int x, int y, int s, int n, Color c, Color bg) {
	this.applet = applet;
	ox = x;
	oy = y;
	size = s;
	naptime = n;
	color = c;
	if (color == null) {
	    color = Color.black;
	}
	current = previous = s;
    }
    //该线程的run()方法
    public void run() {
	thread.setPriority(Thread.MIN_PRIORITY+1);
	while (thread != null) {
	    changed = true;
	    previous = current;
	    if (growing) {
		current += growthFactor;
	    } else {
		current -= growthFactor;
	    }
	    if (current == size || current == 0) {
		growing = !growing;
	    }
	    try { thread.sleep(naptime); } catch (InterruptedException e) {};
	    applet.repaint();
	}
	thread = null;
    }
    //启动线程
    public void start() {
	if (thread == null) {
	    thread = new Thread(this, Integer.toString(threadNum++));
	    thread.start();
	}
    }

    public void stop() {
	thread = null;
    }
    //paint()方法
    public void paint(Graphics g) {
	int prevHalf = (int)(previous/2);
	int curHalf = (int)(current/2);
	g.setColor(bg);
	g.fillRect(ox-prevHalf, oy-prevHalf, previous, previous);	
	g.setColor(color);
	g.fillRect(ox-curHalf, oy-curHalf, current, current);
    }
}










⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -