Java awt 生日抽奖小程序
开发愿景:我女朋友不发解我的IT到底是做什么的,所以在她生日的时候我想到了做这个东西,一方面希望她能了解我的工作,另一方面希望能让她开心,还有另一个方面就是,我不想洗衣服啦。。。。哈哈哈哈
好了,看我的初版代码,因为是赶出来的,所以就没有考虑面向对象了。做IT有时候就要给自己找些乐子嘛~~~
好了,看我的初版代码,因为是赶出来的,所以就没有考虑面向对象了。做IT有时候就要给自己找些乐子嘛~~~
Code
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.util.Random;
public class HappyBirthday extends Frame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
TextArea ta;
TextField tf;
Button btn;
Miusk miusk;
MyThread thread;
Panel p;
public HappyBirthday() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
setTitle("命中注定抽奖系统");
setSize(400, 300);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();// 获取屏幕大小
int w = getSize().width;
int h = getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
setLocation(x, y);
ta = new TextArea();
tf = new TextField(40);
btn = new Button("确定");
btn.addActionListener(this);
p = new Panel();
p.add(tf);
p.add(btn);
miusk = new Miusk();
add(ta, BorderLayout.CENTER);
add(p, BorderLayout.SOUTH);
setVisible(true);
miusk.play1();
try {
showMsg("欢迎来到'命中注定抽奖系统'");
Thread.sleep(4000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
thread = new MyThread(this);
thread.setOn(true);
thread.index = 1;
thread.start();
}
public void showMsg(String msg) {
ta.append(msg + "\n");
}
public void clearMsg() {
ta.setText("");
}
@Override
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == btn) {
if (tf.getText().length() == 0) {
thread.index = 2;
thread.setOn(true);
} else if (tf.getText().equals("1")) {
showMsg("随机抽奖开始啦");
Thread.sleep(2000);
thread.index = 3;
thread.setOn(true);
} else if (tf.getText().equals("0")) {
showMsg("你真的要放弃?如果不放弃,请输入1再按确定");
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new HappyBirthday();
}
}
class Miusk {
private static AudioClip ac1;
private static AudioClip ac2;
public Miusk() {
URL url1 = getClass().getResource("shengrikuaile1.wav");
ac1 = Applet.newAudioClip(url1);// 加载音频文件
URL url2 = getClass().getResource("shengrikuaile2.wav");
ac2 = Applet.newAudioClip(url2);
}
public void play1() // 变形时的声音
{
ac1.loop();
}
public void play2() // 加速下时的声音
{
ac2.play();
}
}
class MyThread extends Thread {
HappyBirthday hb;
String[] msg;
int index;
boolean isOn;
int sleepTime;
int sumTime;
int maxTime;
String[] jiang = { "一等奖", "二等奖", "三等奖", "幸运奖", "倒霉奖" };
Random random = new Random();
public boolean isOn() {
return isOn;
}
public void setOn(boolean isOn) {
this.isOn = isOn;
}
public MyThread(HappyBirthday hb) {
this.hb = hb;
}
public void setMsg(String[] msg) {
this.msg = msg;
}
public void run() {
try {
while (true) {
if (isOn) {
if (index == 1) {
sleepTime = 4000;
maxTime = 20000;
hb.showMsg("祝你生日快乐~~");
this.sleep(sleepTime);
sumTime += sleepTime;
if (sumTime > maxTime) {
isOn = false;
hb.showMsg("");
this.sleep(3000);
hb.clearMsg();
hb.showMsg("小兰子,生日啦,我为你准备了下面的抽奖活动:");
hb.showMsg("一等奖:手机一个");
hb.showMsg("二等奖:衣服一套");
hb.showMsg("三等奖:无条件为你洗一个月的衣服");
hb.showMsg("幸运奖:无条件为你按摩一个月");
hb.showMsg("请按确定继续");
sumTime = 0;
}
} else if (index == 2) {
hb.clearMsg();
hb.showMsg("当然,为了在生日这天考验一下你的勇气");
hb.showMsg("本系统加入了唯一一个反面奖项");
this.sleep(2000);
hb.showMsg("倒霉奖:帮我无条件的洗一个月的衣服");
this.sleep(2000);
hb.showMsg("本系统本着公平、公正、公开原则");
this.sleep(2000);
hb.showMsg("你可以选择放弃,同时得到一个苹果");
this.sleep(2000);
hb.showMsg("你也可以进行抽奖");
this.sleep(2000);
hb.showMsg("输入0,按确定则放弃");
hb.showMsg("输入1,按确定则开始抽奖");
hb.showMsg("请选择");
isOn = false;
} else if (index == 3) {
int i = random.nextInt(4);
hb.clearMsg();
hb.showMsg(jiang[i]);
this.sleep(100);
sumTime += 500;
if (sumTime > 10000) {
isOn = false;
hb.clearMsg();
hb.showMsg(jiang[0]);
this.sleep(900);
hb.clearMsg();
hb.showMsg(jiang[1]);
this.sleep(1000);
hb.clearMsg();
hb.showMsg(jiang[2]);
this.sleep(1100);
hb.clearMsg();
hb.showMsg(jiang[4]);
this.sleep(1200);
hb.clearMsg();
hb.showMsg(jiang[1]);
this.sleep(1500);
hb.clearMsg();
hb.showMsg(jiang[3]);
this.sleep(1800);
hb.clearMsg();
hb.showMsg(jiang[2]);
this.sleep(2000);
hb.clearMsg();
hb.showMsg("倒霉奖\n");
this.sleep(2000);
hb.showMsg("恭喜你获得本次的倒霉奖");
hb.showMsg("帮我无条件的洗一个月的衣服");
hb.showMsg("这也能得倒霉奖啊?!!");
hb.showMsg("我也不知道结果会是这样的哦");
isOn = false;
}
}
} else {
this.sleep(1000);
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.util.Random;
public class HappyBirthday extends Frame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
TextArea ta;
TextField tf;
Button btn;
Miusk miusk;
MyThread thread;
Panel p;
public HappyBirthday() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
setTitle("命中注定抽奖系统");
setSize(400, 300);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();// 获取屏幕大小
int w = getSize().width;
int h = getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
setLocation(x, y);
ta = new TextArea();
tf = new TextField(40);
btn = new Button("确定");
btn.addActionListener(this);
p = new Panel();
p.add(tf);
p.add(btn);
miusk = new Miusk();
add(ta, BorderLayout.CENTER);
add(p, BorderLayout.SOUTH);
setVisible(true);
miusk.play1();
try {
showMsg("欢迎来到'命中注定抽奖系统'");
Thread.sleep(4000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
thread = new MyThread(this);
thread.setOn(true);
thread.index = 1;
thread.start();
}
public void showMsg(String msg) {
ta.append(msg + "\n");
}
public void clearMsg() {
ta.setText("");
}
@Override
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == btn) {
if (tf.getText().length() == 0) {
thread.index = 2;
thread.setOn(true);
} else if (tf.getText().equals("1")) {
showMsg("随机抽奖开始啦");
Thread.sleep(2000);
thread.index = 3;
thread.setOn(true);
} else if (tf.getText().equals("0")) {
showMsg("你真的要放弃?如果不放弃,请输入1再按确定");
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new HappyBirthday();
}
}
class Miusk {
private static AudioClip ac1;
private static AudioClip ac2;
public Miusk() {
URL url1 = getClass().getResource("shengrikuaile1.wav");
ac1 = Applet.newAudioClip(url1);// 加载音频文件
URL url2 = getClass().getResource("shengrikuaile2.wav");
ac2 = Applet.newAudioClip(url2);
}
public void play1() // 变形时的声音
{
ac1.loop();
}
public void play2() // 加速下时的声音
{
ac2.play();
}
}
class MyThread extends Thread {
HappyBirthday hb;
String[] msg;
int index;
boolean isOn;
int sleepTime;
int sumTime;
int maxTime;
String[] jiang = { "一等奖", "二等奖", "三等奖", "幸运奖", "倒霉奖" };
Random random = new Random();
public boolean isOn() {
return isOn;
}
public void setOn(boolean isOn) {
this.isOn = isOn;
}
public MyThread(HappyBirthday hb) {
this.hb = hb;
}
public void setMsg(String[] msg) {
this.msg = msg;
}
public void run() {
try {
while (true) {
if (isOn) {
if (index == 1) {
sleepTime = 4000;
maxTime = 20000;
hb.showMsg("祝你生日快乐~~");
this.sleep(sleepTime);
sumTime += sleepTime;
if (sumTime > maxTime) {
isOn = false;
hb.showMsg("");
this.sleep(3000);
hb.clearMsg();
hb.showMsg("小兰子,生日啦,我为你准备了下面的抽奖活动:");
hb.showMsg("一等奖:手机一个");
hb.showMsg("二等奖:衣服一套");
hb.showMsg("三等奖:无条件为你洗一个月的衣服");
hb.showMsg("幸运奖:无条件为你按摩一个月");
hb.showMsg("请按确定继续");
sumTime = 0;
}
} else if (index == 2) {
hb.clearMsg();
hb.showMsg("当然,为了在生日这天考验一下你的勇气");
hb.showMsg("本系统加入了唯一一个反面奖项");
this.sleep(2000);
hb.showMsg("倒霉奖:帮我无条件的洗一个月的衣服");
this.sleep(2000);
hb.showMsg("本系统本着公平、公正、公开原则");
this.sleep(2000);
hb.showMsg("你可以选择放弃,同时得到一个苹果");
this.sleep(2000);
hb.showMsg("你也可以进行抽奖");
this.sleep(2000);
hb.showMsg("输入0,按确定则放弃");
hb.showMsg("输入1,按确定则开始抽奖");
hb.showMsg("请选择");
isOn = false;
} else if (index == 3) {
int i = random.nextInt(4);
hb.clearMsg();
hb.showMsg(jiang[i]);
this.sleep(100);
sumTime += 500;
if (sumTime > 10000) {
isOn = false;
hb.clearMsg();
hb.showMsg(jiang[0]);
this.sleep(900);
hb.clearMsg();
hb.showMsg(jiang[1]);
this.sleep(1000);
hb.clearMsg();
hb.showMsg(jiang[2]);
this.sleep(1100);
hb.clearMsg();
hb.showMsg(jiang[4]);
this.sleep(1200);
hb.clearMsg();
hb.showMsg(jiang[1]);
this.sleep(1500);
hb.clearMsg();
hb.showMsg(jiang[3]);
this.sleep(1800);
hb.clearMsg();
hb.showMsg(jiang[2]);
this.sleep(2000);
hb.clearMsg();
hb.showMsg("倒霉奖\n");
this.sleep(2000);
hb.showMsg("恭喜你获得本次的倒霉奖");
hb.showMsg("帮我无条件的洗一个月的衣服");
hb.showMsg("这也能得倒霉奖啊?!!");
hb.showMsg("我也不知道结果会是这样的哦");
isOn = false;
}
}
} else {
this.sleep(1000);
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}