java有颜色的小弹球_Java之练手篇---经典Swing小弹球游戏

1.小球类

package teacherBall;

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;

private int yMoveDis=10;

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;

}

}

2.主界面

package teacherBall;

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();

}

}

3.小球面板类

package teacherBall;

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 balls;

public BallPanel(){

balls = new ArrayList();

startPanitThread();

}

public void addBall(Ball ball){

balls.add(ball);

}

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());

}

}

}

4.小球线程类

package teacherBall;

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++;

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值