刚学java,老师带着我们一起写得一个小程序,设置十个小球,每个球的大小,颜色,位置,运动方向等都让它随机,然后碰到框架的边框时反弹,反弹之后小球颜色变为另一种随机颜色。做完这个,我想如果能够解决键盘控制这个面板上的坐标的话,就可以加上一个小木板,这样就做成了小时候经常玩的弹球游戏了,等后面再慢慢研究这个东西。
当中运动的轨迹的边界的处理,要考虑到画小球的那个横纵坐标是指小球外接方形的左上角坐标,所以要注意设置好参数。
下面是运动小球的代码:
首先是这个Ball类的代码:
package day04;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class Ball {
//四个方向
public final static int LEFT_UP=0;
public final static int LEFT_DOWN=1;
public final static int RIGHT_UP=2;
public final static int RIGHT_DOWN=3;
//球的属性
private int x;
private int y;
private int r;
private int direction;
private Color color;
private int speed;
private JPanel panel;
public void changeColor(){
//改变当前小球的颜色为随机颜色
Random ran=new Random();
this.setColor(new Color(
ran.nextInt(256),ran.nextInt(256),ran.nextInt(256)));
}
public void draw(Graphics g){
//画一个圆
g.setColor(color);
g.fillOval(x, y, 2*r, 2*r);
}
public void move(){
//小球的运动方法
switch(direction){
case LEFT_UP:
x--;y--;
if(x<=0) {direction=RIGHT_UP;changeColor();}
if(y<=0) {direction=LEFT_DOWN;changeColor();}
break;
case LEFT_DOWN:
x--;y++;
if(x<=0) {direction=RIGHT_DOWN;changeColor();}
if(y>=panel.getHeight()-2*r){direction=LEFT_UP;changeColor();}
break;
case RIGHT_UP:
x++;y--;
if(x>=panel.getWidth()-2*r){direction=LEFT_UP;changeColor();}
if(y<=0) {direction=RIGHT_DOWN;changeColor();}
break;
case RIGHT_DOWN:
x++;y++;
if(x>=panel.getWidth()-2*r) {direction=LEFT_DOWN;changeColor();}
if(y>=panel.getHeight()-2*r) {direction=RIGHT_UP;changeColor();}
break;
}
}
//各函数的构造方法
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 getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void setPanel(JPanel panel) {
this.panel = panel;
}
}
然后是这个MyBallFrame框架的代码:
package day04;
import javax.swing.JFrame;
public class MyBallFrame {
public static void main(String [] args) {
//设置框架的各类属性
JFrame frame=new JFrame();
frame.setSize(800,600);
frame.setTitle("一个运动的小球");
frame.setLocation(100,50);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyBallPanel panel= new MyBallPanel();
frame.add(panel);
panel.startRun();
frame.setVisible(true);
}
}
最后是这个MyBallPanel的面板代码:
package day04;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class MyBallPanel extends JPanel{
private Ball[]balls=new Ball[10];
public MyBallPanel(){
//构造方法
Random ran=new Random();
for(int i=0;i<balls.length;i++){
//初始化10个小球的属性值
Ball ball=new Ball();
ball.setR(10+5*i);
ball.setX(ran.nextInt(800-2*ball.getR()-10));
ball.setY(ran.nextInt(600-2*ball.getR())-10);
ball.setColor(new Color(
ran.nextInt(256),ran.nextInt(256),ran.nextInt(256)));
ball.setDirection(ran.nextInt(4));
ball.setSpeed(3+ran.nextInt(20));
ball.setPanel(this);
balls[i]=ball;
}
}
public void paint(Graphics g){
//画出10个园
super.paint(g);
for(int i=0;i<balls.length;i++){
balls[i].draw(g);
}
}
public void startRun(){
//kaishi
for(int i=0;i<balls.length;i++){
final int temp=i;
new Thread(){
public void run(){
while(true){
balls[temp].move();
try {
sleep(balls[temp].getSpeed());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
}.start();
}
}
}