设计一个简单的弹球模型:当小球移动到屏幕左端时向右移动,移动到屏幕右端时向左移动
其实我们只需要二个线程:
第一个线程:不停地在界面上画小球的位置;
第二个线程:不停地改变小球的坐标;
以上两个线程共享存放小球的队列,鼠标每点击一次,就向队列中存入一个小球,这里我们使用ArrayList来存储小球队列
1、第一个线程:画小球
public class ThreadDraw extends Thread {
private ArrayList<Bullet> bs;
private Graphics g;
//构造函数初始化
public ThreadDraw(ArrayList<Bullet> bs, Graphics g) {
this.bs = bs;
this.g = g;
}
@Override
public void run() {
while (true) {
//对ArrayList中的每个小球进行绘制
for (int i = 0; i < bs.size(); i++) {
Bullet b = bs.get(i);
g.setColor(Color.red);
g.fillOval(b.getX(), b.getY(), b.getRound(), b.getRound());
}
try {
Thread.sleep(20);
} catch (Exception e) {
}
//清屏
g.setColor(Color.white);
g.fillRect(0, 0, 1500, 700);
}
}
}
2、第二个线程:移动小球
public class ThreadMove extends Thread {
private JFrame jFrame;
private ArrayList<Bullet> bs;
//构造函数初始化
public ThreadMove(ArrayList<Bullet> bs, JFrame jFrame) {
this.bs = bs;
this.jFrame = jFrame;
}
@Override
public void run() {
while (true) {
//对队列中的每个小球都移动一定距离
for (int i = 0; i < bs.size(); i++) {
Bullet bullet = bs.get(i);
//当到达屏幕最左端或者最右端时改变小球运动方向
if (bullet.getX() >= jFrame.getWidth() || bullet.getX() <= 0) {
bullet.changeState();
}
//移动小球
bullet.move();
}
try {
Thread.sleep(20);
} catch (Exception e) {
}
}
}
}
小球类:
public class Bullet {
private int x, y;
private int round = 20;
private int factor = 3;
public Bullet(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getRound() {
return round;
}
public void move() {
this.x += factor;
}
public void changeState() {
factor = -factor;
}
}
界面设计:
public void init() {
this.setSize(800, 400);
this.setTitle("小球碰撞");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
Graphics g = this.getGraphics();
//事件处理类MouseLis
MouseLis lis = new MouseLis(bs);
this.addMouseListener(lis);
//启动两个线程,调用start函数
ThreadMove bm = new ThreadMove(bs, this);
bm.start();
ThreadDraw bt = new ThreadDraw(bs, g);
bt.start();
}
鼠标监听器,点击一次加入一个小球:
public void mousePressed(MouseEvent e) {
Bullet bs1 = new Bullet(e.getX(), e.getY());
this.bs.add(bs1);
}
运行效果:
其中小球是不断左右运动的
完整代码:
Bullet类:
package p1;
public class Bullet {
private int x, y;
private int round = 20;
private int factor = 3;
public Bullet(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getRound() {
return round;
}
public void move() {
this.x += factor;
}
public void changeState() {
factor = -factor;
}
}
MouseLis类:
package p1;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.Array;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.function.Predicate;
public class MouseLis extends MouseAdapter {
private ArrayList<Bullet> bs;
public MouseLis(ArrayList<Bullet> bs) {
this.bs = bs;
}
@Override
//每点击一次鼠标向队列中加入一个小球
public void mousePressed(MouseEvent e) {
Bullet bs1 = new Bullet(e.getX(), e.getY());
this.bs.add(bs1);
}
}
ThreadDraw类:
package p1;
import java.awt.*;
import java.util.ArrayList;
public class ThreadDraw extends Thread {
private ArrayList<Bullet> bs;
private Graphics g;
public ThreadDraw(ArrayList<Bullet> bs, Graphics g) {
this.bs = bs;
this.g = g;
}
@Override
public void run() {
while (true) {
for (int i = 0; i < bs.size(); i++) {
Bullet b = bs.get(i);
g.setColor(Color.red);
g.fillOval(b.getX(), b.getY(), b.getRound(), b.getRound());
}
try {
Thread.sleep(20);
} catch (Exception e) {
}
//清屏
g.setColor(Color.white);
g.fillRect(0, 0, 1500, 700);
}
}
}
ThreadMove类:
package p1;
import javax.swing.*;
import java.util.ArrayList;
public class ThreadMove extends Thread {
private JFrame jFrame;
private ArrayList<Bullet> bs;
public ThreadMove(ArrayList<Bullet> bs, JFrame jFrame) {
this.bs = bs;
this.jFrame = jFrame;
}
@Override
public void run() {
while (true) {
for (int i = 0; i < bs.size(); i++) {
Bullet bullet = bs.get(i);
if (bullet.getX() >= jFrame.getWidth() || bullet.getX() <= 0) {
bullet.changeState();
}
bullet.move();
}
try {
Thread.sleep(20);
} catch (Exception e) {
}
}
}
}
Fly类:
package p1;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.nio.Buffer;
import java.util.ArrayList;
public class Fly extends JFrame {
private ArrayList<Bullet> bs = new ArrayList<>();
public void init() {
this.setSize(800, 400);
this.setTitle("小球碰撞");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
Graphics g = this.getGraphics();
MouseLis lis = new MouseLis(bs);
this.addMouseListener(lis);
ThreadMove bm = new ThreadMove(bs, this);
bm.start();
ThreadDraw bt = new ThreadDraw(bs, g);
bt.start();
}
public static void main(String[] args) {
Fly f = new Fly();
f.init();
}
}