1、介绍
本案例中使用的知识点如下:
-
GUI
-
线程
-
单例模式
-
面向对象六大原则
2、案例代码
2.1、球体
/**
* 主题:球体
* 作者:AT阿宝哥
* 时间:2016年8月24日
* 描述:
*
*
* 注意:
*
*/
package com.company.project.sample.s01;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JPanel;
//类:球体
public class Ball extends JPanel implements Runnable, MouseListener {
// 定义球体编号
private String id;
// 定义球体在“球桌”里的x,y坐标
private int x;
private int y;
// 定义球体在x,y方向的可移动的范围
private int deskWidth;
private int deskHeight;
// 定义球体移动的方向
private String direction;
// 定义球体颜色
private Color ballColor;
// 定义球体移动开关
private boolean canMove;
// 构造方法
public Ball(String id, int deskWidth, int deskHeight, Color deskBackColor) {
super();
this.id = id;
this.x = new Random().nextInt(deskWidth);
this.y = new Random().nextInt(deskHeight);
this.deskWidth = deskWidth;
this.deskHeight = deskHeight;
// 随机产生运动方向
changeDirection();
// 注册监听器
this.addMouseListener(this);
// 设置球体颜色
this.ballColor = new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
(int) (Math.random() * 255));
// 设置球体可移动
this.canMove = true;
// 设置面板背景色
this.setBackground(deskBackColor);
// 设置面板初始位置和大小
this.setBounds(x, y, 50, 50);
}
// 重写方法:线程体
@Override
public void run() {
System.out.println(id + "已启动...");
while (true) {
if (canMove) {
move();
} else {
break;
}
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(id + "已结束...");
}
// 方法:球体移动
private void move() {
// 记录原来的方向
String originalDirection = direction;
// 判断并设置移动方向
if (y <= 0) {
// 移动到“----上边----”的情况
if (originalDirection == "左上") {
direction = "左下";
} else if (originalDirection == "右上") {
direction = "右下";
}
}
if (x >= this.deskWidth - this.getSize().width) {
// 移动到“----右边----”的情况
if (originalDirection == "右上") {
direction = "左上";
} else if (originalDirection == "右下") {
direction = "左下";
}
}
if (y >= this.deskHeight - this.getSize().height) {
// 移动到“----下边----”的情况
if (originalDirection == "左下") {
direction = "左上";
} else if (originalDirection == "右下") {
direction = "右上";
}
}
if (x <= 0) {
// 移动到“----左边----”的情况
if (originalDirection == "左下") {
direction = "右下";
} else if (originalDirection == "左上") {
direction = "右上";
}
}
switch (direction) {
case "右下":
this.setLocation(x++, y++);
break;
case "左下":
this.setLocation(x--, y++);
break;
case "左上":
this.setLocation(x--, y--);
break;
case "右上":
this.setLocation(x++, y--);
break;
default:
break;
}
}
// 重写方法:重绘圆形
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(ballColor);
g.fillOval(0, 0, 50, 50);
// System.out.println(id + "被重画了...");
}
// 方法:停止移动
public void stopMove() {
this.canMove = false;
}
// 方法:改变方向
private void changeDirection() {
int dice = (new Random()).nextInt(5);
if (dice == 1) {
this.direction = "右下";
} else if (dice == 2) {
this.direction = "左下";
} else if (dice == 3) {
this.direction = "左上";
} else if (dice == 4) {
this.direction = "右上";
} else {
this.direction = "右下";
}
System.out.println(id + "改变方向了...");
}
@Override
public void mouseClicked(MouseEvent event) {
changeDirection();
}
@Override
public void mouseEntered(MouseEvent event) {
changeDirection();
}
@Override
public void mouseExited(MouseEvent event) {
changeDirection();
}
@Override
public void mousePressed(MouseEvent event) {
changeDirection();
}
@Override
public void mouseReleased(MouseEvent event) {
changeDirection();
}
}