验证中...
私信发送成功
语言: Java
分类: 其他
最后更新于 2018-01-07 15:18
片段 1 片段 2 片段 3 片段 4 片段 5 片段 6
Ball.java
原始数据 复制代码
import java.awt.Color;
import java.awt.Graphics;
public class Ball {
private int x=0;
private int y=0;
double k=0;
double b=0;
private double lastX=0;
private double lastY=0;
int dy=1;
int dx=1;
int r=40;
private Color color;
private MyCanvas canvas;
private static int POINTX=250;
private static int POINTY=100;//顶点
private int num=0;//给小球计数
public Ball(MyCanvas canvas,Color color){
this.canvas=canvas;
this.color=color;
setFirstLocation();
}
public void draw(Graphics g){
g.setColor(color);//颜色
g.drawLine(POINTX,POINTY,x+r/2,y+r/2);
g.fillOval(x,y,r,r);//填充椭圆区域
}
private void setFirstLocation(){//初始化位置和斜率
x=(int)((Math.random()*canvas.getWidth()));
y=(int)((Math.random()*canvas.getHeight()));
k=(y-POINTY)/((x-POINTX)+0.1);
b=POINTY-k*POINTX;
if(y>POINTY)//向汇聚点移动
dy=-1;
if(x>POINTX)
dx=-1;
}
public void move(){
lastX=x;
lastY=y;
if(lastY+dy<0||lastY+dy>canvas.getHeight()-r)
{
new MusicThread().start();
k=-k;
b=lastY-k*lastX;
if(lastX+dx<0)
dx=1;
else if(lastX+dx>canvas.getWidth()-r)
dx=-1;
if(lastY+dy<0)
dy=1;
else if(lastY+dy>canvas.getHeight()-r)
dy=-1;
}
else if(lastX+dx<0||lastX+dx>canvas.getWidth()-r)
{
new MusicThread().start();
k=-k;
b=lastY-k*lastX;
if(lastY+dy<0)
dy=1;
else if(lastY+dy>canvas.getHeight()-r)
dy=-1;
if(lastX+dx<0)
dx=1;
else if(lastX+dx>canvas.getWidth()-r)
dx=-1;
}
if(Math.abs(k)<1){
x=x+dx;
y=(int)(k*x+b);
}
else
{
y=y+dy;
x=(int)((y-b)/k);
}
/*for (int i = 0; i < canvas.returnBalls().size(); i++) //判断小球间是否发生碰撞
{
Ball ball = canvas.returnBalls().get(i);
if (this.equals(ball)) //自己和自己不碰撞
continue;
if (((ball.x - x) * (ball.x - x) + (ball.y - y) * (ball.y - y) <= r*r)&&ball.getk()!=this.getk())
//当两球间的距离小于直径且斜率不同时,可认为两小球发生了碰撞
{
double degree = Math.atan((y - ball.y) / (x - ball.x));
//获取自己与发生碰撞的小球之间所形成的夹角,因为夹角只能在-pi/2-pi/2之间,所以还需判断两球的x坐标之间的关系
}
} */
}
public void changePoint(int pointx,int pointy){//改变顶点的位置
dy=1;
dx=1;
POINTX=pointx;
POINTY=pointy;
k=(y-POINTY)/((x-POINTX)+0.1);
b=POINTY-k*POINTX;
if(y>POINTY)
dy=-1;
if(x>POINTX)
dx=-1;
}
public Double getk(){//显示斜率
return k;
}
public int getX(){//位置显示每一个小球的编号
return x+20;
}
public int getY(){
return y+20;
}
public void setNumber(int num){//记录小球编号
this.num=num;
}
public int getNumber(){//在BallThread里面label获取小球编号
return num;
}
}
BallThread.java
原始数据 复制代码
import java.awt.Color;
import java.awt.Label;
public class BallThread extends Thread {
private Ball ball;
private Label label;//若不初始化出现NULLPOINTER错误
volatile boolean isStop=false;
volatile boolean stop=false;
private MyCanvas canvas;
public BallThread(Color color,Label label,MyCanvas canvas){
this.label=label;
this.canvas=canvas;
ball=new Ball(canvas,color);
canvas.addBall(ball);
}
public void run(){
while(!isStop){
if(!stop){
ball.move();
label.setText("小球"+ball.getNumber()+"斜率:"+ball.getk());
canvas.repaint();
try{
sleep(6);//时延 3ms
}
catch(Exception e){
};
}
}
}
public void setStop(boolean Stop)
{
isStop = Stop;
}
public void stop(boolean stop)
{
this.stop = stop;
}
}
FirstBall.java
原始数据 复制代码
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
public class FirstBall {
Frame frame=new Frame();
Ball ball;
MyCanvas canvas=new MyCanvas();
//-------------右侧panel显示按钮信息
Button btnAdd=new Button("add");
Button btnClear=new Button("clear");
Button btnStopAndContinue = new Button("cut one"); //随机删除小球按钮
Button btnstop=new Button("stop");
Dialog cutone=new Dialog(frame,"请输入要删除的小球",true);
TextField choice=new TextField(20); //输入小球编号
Button cut=new Button("确定");
Button no=new Button("取消");
Dialog error=new Dialog(frame,"ERROR",true);
Label errors=new Label("错误的小球编号!");
Button errorExit=new Button("确定");
Panel panel=new Panel();
//-----------关闭窗口
Dialog d=new Dialog(frame,"ExitProcess",true);
Button btnYes=new Button("YES");
Button btnNo=new Button("NO");
Button btnCancel=new Button("Cancel");
Label labelExit=new Label("Exit ?");
//-----------左侧状态栏
Panel panelLabel=new Panel();
Label ballk=new Label("小球情况");
Label[] labels=new Label[20];
static int BALLNUM=0;
static int labelNum=0;
//------------菜单
MenuBar mb=new MenuBar();
Menu file=new Menu("文件");
Menu help=new Menu("帮助");
//文件下的选项
MenuItem setItem=new MenuItem("设置",new MenuShortcut(KeyEvent.VK_B));
CheckboxGroup cbg=new CheckboxGroup();
Checkbox check21=new Checkbox("10",cbg,true);
Checkbox check22=new Checkbox("15",cbg,false);
Checkbox check23=new Checkbox("20",cbg,false);
Dialog setDl=new Dialog(frame,"设置小球上限",true);
Button exit4=new Button("exit");
//指定使用“CTRL+X"快捷键退出
MenuItem exitItem=new MenuItem("退出",new MenuShortcut(KeyEvent.VK_X));
//帮助下的选项
Menu brief=new Menu("简介");
MenuItem group=new MenuItem("小组介绍",new MenuShortcut(KeyEvent.VK_A));
MenuItem function=new MenuItem("功能介绍",new MenuShortcut(KeyEvent.VK_C));
Dialog groupIntroduction=new Dialog(frame,"小组介绍",true);
Dialog functionIntroduction=new Dialog(frame,"功能介绍",true);
Label partnerText0=new Label("组名:第九小组");//-------------------------
Label partnerText1=new Label("组长:李思雨");
Label partnerText2=new Label("组员:胡 杰");
Label partnerText3=new Label("组员:曹寅清");
Label guideText0=new Label("add:增加一个小球");
Label guideText1=new Label("clear:清除画布");//---------------------
Label guideText2=new Label("cutone:删除任意编号小球");//-----------------------------
Button exit2=new Button("exit");
Button exit3=new Button("exit");
//----------设置小球上限个数并用对话框显示
static int max=10;
Dialog dballmax=new Dialog(frame,"Warning!",true);
Button btnmax=new Button("exit");//--------通用
Label labelmax=new Label("小球数已经达到上限");
ArrayList<BallThread> thread=new ArrayList<BallThread>();
//-----------监听器内部类
class cutOneListener implements ActionListener{//任意删除一个小球
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (btnStopAndContinue.getLabel().equals("cut one"))
{
btnStopAndContinue.setLabel("cutting");
btnAdd.setEnabled(false); //Add按钮不可用
cutone.setVisible(true);
}
else
{
btnStopAndContinue.setLabel("cut one");
btnAdd.setEnabled(true); //Add按钮可用
}
}
}
class stopListener implements ActionListener{//暂停
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (btnstop.getLabel().equals("stop"))
{
for(BallThread thread0:thread){
thread0.stop(true);
}
btnstop.setLabel("continue");
btnAdd.setEnabled(false); //Add按钮不可用
}
else
{
for(BallThread thread0:thread){
thread0.stop(false);
}
btnstop.setLabel("stop");
btnAdd.setEnabled(true); //Add按钮可用
}
}
}
class cutListener implements ActionListener{//输入小球编号任意删除一个小球
int aBallNumber=0;
boolean nope=false;
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
cutone.setVisible(false);
try{
aBallNumber=Integer.valueOf(choice.getText());}
catch(Exception e1){
nope=true;
error.setVisible(true);
}
if(!nope)
{
if(aBallNumber>thread.size()-1||aBallNumber<0)
error.setVisible(true);
else
{
thread.get(aBallNumber).setStop(true);
thread.remove(aBallNumber);
labels[labelNum]=new Label(" ");
labels[aBallNumber].setText(" ");
labelNum--;
BALLNUM--;
canvas.deleteThisBall(aBallNumber);
}
}
}
}
class addListener implements ActionListener{//add
Color[] colorArray={Color.blue,Color.CYAN,Color.GREEN,
Color.magenta,Color.red,Color.yellow,Color.pink,Color.orange,Color.blue};
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(canvas.returnBalls().size()>=max){
dballmax.setVisible(true);
}
else{
int i=(int)(Math.random()*9);
labels[labelNum].setBounds(0,50+labelNum*30,100,20);
panelLabel.add(labels[labelNum]);
thread.add(new BallThread(colorArray[i],labels[BALLNUM],canvas));
thread.get(BALLNUM).start();
BALLNUM++;
labelNum++;
}
}
}
class clearListener implements ActionListener{//clear
public void actionPerformed(ActionEvent e){
// TODO Auto-generated method stub
for(BallThread thread0:thread){
thread0.setStop(true);
}
canvas.deleteBall();//只是canvas不画小球而已我们看不到,实际小球的进程仍在运行。
canvas.repaint();
for(int i=0;i<labelNum;i++){
labels[i].setText(" ");
}
labelNum=0;
BALLNUM=0;
thread.removeAll(thread);
}
}
class closeWindowListener extends WindowAdapter{//对话框提示是否关闭窗口
public void windowClosing(WindowEvent e){
d.setVisible(true);
}
}
class closeListener implements ActionListener{//对话框里的按钮yes关闭窗口
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
}
class cancelOrCloseListener implements ActionListener{//对话框里的按钮cancel取消关闭窗口/删除任意一个球对话框的关闭
private Dialog d;
public cancelOrCloseListener(Dialog d){
this.d=d;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
d.setVisible(false);
}
}
class exitDialogListener implements ActionListener{//接口 传值实现通用的监听器:对话框的关闭:小球已达上限对话框/小组成员情况/功能介绍
private Dialog d;
public exitDialogListener(Dialog d){
this.d=d;
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
d.setVisible(false);
}
}
class setMaxListener extends FocusAdapter{//设置小球上限为20/10/15
private int ballnum;
public setMaxListener(int ballnum){
this.ballnum=ballnum;
}
public void focusGained(FocusEvent e){
if(max<ballnum)
{
for(int i=max;i<ballnum;i++){
labels[i]=new Label(" ");
}
}
max=ballnum;
}
}
class displayDialogListener implements ActionListener{//通用的监听器:小组成员情况介绍+功能介绍
private Dialog d;
public displayDialogListener(Dialog d){
this.d=d;
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
d.setVisible(true);
}
}
public void init(){
canvas.setBounds(100,0,800,800);
canvas.addMouseListener(canvas.f6);//鼠标点击画布聚点改变
canvas.setBackground(Color.white);
//-------------左侧状态栏
panelLabel.setBounds(5,0,100,800);
panelLabel.setBackground(Color.red);
panelLabel.setLayout(null);
ballk.setBounds(0,0,100,50);
panelLabel.add(ballk);
int i=0;
for(i=0;i<max;i++){
labels[i]=new Label(" ");
}
//-------------右侧状态栏
panel.setBounds(800,0,100,800);
panel.setBackground(Color.red);
addListener f1=new addListener();
btnAdd.addActionListener(f1);
btnAdd.setBounds(5,60,90,50);
btnAdd.setBackground(Color.white);
panel.add(btnAdd);
clearListener f2=new clearListener();
btnClear.addActionListener(f2);
btnClear.setBounds(5,130,90,50);
btnClear.setBackground(Color.white);
panel.add(btnClear);
btnStopAndContinue.setBounds(5,200,90,50);
cutOneListener f7=new cutOneListener();
btnStopAndContinue.addActionListener(f7);
btnStopAndContinue.setBackground(Color.white);
panel.add(btnStopAndContinue);
stopListener fl=new stopListener();
btnstop.addActionListener(fl);
btnstop.setBounds(5,270,90,50);
btnstop.setBackground(Color.white);
panel.add(btnstop);
panel.setLayout(null);
//-------------关闭窗口显示对话框
closeWindowListener f3=new closeWindowListener();//按右上角的x显示对话框
frame.addWindowListener(f3);
closeListener f4=new closeListener();
cancelOrCloseListener f5=new cancelOrCloseListener(d);
labelExit.setBounds(20,50,100,20);
btnYes.setBounds(100,100,50,20);
btnYes.addActionListener(f4);
btnCancel.setBounds(160,100,50,20);
btnCancel.addActionListener(f5);
d.setBounds(400,400,250,130);
d.add(labelExit);
d.add(btnYes);
d.add(btnCancel);
d.setLayout(null);
//-------------菜单
//文件
displayDialogListener f110=new displayDialogListener(d);
exitItem.addActionListener(f110);
//----设置小球上限
check21.setBounds(0,50,200,25);
check22.setBounds(0,75,200,25);
check23.setBounds(0,100,200,25);
exitDialogListener f116=new exitDialogListener(setDl);
exit4.addActionListener(f116);
exit4.setBounds(0,125,200,25);
setMaxListener f15=new setMaxListener(10);
check21.addFocusListener(f15);
setMaxListener f11=new setMaxListener(15);
check22.addFocusListener(f11);
setMaxListener f12=new setMaxListener(20);
check23.addFocusListener(f12);
setDl.add(check21);
setDl.add(check22);
setDl.add(check23);
setDl.add(exit4);
setDl.setLayout(null);
setDl.setBounds(500,500,200,200);
displayDialogListener f9=new displayDialogListener(setDl);
setItem.addActionListener(f9);
file.add(setItem);
//------------添加菜单分割线
file.addSeparator();
file.add(exitItem);
//-----------小组介绍
partnerText0.setBounds(110,50,100,40);
partnerText1.setBounds(110,100,100,40);
partnerText2.setBounds(110,140,100,40);
partnerText3.setBounds(110,180,100,40);
groupIntroduction.add(partnerText0);
groupIntroduction.add(partnerText1);
groupIntroduction.add(partnerText2);
groupIntroduction.add(partnerText3);
exitDialogListener f114=new exitDialogListener(groupIntroduction);
exit3.addActionListener(f114);
exit3.setBounds(95,220,100,40);//----------------------
groupIntroduction.add(exit3);
groupIntroduction.setBounds(400,400,300,300);
groupIntroduction.setLayout(null);
displayDialogListener f111=new displayDialogListener(groupIntroduction);
group.addActionListener(f111);
//-----------功能介绍
exitDialogListener f115=new exitDialogListener(functionIntroduction);
exit2.addActionListener(f115);
exit2.setBounds(105, 240, 100, 50);
guideText0.setBounds(105, 60, 100, 50);
guideText1.setBounds(105, 120, 100, 50);
guideText2.setBounds(105,180,150,50);
functionIntroduction.add(guideText0);
functionIntroduction.add(guideText1);
functionIntroduction.add(guideText2);
functionIntroduction.add(exit2);
functionIntroduction.setBounds(400,400,300,300);
functionIntroduction.setLayout(null);
displayDialogListener f112=new displayDialogListener(functionIntroduction);
function.addActionListener(f112);
brief.add(group);
//-------------添加菜单分割线
brief.add(new MenuItem("-"));
brief.add(function);
help.add(brief);
mb.add(file);
mb.add(help);
frame.setMenuBar(mb);
//小球数设置上限
dballmax.setBounds(500,500,300,130);
exitDialogListener f8=new exitDialogListener(dballmax);
btnmax.addActionListener(f8);
dballmax.add(labelmax,BorderLayout.CENTER);
dballmax.add(btnmax, BorderLayout.SOUTH);
cut.setBounds(45, 150, 100, 30);
cutListener aCut=new cutListener();
cut.addActionListener(aCut);
no.setBounds(165, 150, 100, 30);
cancelOrCloseListener noListener=new cancelOrCloseListener(cutone);
no.addActionListener(noListener);
errors.setBounds(95,60,100,30);
errorExit.setBounds(95, 120, 100, 30);
cancelOrCloseListener errorListener=new cancelOrCloseListener(error);
errorExit.addActionListener(errorListener);
error.add(errors);
error.add(errorExit);
error.setLayout(null);
error.setBounds(400, 400, 300, 200);
choice.setBounds(45, 90, 100, 30);
cutone.add(choice);
cutone.add(cut);
cutone.add(no);
cutone.setLayout(null);
cutone.setBounds(400, 400, 300, 200);
frame.add(panelLabel,BorderLayout.WEST);
frame.add(panel,BorderLayout.EAST);
frame.add(canvas,BorderLayout.CENTER);
frame.setBounds(80,80,900,800);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new FirstBall().init();
}
}
MusicThread.java
原始数据 复制代码
import java.io.FileInputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
@SuppressWarnings("restriction")
public class MusicThread extends Thread {
public void run(){
try{
FileInputStream fileau=new FileInputStream("src/bom.wav");
AudioStream as=new AudioStream(fileau);
AudioPlayer.player.start(as);
}catch(Exception e){
e.printStackTrace();
}
}
}
MyCanvas.java
原始数据 复制代码
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
public class MyCanvas extends Canvas{
/**
*
*/
private static final long serialVersionUID = 1L;
class SixthListener extends MouseAdapter{//继承父类 鼠标点击改变小球运动方向
public void mousePressed(MouseEvent e){
for(Ball ball:balls){
ball.changePoint(e.getX(),e.getY());
}
repaint();
}
}
SixthListener f6=new SixthListener();
ArrayList<Ball> balls=new ArrayList<Ball>();
Ball ball;
public void paint(Graphics g){
int i=0;
for(Ball ball:balls){
ball.draw(g);
g.setColor(Color.black);
g.drawString(" "+i,ball.getX(), ball.getY()); //显示各个小球编号
ball.setNumber(i);//记录小球编号
i++;
}
}
public void addBall(Ball ball){//加球
balls.add(ball);
}
public void deleteBall(){//清空canvas
balls.clear();
}
public ArrayList<Ball> returnBalls(){
return balls;
}
public void deleteThisBall(int num){
balls.remove(num);
}
}
bom.wav

评论列表( 1 )

1718506_bananasimon
Simon 2018-01-07 15:18

帅哥,你做的这个是为了干嘛呢??

你可以在登录后,对此项目发表评论

6_float_left_people 6_float_left_close