Java 线程初用:运用线程知识实现多小球游动

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();
    }
}

在这里插入图片描述

二、创建两个小球线程

  1. 创建线程类Ball_1和Ball_2

  2. 重写run方法

    • 利用循环不断更改球的坐标并绘制
  3. 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();
    }
}

体验“群魔乱舞”

在这里插入图片描述

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Neko1145

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值