池塘夜降彩色雨
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
package Caiseyu;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.util.*;
import javax.swing.*;
public class Caiseyu extends JFrame implements ActionListener { Mypanel mp;
JButton jb;
Tishi t1;
JTextField jtf;
public static void main(String[] args) {
Caiseyu c=new Caiseyu();
}
public Caiseyu(){
jb=new JButton("黑夜来了");
jb.addActionListener(this);
jb.setActionCommand("aa");
jtf=new JTextField();
t1=new Tishi();
this.add(jb,BorderLayout.SOUTH);
this.add(t1);
this.setSize(600,600);
this.setTitle("池塘夜降彩色雨");
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("aa"))
{
//黑夜面板
mp=new Mypanel();
//启动线程
Thread t=new Thread(mp);
t.start();
this.add(mp);
this.remove(t1);
this.remove(jb);
this.setVisible(true);
}
}
}
class Tishi extends JPanel
{
public void paint(Graphics g)
{
super.paint(g);
// 设置字体颜色
g.setColor(Color.black);
//设置字体
Font myFont=new Font("华文彩云",Font.BOLD,50);
g.setFont(myFont);
g.drawString("让暴风雨来得更猛烈些吧!!!", 1, 280);
}
}
//定义我的画板
class Mypanel extends JPanel implements Runnable
{
//定义雨水对象
Rain rains[];
int ensize=400;
public Mypanel()
{
this.rains=new Rain[ensize];
//初始化雨水
for(int i=0;i<rains.length;i++)
{
Random x=new Random();
Random y=new Random();
rains[i]=new Rain(x.nextInt(700),y.nextInt(400));
rains[i].setType(i%7);
//启动线程
Thread t=new Thread(rains[i]);
t.start();
}
AePlayWave apw1=new AePlayWave("d://R1.wav");
AePlayWave apw2=new AePlayWave("d://R2.wav");
apw1.start();
apw2.start();
}
//重写repaint函数
public void paint(Graphics g)
{
super.paint(g);
g.fill3DRect(0,0 ,600,600,false);
//闪电
Image img3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/a.gif"));
g.drawImage(img3, 0, 0, 600, 400, this);
for(int i=0; i<rains.length; i++)
{
//画雨
//if(rains[i]!=null&&rains[i].isLive)
this.Drawyushui(rains[i].x, rains[i].y, g,rains[i].getType());
}
//画池塘
g.setColor(Color.blue);
g.draw3DRect(0, 400, 600, 350, false);
g.fill3DRect(0, 400, 600, 350, false);
Image img=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/h.gif"));
g.drawImage(img, 160, 340, 210, 90, this);
Image img1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/k.gif"));
g.drawImage(img1, 330, 450, 140, 60, this);
Image img2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/k.gif"));
g.drawImage(img2, 30, 450, 140, 60, this);
//击雨
for(int i=0;i<rains.length;i++)
{
if(rains[i].isLive)
{
this.hit(rains[i],g,rains[i].type);
}
}
}
//写一个函数判断雨水是否击中水池
public void hit(Rain r,Graphics g,int type)
{
switch(type)
{
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
case 2:
g.setColor(Color.green);
break;
case 3:
g.setColor(Color.gray);
break;
case 4:
g.setColor(Color.red);
break;
case 5:
g.setColor(Color.blue);
break;
case 6:
g.setColor(Color.orange);
break;
case 7:
g.setColor(Color.pink);
break;
}
if(r.x>20&&r.x<500&&r.y>400&&r.y<450) {
g.drawOval(r.x, r.y, 20, 10);
g.fillOval(r.x, r.y, 20, 10);
}
}
//画雨水
public void Drawyushui(int x,int y,Graphics g,int type) {
switch(type)
{
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
case 2:
g.setColor(Color.green);
break;
case 3:
g.setColor(Color.gray);
break;
case 4:
g.setColor(Color.red);
break;
case 5:
g.setColor(Color.blue);
break;
case 6:
g.setColor(Color.orange);
break;
case 7:
g.setColor(Color.pink);
break;
}
//g.drawOval(x, y, 10, 8);
//g.fillOval(x, y, 10, 8);
g.drawLine(x, y, x+10, y+20);
}
@Override
public void run() {
while(true)
{
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
this.repaint();
}
}
}
package Caiseyu;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Vector;
import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
//雨水类,实现runable接口
class Rain implements Runnable
{
int x;
int y;
int type;
int speed=2;
//是否到边缘
boolean isLive=true;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public Rain(int x,int y)
{
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public void run() {
// 重写run函数
while(true)
{
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception }
//向下
y+=speed;
x+=speed/3;
//判断该雨水是否碰到边缘
if(y>500||x>600)
{
this.isLive=false;
//break;
}
Random a=new Random();
Random b=new Random();
if(this.isLive==false)
{
x=a.nextInt(700);
y=b.nextInt(400);
}
System.out.println("线程启动");
}
}
}
//播放声音的类
class AePlayWave extends Thread {
private String filename;
public AePlayWave(String wavfile) {
filename = wavfile;
}
public void run() {
File soundFile = new File(filename);
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
info = new (SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (Exception e) {
e.printStackTrace();
return;
}
auline.start();
int nBytesRead = 0;
//这是缓冲
byte[] abData = new byte[512];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
auline.drain();
auline.close();
}
}
}。