日期:2014-05-20 浏览次数:21023 次
package view;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
public class MainView extends Frame{
private static final long serialVersionUID = 4090565292134499038L;
public static final int ROWS = 3;
public static final int COLS = 3;
public static final int BLOCK_SIZE = 100;
public static int N = 100;
public static int M = 50;
public String str1 = "111221......"; //此处,我准备储存把小球的路径的坐标写成字符串的形式赋给str
public String str2 = "111221......";
Ball ball = null;
Image offScreenImage = null;
List<Point> points = null;
MainView(List<Point> points){
this.points = points;
}
public void launch(){
ball = new Ball(this.points.get(0));
this.setLocation(100,100);
this.setSize(800,600);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
new Thread(new WatcherThread(ball,points,this)).start();
}
public static void main(String[] args) {
List<Point> points = new ArrayList<Point>();
points.add(new Point(150,150));//此处的Point 值我想通过读取str中的数据来赋值
points.add(new Point(150,250));//另外就是str中的坐标长度可能不一样,所需的point也就不一样,看看怎么改
points.add(new Point(250,250));
points.add(new Point(250,350));
points.add(new Point(150,350));
points.add(new Point(150,450));
new MainView(points).launch();
}
public void paint(Graphics g) {
super.paint(g);
g.drawLine(1*N+M,1*N+M,1*N+M,4*N+M);
g.drawLine(2*N+M,1*N+M,2*N+M,4*N+M);
g.drawLine(3*N+M,1*N+M,3*N+M,4*N+M);
g.drawLine(4*N+M,1*N+M,4*N+M,4*N+M);
g.drawLine(1*N+M,1*N+M,4*N+M,1*N+M);
g.drawLine(1*N+M,2*N+M,4*N+M,2*N+M);
g.drawLine(1*N+M,3*N+M,4*N+M,3*N+M);
g.drawLine(1*N+M,4*N+M,4*N+M,4*N+M);
ball.draw(g);
}
@Override
public void update(Graphics g) {// 双缓冲
if (offScreenImage == null) {
offScreenImage = this.createImage(500,500);
}
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
private class WatcherThread implements Runnable {
boolean flag = true;
private Ball ball;
private List<Point> points;
private MainView main;
WatcherThread(Ball ball,List<Point> points,MainView main) {
this.ball = ball;
this.points = points;
this.main = main;
this.flag = true;
}
public void run(){
while (flag) {
if (points.size()>=1){
if (ball.x == points.get(0).x
&& ball.y == points.get(0).y
&& points.size()==1){
flag = false;
}else{
if (ball.x == points.get(0).x&& ball.y == points.get(0).y){// 拿Point.get(0)那个Point.get(1)出来比较,就可以确定方向。
Direction dir = getDir(points.get(0), points.get(1));
ball.dir = dir;
points.remove(0);
}
}
}else {
flag = false;
}
try {
Thread.sleep(100);
ball.move();
main.repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private Direction getDir(Point point, Point point1) {// 线程监听临界状况,在判断方向。
if (point1.x > point.x){
return Direction.RIGHT;
}
else if (point1.y > point.y) {
return Direction.DOWN;
}
else if (point1.y < point.y) {
return Direction.UP;
}
else if (point1.x < point.x){
return Direction.LEFT;
}
return Direction.RIGHT;
}
}
}
enum Direction {//支持上下左右,你可以扩展的。
UP, LEFT, RIGHT, DOWN;
}
class Point{
public Point(int x, int y) {
this.x = x;
this.y = y;
}
int x;
int y;
}
class Ball {
static final int SPEED = 5;
Direction dir;
int x;
int y;
Ball(){
x = 0;
y = 0;
dir = Direction.RIGHT;
}
public Ball(Point point) {
this.x = point.x;
this.y = point.y;
}
public int getX() {
return x;
}
void draw(Graphics g){
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(x-5, y-5, 10, 10);
g.setColor(c);
}
void move(){
switch(this.dir){
case UP:
this.y = y - SPEED;
break;
case DOWN:
this.y = y + SPEED;
break;
case LEFT:
this.x = x - SPEED;
break;
case RIGHT:
this.x = x + SPEED;
break;
}
}
}