BJFU Java程序设计实验四——红旗9

任务书没找到了,反正是个导弹拦截UI

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class RedFlag extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    public static final int MIN_SPEED = 1;
    public static final int MAX_SPEED = 5;
    JButton startButton, stopButton, interceptButton ,speedupbutton, speeddownbutton;
    JLabel time,count;
    JTextField _time,_count;
    MyPanel mp;
    Random rand = new Random();
    Vector<Ball> balls = new Vector<Ball>();
    private int inter_count;
    private int speed;
    private boolean isPaused;
    private TimeThread timerThread;
    public static void main(String[] args) {
        new RedFlag();
    }

    public RedFlag() {
        mp = new MyPanel();
        setTitle("红旗-9_开发");
        startButton = new JButton("开始");
        stopButton = new JButton("停止");
        interceptButton = new JButton("拦截");
        speedupbutton = new JButton("加速");
        speeddownbutton = new JButton("减速");
        time = new JLabel("计时(秒):");
        count = new JLabel("计数(次):");
        _time = new JTextField("0",5);
        _count = new JTextField("0/120",5);
        _time.setEditable(false);
        _count.setEditable(false);

        speeddownbutton.addActionListener(this);
        speedupbutton.addActionListener(this);
        startButton.addActionListener(this);
        stopButton.addActionListener(this);
        interceptButton.addActionListener(this);

        JPanel p = new JPanel();

        p.add(time);
        p.add(_time);
        p.add(startButton);
        p.add(speedupbutton);
        p.add(interceptButton);
        p.add(speeddownbutton);
        p.add(stopButton);
        p.add(count);
        p.add(_count);

        this.add(mp);
        this.add(p, BorderLayout.SOUTH);
        this.setSize(900, 600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == startButton) {
            inter_count = 0;
            mp.isStart = true;
            Thread t = new Thread(mp);
            t.start();
            start_a();
        } else if (e.getSource() == stopButton) {
            mp.isStart = false;
            //balls.clear();
            mp.repaint();
            pause();
        } else if (e.getSource() == interceptButton) {
            inter_count ++;
            if (inter_count == 120) {
                interceptButton.setEnabled(false);
            }
            _count.setText(inter_count+"/120");

            Ball ball = new Ball(mp.getWidth() / 2, mp.getHeight() - 30, 10, Color.RED, 0,5*speed);
            balls.add(ball);
        } else if (e.getSource() == speedupbutton) {
            up();

        } else if (e.getSource() == speeddownbutton) {
            down();
        }
    }

    class Ball {
        int x, y, r, dx,dy;
        Color color;

        public Ball(int x, int y, int r, Color color, int dx ,int dy) {
            this.x = x;
            this.y = y;
            this.r = r;
            this.color = color;
            this.dx = dx;
            this.dy = dy;
        }

        public void move() {
            x += dx;
            y -= dy;
        }
        public void upordown(){
            if(speed>1 && dy ==0)
                this.dx = this.dx+(speed-1)*2/8;
            else if(speed>1 && dx == 0)
                this.dy = this.dy+(speed-1)*2/9;
        }

        public boolean collision(Ball ball) {
            int dx = x - ball.x;
            int dy = y - ball.y;
            int distance = (int) Math.sqrt(dx * dx + dy * dy);
            if (distance < r + ball.r) {
                return true;
            } else {
                return false;
            }
        }
    }

    class MyPanel extends JPanel implements Runnable {

        private static final long serialVersionUID = 1L;
        boolean isStart = false;

        public void paint(Graphics g) {
            super.paint(g);
            for (int i = 0; i < balls.size(); i++) {
                Ball ball = balls.get(i);
                g.setColor(ball.color);
                g.fillOval(ball.x - ball.r, ball.y - ball.r, ball.r * 2, ball.r * 2);
            }
        }

        public void run() {
            while (isStart) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < balls.size(); i++) {
                    Ball ball = balls.get(i);
                    ball.move();
                    ball.upordown();
                    if (ball.y <= 0 || ball.y >= this.getHeight()) {
                        balls.remove(ball);
                    }
                    for (int j = 0; j < balls.size(); j++) {
                        if (i != j) {
                            Ball otherBall = balls.get(j);
                            if (ball.collision(otherBall)) {
                                balls.remove(ball);
                                balls.remove(otherBall);
                            }
                            if (ball.x == getWidth()-1)
                                balls.remove(ball);
                        }
                    }
                }
                if (balls.size() < 10) {
                    int x = rand.nextInt(this.getWidth()/3);
                    int y = rand.nextInt(this.getHeight());
                    Ball ball = new Ball(x, y, 20, Color.blue, 5,0);
                    balls.add(ball);
                }

                this.repaint();
            }
        }
    }
    public class TimeThread extends Thread{
        public void run() {
            int seconds = 0;
            while (true) {
                if (!isPaused) {
                    _time.setText(Integer.toString(seconds));
                    seconds++;
                }
                try {
                    Thread.sleep(1000/speed);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    private synchronized void start_a() {
        // 启动计时器线程
        if (timerThread == null) {
            timerThread = new TimeThread();
            timerThread.start();
        }
        isPaused = false;
        speed = 1;
        startButton.setEnabled(false);
        stopButton.setEnabled(true);
        speedupbutton.setEnabled(true);
        speeddownbutton.setEnabled(false);

    }


    private synchronized void pause() {
        // 暂停计时器线程
        isPaused = true;
        speed = 1;
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
    }
    private synchronized void up(){
        if(speed == MAX_SPEED-1){
            speed++;
            speedupbutton.setEnabled(false);
            speeddownbutton.setEnabled(true);
        }
        else {
            speed++;
            speedupbutton.setEnabled(true);
            speeddownbutton.setEnabled(true);
        }
    }
    private synchronized void down(){
        if(speed == MIN_SPEED+1){
            speed--;
            speedupbutton.setEnabled(true);
            speeddownbutton.setEnabled(false);
        }
        else {
            speed--;
            speedupbutton.setEnabled(true);
            speeddownbutton.setEnabled(true);
        }
    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值