package com.Joney.thread;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Random;
public class BallGame extends Frame{
int WIDTH=600,HEIGHT=400;
Ball ball =new Ball(10,30,3,2,false,false,Color.BLUE,this);
Ball ball2 =new Ball(20,60,5,2,false,false,Color.ORANGE,this);
//存放小球的集合,使用泛型
java.util.List<Ball> balls =new ArrayList<Ball>();
Label label =new Label();
public BallGame(){
setSize(WIDTH,HEIGHT);
setTitle("点击F键添加小球");
balls.add(ball);
balls.add(ball2);
//匿名内部类,实现关闭窗体
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
this.addKeyListener(new MyKeyListener());
label.setText("目前小球个数:"+balls.size());
add(label,BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
BallGame bg =new BallGame();
Thread bt=new BallThread(bg);
bt.start();
}
@Override
public void paint(Graphics g) {
//增强for循环,遍历小球集合,将每个小球都绘制出来
for(Ball b:balls){
b.draw(g);
}
}
class MyKeyListener extends KeyAdapter{
Random r =new Random();
@Override
public void keyPressed(KeyEvent arg0) {
//如果点击“F”键,则相应
if(arg0.getKeyCode()==KeyEvent.VK_F){
createNewball();
}
}
public synchronized void createNewball(){
//随即出现的位置
int x=r.nextInt(WIDTH-50);
int y=r.nextInt(HEIGHT-50);
//随即速度
int xs=r.nextInt(5)+1;
int ys=r.nextInt(5);
//随即颜色
Color c =new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
Ball b =new Ball(x,y,xs,ys,false,false,c,BallGame.this);
balls.add(b);
label.setText("目前小球个数:"+balls.size());
}
}
}
class BallThread extends Thread{
BallGame map;
public BallThread(BallGame bg){
map=bg;
}
@Override
public void run() {
while (true){
//增强for循环,遍历小球集合,将每个小球都移动一次
for(Ball b:map.balls){
b.move();
}
map.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}/*===========================================================================================================*/
package com.Joney.thread;
import java.awt.Color;
import java.awt.Graphics;
public class Ball {
int x,y;//x y坐标
int xs,ys;//x轴y轴速度
boolean xb,yb;//是否到地图边缘
Color color;//颜色
BallGame map;//小球所 在 地图
public Ball(){}
public Ball(int x, int y, int xs, int ys, boolean xb, boolean yb,
Color color,BallGame map) {
super();
this.x = x;
this.y = y;
this.xs = xs;
this.ys = ys;
this.xb = xb;
this.yb = yb;
this.color = color;
this.map=map;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getXs() {
return xs;
}
public void setXs(int xs) {
this.xs = xs;
}
public int getYs() {
return ys;
}
public void setYs(int ys) {
this.ys = ys;
}
public boolean isXb() {
return xb;
}
public void setXb(boolean xb) {
this.xb = xb;
}
public boolean isYb() {
return yb;
}
public void setYb(boolean yb) {
this.yb = yb;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public void move(){
//判断是否到达边缘
if(x>(map.WIDTH-10))xb=true;
if(x<5)xb=false;
if(y>(map.HEIGHT-35))yb=true;
if(y<10)yb=false;
//运动
if(!xb)x+=xs;//x坐标为x方向的速度
else x-=xs;
if(!yb)y+=ys;//x坐标为x方向的速度
else y-=ys;
}
public void draw(Graphics g){
Color c =g.getColor();
g.setColor(getColor());
g.fillArc(x, y, 10, 10, 0, 360);
g.setColor(c);
}
}
|
|