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

📄 thunder.java

📁 JavaApplet实例编程源代码, 里面有好几章
💻 JAVA
字号:
import java.applet.*;
import java.awt.*;

public class Thunder extends Applet implements Runnable
{
	private Thread thread = null;
	private boolean no_thunder = true;
	private boolean thunder = true;
	private int[] light;
	private int[] b1;
	private int[] b2;
	private Color whiteSky = new Color(0,0,65), yellowSky = new Color(144,40,40);
	private Image buffer, image;
	private String delay = "3"; 

	public void init()
	{
		image = getImage(getCodeBase(),"City.gif");
		light = new int[getSize().height];
		b1 = new int[getSize().height];
		b2 = new int[getSize().height];
		buffer = this.createImage(getSize().width, getSize().height);
	}
	
	public void paint(Graphics g)
	{
		int i, thr;
		if (no_thunder)
		{
			g.setColor(Color.black);
			g.fillRect(0 , 0 , getSize().width, getSize().height);
			g.drawImage(image,0,0,this);
		}
		else
		{
			if(thunder)
				g.setColor(whiteSky);
			else 
				g.setColor(yellowSky);
			g.fillRect(0 , 0 , getSize().width, getSize().height);
			thr = (int) (0.8F * getSize().height);
			for (i = 1; i < getSize().height; i++)
			{
				if (i < thr)
				{
					g.setColor(Color.darkGray);
					g.drawRect(light[i]-4, i, 3, 1);
					g.drawRect(light[i]+2, i, 3, 1);
					g.setColor(Color.gray);
					g.drawRect(light[i]-1, i, 1, 1);
					g.drawRect(light[i]+1, i, 1, 1);
				}
				if(thunder)
				{
					g.setColor(Color.white);
				}
				else 
					g.setColor(Color.yellow);
				g.drawLine(light[i], i, light[i-1], i-1);
				if (b1[i] >= 0)
				{
					g.drawLine(b1[i], i, b1[i-1], i-1);
				}
				if (b2[i] >= 0)
				{
					g.drawLine(b2[i], i, b2[i-1], i-1);
				}
			}
			g.drawImage(image,0,0,this);
			thunder = !thunder;
		}
	}
   
	void drawBuffer()
	{
		Graphics g;
		g = buffer.getGraphics();
		paint(g);
	}
	
	public void start()
	{
		if (thread == null)
		{
			thread = new Thread(this);
			thread.start();
		}
	}
   
	public void stop()
	{
		if (thread != null)
		{
			thread.stop();
			thread = null;
		}
	}

	void createThunder()
	{
		int i;
		int bs1, bs2;
		int be1, be2;
		light[0] = (int) (Math.random() * getSize().width);
		b1[0] = light[0];
		b2[0] = light[0];
		bs1 = (int) (Math.random() * getSize().height) + 1;
		bs2 = (int) (Math.random() * getSize().height) + 1;
		be1 = bs1 + (int) (0.5 * Math.random() * getSize().height) + 1;
		be2 = bs2 + (int) (0.5 * Math.random() * getSize().height) + 1;
		for (i = 1; i<getSize().height; i++)
		{			light[i] = light[i-1] + ((Math.random() >0.5)?1:-1);
			b1[i] = light[i];
			b2[i] = light[i];
		}
		for (i = bs1; i<getSize().height; i++)
		{
			b1[i] = b1[i-1] + ((Math.random() >0.5)?2:-2);
		}
		for (i = bs2; i<getSize().height; i++)
		{
			b2[i] = b2[i-1] + ((Math.random() >0.5)?2:-2);
		}
		for (i = be1; i<getSize().height; i++)
		{
			b1[i] = -1;
		}
		for (i = be2; i<getSize().height; i++)
		{
			b2[i] = -1;
		}
	}
	
	public void run()
	{
		Graphics g;
		while (true)
		{
			try
			{
				drawBuffer();
				g = this.getGraphics();
				g.drawImage(buffer, 0, 0, this);
				Thread.sleep((int) (Integer.parseInt(delay) * 1000 * Math.random()));
				no_thunder = false;
				createThunder();
				drawBuffer(); 
				g = this.getGraphics(); 
				g.drawImage(buffer, 0, 0, this);
				Thread.sleep(1000);
				no_thunder = true;
			}
			catch (InterruptedException e)
			{
				stop();
			}
		}
	}
}

⌨️ 快捷键说明

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