弹弹球

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

弹弹球的相关文章

java学习日记-----------------------------弹弹球

好玩的小程序,弹弹弹.................... import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; public class BallsCanvas extends Canvas implements ActionListener,FocusListener{ private Ball balls[]; private Timer timer;//

关于processing弹弹球

这些是关于弹弹球的过程,而制作弹弹球我们肯定首先要画一个圆,接着还要设置碰到边框就反弹等程序,还可以加上背景颜色和球的颜色,更好看 原文地址:https://www.cnblogs.com/wanyuehan/p/10333944.html

是男人就坚持20秒java的swing弹弹球小游戏代码

package com.day09.am01; import java.awt.Color; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.util.Timer; import ja

是男人就坚持20秒 java的swing弹弹球GAME

package com.day09.am01; import java.awt.Color; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.util.Timer; import ja

c语言 弹弹球小游戏

#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <time.h>#include<mmsystem.h>#pragma comment(lib, "WINMM.LIB") #define BRICK_NUM 100//形状类型#define SQUARE 1#define STAR 2#define DIAMOND 3 /*建立对应模型:20/

如何完成一个弹弹球

float circle_x = 200;float circle_y = 10;float move_x = 2;float move_y = 2; void setup(){ size(500,300);}void draw(){ background(225); ellipse(circle_x,circle_y,50,50); circle_x=circle_x+move_x; circle_y=circle_y+move_y; if(circle_x>width){ circle_x=

如何通俗易懂地解释遗传算法?有什么例子?

遗传算法,核心是达尔文优胜劣汰适者生存的进化理论的思想. 我们都知道一个种群,通过长时间的繁衍,种群的基因会向着更适应环境的趋势进化,牛B个体的基因被保留,后代越来越多,适应能力低个体的基因被淘汰,后代越来越少.经过几代的繁衍进化,留下来的少数个体,就是相对能力最强的个体了. 那么在解决一些问题的时候,我们能不能学习这样的思想,比如先随机创造很多很多的解,然后找一个靠谱的评价体系,去筛选比较好的解,再用这些好的解像生小宝宝一样生一堆可能更好的解,然后再筛再生,反复弄个几代,得到的说不定就是近似最

2015年要做的150件事

在上一篇<再见2014,你好2015>里也算是总结了一下2014年的生活和展望了一下2015年的愿景,只是对2015年的目标定的有些模糊,还把一些事情统称了一个目标,这样做的一个坏处就是实现一个目标很难,而且没有动力,不能从完成的目标中获得成就感来激励自己前进.元旦的时候正好也看到了<泰囧>的电影,王宝强去泰国旅行,他把每件事都具体到完成一件就划掉一件.因此这几天我也重新整理和规划了一下自己的目标,让自己的目标更加的具体,不是给哪个人看,只是为了督促自己: 1. 个人博客 完成1.

[我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之摄像机介绍Cameras

[我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之摄像机介绍Cameras 最近得到一些Unity官方视频教程,一看全是纯英文的讲解,没有任何字幕或者讲稿之类的东西.虽然基本上看的懂,但很难听清其中一些细致的知识点.于是产生了做中文字幕的想法.本篇分享一下首个已完工的视频,即<beginner Graphics – Lessons系列之摄像机介绍Cameras>.听译.时间轴.压制者均为本人.请勿将视频用于非法目的. 优酷视频好像不太清晰,有需