关于小球运动路径问题
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; } }}
//Ball.javaimport java.awt.Color;import java.awt.Graphics;public class Ball { int x, y; static final int SPEED = 5; Direction dir = Direction.STOP; static int cellTime = 100; Ball() { x = 0;y = 0; } public Ball(Point point) { x = point.x;y = point.y; } public Ball(int i, int j) { x = i;y = j; } void draw(Graphics g) { Color c = g.getColor(); g.setColor(Color.red); g.fillOval(x - 5, y - 5, 10, 10); g.setColor(c); } public Ball clone() { return new Ball(x, y); } 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; default: break; } }}//enum Direction.javapublic enum Direction { UP,RIGHT,DOWN,LEFT,STOP;}//Point.javapublic class Point{ int x,y; public Point(int x, int y) { this.x = x;this.y = y; } public String toString(){ return "("+x+","+y+")"; }}//MainView.javapackage projectTest;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Frame;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;public class MainView extends Frame implements Runnable{ 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 static int TIMESEP = (BLOCK_SIZE / Ball.SPEED) * Ball.cellTime; Map<Ball, String> map = null; Map<Ball, String> mapClone = null; Image offScreenImage = null; MainView(Map<Ball, String> map) { this.map = map; mapClone = new HashMap<Ball, String>(); Ball b = null; for (Entry<Ball,String> e:map.entrySet()){ b = e.getKey().clone();//拷贝起点坐标。 System.out.println(b); mapClone.put(b,e.getValue()); } System.out.println(map.size()); } public void launch() { this.setLocation(100, 100); this.setSize(800, 600); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); this.setVisible(true); for (Entry<Ball, String> e : map.entrySet()) { new Thread(new WatcherThread(e.getKey(), e.getValue())).start(); } } public static void main(String[] args) { Ball ball1 = new Ball(150, 150); Ball ball2 = new Ball(450, 450); String str1 = "11123330"; String str2 = "33301112"; Map<Ball, String> map = new HashMap<Ball, String>(); map.put(ball1, str1); map.put(ball2, str2); MainView mv = new MainView(map); mv.launch(); new Thread(mv).start(); } public void paint(Graphics g){ super.paint(g); System.out.println(mapClone.size()); for (Entry<Ball, String> e : mapClone.entrySet()) { draw(g, e); } for (Ball ball : map.keySet()) { ball.draw(g); } Color c = g.getColor(); g.setColor(Color.darkGray); 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); g.setColor(c); } public void draw(Graphics g, Entry<Ball, String> e) { Color c = g.getColor(); g.setColor(Color.green); ((Graphics2D) g).setStroke(new BasicStroke(5)); Ball b = e.getKey(); Point p = new Point(b.x, b.y); String tempStr = e.getValue(); for (int i = 0; i < tempStr.length(); i++){ Point temp = getDistance(tempStr.charAt(i)); g.drawLine(p.x, p.y, p.x = p.x + temp.x, p.y = p.y + temp.y); } g.setColor(c); ((Graphics2D) g).setStroke(new BasicStroke(1)); } public Point getDistance(char c) { switch (c) { case '0': return new Point(0, -BLOCK_SIZE); case '1': return new Point(BLOCK_SIZE, 0); case '2': return new Point(0, BLOCK_SIZE); case '3': return new Point(-BLOCK_SIZE, 0); } return new Point(0, 0); } @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 { private Ball ball; private String str = null; WatcherThread(Ball ball, String str1) { this.ball = ball; str = str1; } public void run() { char[] cs = str.toCharArray(); int timeOut = 0; while (timeOut < cs.length * TIMESEP) { try { if (timeOut % TIMESEP == 0) { ball.dir = Direction.values()[Integer.parseInt(String .valueOf(cs[timeOut / TIMESEP]))]; } Thread.sleep(Ball.cellTime); timeOut += Ball.cellTime; ball.move(); } catch (InterruptedException e) { e.printStackTrace(); } } } } @Override public void run() { while(true){ try { Thread.sleep(Ball.cellTime); repaint(); } catch (InterruptedException e) { e.printStackTrace(); } } }}
[解决办法]
str中我想给的是坐标点,比如:str=“1112222333”是指点(1,1)(1,2)(2,2)(2,3)(3,3)
-------想问下9宫格,每个点可以用你的0,1,2,3表示,你可以传List<String>进去,在转化为map。那样我的就可以简化一点了。我的str的意思是在某个方向上走了定长。
你可以用一个函数转化一下,这代码太长了。应该会把?你的str包含两个信息,其实位置可以new一个Ball出来,在转化为方向序列。
-------解决阴影问题。
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(145, 145, COLS * BLOCK_SIZE + 10, ROWS * BLOCK_SIZE + 10);
for (Entry<Ball, String> e : mapClone.entrySet()) {
draw(g, e);
}
for (Ball ball : map.keySet()) {
ball.draw(g);
}
Color c = g.getColor();
g.setColor(Color.darkGray);
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);
g.setColor(c);
}
加上红线部分就可以了。