多线程、弹球游戏的开发以及彩球弹出、运动的暂停、删除彩球

 

题外话(请直接略过):放假已经过去了一个多月,才开始学习。

1.学java语言还是要大量学习时间啊,就像学英语,如果没有最初的那一阵痛,后面会走得很难!哭
2.别以为一天一两个小时就可以了,必须每天投入大量时间。
3.就像学语言一样,要背的。犹豫

正文:

1.线程的概念:在一个程程序中,这些独立运行的程序片断叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理”。利用线程,用户可按下一个按钮,然后程序会立即作出响应,而不是让用户等待程序完成了当前任务以后才开始响应。


2.线程对象是继承java.util.Thread类的对象或者implements java.util.runnable接口的类的对象。现成的启动调用县城对象的start方法。

3.弹球游戏中把每一个弹球看成是一个单独的线程。每个小球运行自己的,所以先要定义一个  小球的线程类:

import java.awt.Color; 
import java.awt.Graphics; 
import java.util.Random; 

import javax.swing.JPanel; 

public class BallThread extends Thread{ 
int radius;//半径 
int xv;//水平方向速度 
int yv;//竖直方向速度 
int x,y;//彩球的坐标 
int lx,ly;//彩球最后一次的位置 
    Color color;//彩球的颜色 
    JPanel jp; 
    int xDirection = 1; 
    int yDirection = 2; 
Graphics g; 
boolean stopFlag = false; 
boolean pauseFlag = false; 
   
public  BallThread(JPanel jp,Graphics g){ 
this.jp=jp; 
this.g=g; 

    } 
/** 
  * setPause和setStop方法的作用是:从其他类中传入暂停和停止的状态 
  * 使其他类可以控制其true或false的状态 
  */ 
public void setPause(boolean flag){ 
this.pauseFlag=flag; 
} 
public void setStop(boolean flag){ 
this.stopFlag = flag; 
} 


public void run(){ 
Random random = new Random(); 
this.radius = 20+random.nextInt(20); 
this.xv = 2+random.nextInt(5); 
this.yv = 2+random.nextInt(5); 
color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)); 

while(true){ 
try { 
sleep(50); 
if(stopFlag){ 
g.setColor(jp.getBackground()); 
g.fillOval(lx-radius, ly-radius, radius*2, radius*2); 
return; 
} 
if(pauseFlag){ 
continue; 

} 
} catch (InterruptedException e) { 
e.printStackTrace(); 
} 
g.setColor(jp.getBackground()); 
g.fillOval(lx-radius, ly-radius, radius*2, radius*2); 

g.setColor(color); 
g.fillOval(x-radius, y-radius, radius*2, radius*2); 

lx=x; 
ly=y; 
if(x>=jp.getWidth()-radius){ 
xDirection=1; 
} 
if(x=jp.getHeight()-radius){ 
yDirection=3; 
} 

 

 

4.主界面UI:

 

 

import java.awt.BorderLayout;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BubbleUI extends JFrame{
	
Graphics g0;

public void initui(){
	this.setTitle("弹球の0720");
	this.setSize(800,500);
	this.setLayout(new BorderLayout());
	JPanel jp = new JPanel();
	JPanel jp2 = new JPanel();
	
	JButton bt1 = new JButton("弹出");
	JButton bt2 = new JButton("暂停");
	JButton bt3 = new JButton("开始");
	JButton bt4 = new JButton("删除");
	jp.add(bt1);
	jp.add(bt2);
	jp.add(bt3);
	jp.add(bt4);
	
	this.add(jp,BorderLayout.NORTH);
	this.add(jp2,BorderLayout.CENTER);
	
	this.setVisible(true);
	g0=jp2.getGraphics();
	ButtonListener l = new ButtonListener(jp2,g0);
	bt1.addActionListener(l);
	bt2.addActionListener(l);
	bt3.addActionListener(l);
	bt4.addActionListener(l);
}
public static void main(String[] args){
	BubbleUI ui = new BubbleUI();
	ui.initui();
}
}

 

 


5.主界面上按钮的监听器:

import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 

import javax.swing.JPanel; 

public class ButtonListener implements ActionListener{ 
Graphics g1; 
JPanel jp2; 
ArrayList list =new ArrayList(); 

public ButtonListener(JPanel jp2,Graphics g1){ 
this.jp2=jp2; 
this.g1=g1; 

} 
public void actionPerformed(ActionEvent e) { 

if("弹出".equals(e.getActionCommand())){ 
BallThread ball = new BallThread(jp2,g1); 
ball.start(); 
list.add(ball); 
} 
if("暂停".equals(e.getActionCommand())){ 
for(BallThread ball:list){ 
ball.setPause(true); 
} 
} 
if("开始".equals(e.getActionCommand())){ 
for(BallThread ball:list){ 
ball.setPause(false); 
} 
} 
if("删除".equals(e.getActionCommand())){ 
        if(!list.isEmpty()){ 
        BallThread ball = list.remove(0); 
        ball.setStop(true); 
        } 
} 

} 
  
} 

 

 

 



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值