import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
//import com.myemap.ui.Face;
public class BallFrame{
JButton jbStart;
JButton jbStop;
JFrame jf;
BallPanel bp;
public BallFrame(){
jf = new JFrame("Ball Game");
jf.setLayout(new BorderLayout());
bp = new BallPanel();
Panel buttonPanel = new Panel();
jbStart = new JButton("开始");
jbStop = new JButton("停止");
buttonPanel.add(jbStart);
buttonPanel.add(jbStop);
jf.add(buttonPanel,BorderLayout.SOUTH);
jf.add(bp,BorderLayout.CENTER);
this.actionHandler();
this.showMi();
}
private void showMi(){
jf.setSize(400,300);
//jf.pack();
Toolkit tool = Toolkit.getDefaultToolkit();
double x =tool.getScreenSize().getWidth();
double y =tool.getScreenSize().getHeight();
jf.setLocation((int)(x-jf.getWidth())/2,(int)(y-jf.getHeight())/2);
jf.setVisible(true);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void actionHandler(){
jbStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("start pressed");
/*1.创建小球
2.创建小球线程
3.加入小球*/
Ball ball = new Ball();
new BallThread(ball,bp.getBounds()).start();
bp.addBall(ball);
}
});
jbStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("stop pressed");
System.exit(0);
}
});
}
public static void main(String[] args) {
//Face.setFace();
new BallFrame();
}
}
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.util.Random;
public class Ball {
private double x,y; //初始点坐标
public static final int X_SIZE=20; //矩形宽度
public static final int Y_SIZE=20; //矩形长度
private int xMoveDis=10; //x移动距离 步长
private int yMoveDis=10; //y移动距离 步长
private Ellipse2D shape;
private Color color;
private static Random random;
static {
random = new Random();
}
public Ball(){
color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
xMoveDis = 5+random.nextInt(5)-2;
yMoveDis = 5+random.nextInt(5)-2;
}
public Shape getShape() { //返回小球形状的方法
if(shape == null){
shape= new Ellipse2D.Double(x, y , X_SIZE,Y_SIZE);
}else{
shape.setFrame(x, y , X_SIZE,Y_SIZE);
}
return shape;
}
public void move(Rectangle rect){
x+=xMoveDis;
y+=yMoveDis;
//判断边界
if(x < rect.getMinX()){
x = rect.getMinX();
xMoveDis = -xMoveDis;
}else if((x+X_SIZE)>rect.getMaxX()){
x = rect.getMaxX()-X_SIZE;
xMoveDis = -xMoveDis;
}
if(y < rect.getMinY()){
y = rect.getMinY();
yMoveDis = -yMoveDis;
}else if((y+Y_SIZE)>rect.getMaxY()){
y = rect.getMaxY()-Y_SIZE;
yMoveDis = -yMoveDis;
}
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
public class BallPanel extends JPanel{ //负责画小球
private List<Ball> balls;
public BallPanel(){
balls = new ArrayList<Ball>();
startPanitThread();
}
public void addBall(Ball ball){
balls.add(ball);
}
//每隔50ms重绘一次
private void startPanitThread(){
new Thread(){
@Override
public void run() {
while(true)
{
repaint(); //重画
try {
Thread.sleep(50); //50ms重绘一次
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 =(Graphics2D)g;
for(Ball ball: balls){
g2.setColor(ball.getColor());
g2.fill(ball.getShape()); //绘画小球
}
}
}
import java.awt.Rectangle;
import java.util.List;
public class BallThread extends Thread{ //只负责移动
private Ball ball;
private Rectangle rect;
public BallThread(Ball ball,Rectangle rect){
this.ball = ball;
this.rect=rect;
}
@Override
public void run(){
int count = 0;
while(count<10000)
{
ball.move(rect);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}
(1)框架类 界面、开始按钮(创建小球、开启线程)、暂停按钮的监听(退出系统)
(2)面板类 保存小球的列表、每隔50ms绘画小球、绘画小球
(3)小球类 小球自身的设定,小球的移动。
(4)线程类 调用小球的移动。
所谓的线程,就是为了并发执行。线程是客户端调用。