Java 线程初用:运用线程知识实现多小球游动
上篇文章我们对多线程知识有了初步的了解,这篇文章我们进行简单的应用,让多个小球在界面游动
一、UI界面
使用之前我们学的JFrame创建一个简单的界面
public class GameUI extends JFrame {
public void initUI(){
setTitle("线程游戏-v0.1");
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// 直接关闭
setLocationRelativeTo(null);// 居中
getContentPane().setBackground(Color.WHITE);// 白色背景
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
public static void main(String[] args) {
new GameUI().initUI();
}
}
二、创建两个小球线程
创建线程类Ball_1和Ball_2
重写run方法
- 利用循环不断更改球的坐标并绘制
GameUI中启动线程
public class Ball_1 extends Thread {
int x, y;
Graphics g;
//x y 速度
int speedx = 2;
int speedy = 4;
//传入画笔
public Ball_1(JFrame jf) {
this.g = jf.getGraphics();
}
@Override
public void run() {
for (int i = 0;; i++) {
g.setColor(Color.BLACK);
g.fillOval(x, y, 20, 20);
try {
Thread.sleep(60);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 去除球移动残留的黑边
g.setColor(Color.WHITE);
g.fillOval(x, y, 20, 20);
x+=speedx;
y+=speedy;
// 碰到边框改变方向
if (x <= 0 || x >= 780) {
speedx = -speedx;
}
if (y <= 0 || y >= 580) {
speedy = -speedy;
}
}
}
}
public class Ball_2 extends Thread {
int x1, y1;
Graphics g;
//x y 速度
int speedx = 2;
int speedy = 8;
//传入画笔
public Ball_2(JFrame jf) {
this.g = jf.getGraphics();
}
@Override
public void run() {
x1 = 500;
y1 = 500;
for (int i = 0; ; i++) {
g.setColor(Color.BLACK);
g.fillOval(x1, y1, 20, 20);
try {
Thread.sleep(60);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 去除球移动残留的黑边
g.setColor(Color.WHITE);
g.fillOval(x1, y1, 20, 20);
x1 += speedx;
y1 += speedy;
// 碰到边框改变方向
if (x1 <= 0 || x1 >= 780) {
speedx = -speedx;
}
if (y1 <= 0 || y1 >= 580) {
speedy = -speedy;
}
}
}
}
在窗体GameUI中创建小球对象并启动小球线程
public class GameUI extends JFrame {
public void initUI(){
setTitle("线程游戏-v0.1");
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// 直接关闭
setLocationRelativeTo(null);// 居中
getContentPane().setBackground(Color.WHITE);// 白色背景
// 小球对象
ball_1 = new Ball_1(this);
ball_2 = new Ball_2(this);
// 启动
ball_1.start();
ball_2.start();
}
Ball_1 ball_1;
Ball_2 ball_2;
@Override
public void paint(Graphics g) {
super.paint(g);
}
public static void main(String[] args) {
new GameUI().initUI();
}
}
现在运行我们就可以看见两个移动的小球了
三、多个小球
和上面思路一样,这次我们创建500个颜色、速度、起始位置都随机的小球进一步体验多线程的魅力。
public class Ball_3 extends Thread {
int x1, y1;
Graphics g;
//x y 速度
int speedx;
int speedy;
//传入画笔
public Ball_3(JFrame jf) {
this.g = jf.getGraphics();
}
@Override
public void run() {
Random random = new Random();
x1 = random.nextInt(800);
y1 = random.nextInt(600);
speedx = random.nextInt(10)-5;
speedy = random.nextInt(10)-5;
Color color = new Color(random.nextInt(Integer.MAX_VALUE));
for (int i = 0; ; i++) {
g.setColor(color);
g.fillOval(x1, y1, 20, 20);
try {
Thread.sleep(60);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 去除球移动残留的黑边
g.setColor(Color.WHITE);
g.fillOval(x1, y1, 20, 20);
x1 += speedx;
y1 += speedy;
// 碰到边框改变方向
if (x1 <= 0 || x1 >= 780) {
speedx = -speedx;
}
if (y1 <= 0 || y1 >= 580) {
speedy = -speedy;
}
}
}
}
public class GameUI extends JFrame {
public void initUI(){
setTitle("线程游戏-v0.1");
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// 直接关闭
setLocationRelativeTo(null);// 居中
getContentPane().setBackground(Color.WHITE);// 白色背景
// ball_1 = new Ball_1(this);
// ball_2 = new Ball_2(this);
//
// ball_1.start();
// ball_2.start();
// 500个彩色小球线程
for (int i = 0; i < 500; i++) {
ball_3 = new Ball_3(this);
ball_3.start();
}
}
Ball_1 ball_1;
Ball_2 ball_2;
Ball_3 ball_3;
@Override
public void paint(Graphics g) {
super.paint(g);
}
public static void main(String[] args) {
new GameUI().initUI();
}
}
体验“群魔乱舞”