package cn.hncu.p24; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.Timer; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class BallsJFrame extends JFrame implements ChangeListener, ActionListener { BallsCanvas ballscanvas; JSpinner spinner; JButton btn; Color color; public BallsJFrame() { super("弹弹球"); Dimension dms = getToolkit().getScreenSize(); setBounds(dms.width / 4, dms.height / 4, dms.width / 2, dms.height / 2); setDefaultCloseOperation(EXIT_ON_CLOSE); Color[] colors = { Color.red, Color.green, Color.cyan, Color.blue, Color.black }; ballscanvas = new BallsCanvas(colors, 100); getContentPane().add(ballscanvas); northLayout(); setVisible(true); } private void northLayout() { JPanel p2 = new JPanel(); getContentPane().add(p2, BorderLayout.SOUTH); spinner = new JSpinner(); spinner.setValue(100); spinner.addChangeListener(this); btn = new JButton("添加彩球"); btn.addActionListener(this); p2.add(btn); p2.add(spinner); } public static void main(String[] args) { new BallsJFrame(); } @Override public void stateChanged(ChangeEvent e) { int delay = (Integer) spinner.getValue(); ballscanvas.timer.setDelay(delay); } @Override public void actionPerformed(ActionEvent e) { color = JColorChooser.showDialog(this, "颜色选择", Color.red); ballscanvas.pulsBall(new Ball(color, ballscanvas)); } } class BallsCanvas extends Canvas implements FocusListener, ActionListener { Ball[] balls; Timer timer; int delay; int width; int hight; public BallsCanvas(Color[] colors, int delay) { balls = new Ball[colors.length]; this.setSize(200, 300); getDimension();// 得到宽和高 for (int i = 0; i < colors.length; i++) { balls[i] = new Ball(colors[i], this); } // 使用定时器 timer = new Timer(delay, this);// 注册actionListemer监听器********************** timer.start(); this.addFocusListener(this); } public void pulsBall(Ball ball) { Ball[] temp = new Ball[balls.length + 1]; for (int i = 0; i < balls.length; i++) { temp[i] = balls[i]; } temp[balls.length] = ball; balls = temp; repaint(); } public void getDimension() { width = this.getWidth(); hight = this.getHeight(); } @Override public void paint(Graphics g) { for (int i = 0; i < balls.length; i++) { g.setColor(balls[i].color); balls[i].x = balls[i].left ? balls[i].x - 5 : balls[i].x + 5; if (balls[i].x <= 0 || balls[i].x >= this.getWidth()) { balls[i].left = !balls[i].left; } balls[i].y = balls[i].up ? balls[i].y - 5 : balls[i].y + 5; if (balls[i].y <= 0 || balls[i].y >= this.getHeight()) { balls[i].up = !balls[i].up; } g.fillOval(balls[i].x, balls[i].y, balls[i].r, balls[i].r); for (int j = 0; j < balls.length; j++) { if (i != j) { if ((Math.sqrt((balls[i].x - balls[j].x) * (balls[i].x - balls[j].x) + (balls[i].y - balls[j].y) * (balls[i].y - balls[j].y))) <= balls[i].r) { balls[i].left = !balls[i].left; balls[i].up = !balls[i].up; balls[j].left = !balls[j].left; balls[j].up = !balls[j].up; } } } } } @Override public void focusGained(FocusEvent e) { timer.stop(); } @Override public void focusLost(FocusEvent e) { timer.restart(); } @Override public void actionPerformed(ActionEvent e) { repaint(); } } class Ball { int x = 1, y = 1, r = 20; boolean up, left; Color color; public Ball(Color color, BallsCanvas ballscanvas) { int row = (int) (Math.random() * 200) % (ballscanvas.width); int column = (int) (Math.random() * 300) % (ballscanvas.hight); this.x = x * row; this.y = y * column; up = left = false; this.color = color; } }
时间: 2024-10-09 23:03:13