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;
}
}
}各位大神,能否帮忙把这段程序改一下!有以下要求:
1.把程序中路径和小球的运动分离开,先根据坐标点返回给出小球的路径(主要是方向 UP, LEFT, RIGHT, DOWN的序列),然后再让小球根据路径运动(可以让小球每一的方向都运动一个固定定时间把该段路径走完,之后走下一个方向,从而实现小球沿路径的运动);
2.要把小球扩展成两个,因为要给两条指定的坐标点(可以分别放到str1,str2中),然后先分别返回小球路径的方向序列,再让两个小球同时按方向序列运动;
3. points.add(new Point(x,y));//此处的Point 值我想通过读取str1(或str2)中的数据来赋值,另外就是str中的坐标序列长度可能不一样,所需的point个数也就不一样,看看怎么改注:其实,这个程序就是给出两条坐标路径,让两个小球同时沿不同路径运动,只是要求需要先返回路径的方向序列,再让小球运动;另外就是,给出的坐标路径如何给 points.add(new Point(x,y));此处赋值,特别注意坐标路径要从str中获取,但给出的str1,str2又是不定的,所以看看如何用!
谢谢各位!希望能给出详细代码!我是新手,水平实在有限!
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;
}
}
}各位大神,能否帮忙把这段程序改一下!有以下要求:
1.把程序中路径和小球的运动分离开,先根据坐标点返回给出小球的路径(主要是方向 UP, LEFT, RIGHT, DOWN的序列),然后再让小球根据路径运动(可以让小球每一的方向都运动一个固定定时间把该段路径走完,之后走下一个方向,从而实现小球沿路径的运动);
2.要把小球扩展成两个,因为要给两条指定的坐标点(可以分别放到str1,str2中),然后先分别返回小球路径的方向序列,再让两个小球同时按方向序列运动;
3. points.add(new Point(x,y));//此处的Point 值我想通过读取str1(或str2)中的数据来赋值,另外就是str中的坐标序列长度可能不一样,所需的point个数也就不一样,看看怎么改注:其实,这个程序就是给出两条坐标路径,让两个小球同时沿不同路径运动,只是要求需要先返回路径的方向序列,再让小球运动;另外就是,给出的坐标路径如何给 points.add(new Point(x,y));此处赋值,特别注意坐标路径要从str中获取,但给出的str1,str2又是不定的,所以看看如何用!
谢谢各位!希望能给出详细代码!我是新手,水平实在有限!
====固定时间怎么衡量?
因为我接收到的路径坐标会是String 字符串,需要从字符串中提取坐标,所以用str另外,固定的时间能不能定义一个static类型的固定值,现实应用中,根据九宫格的大小,调整该固定值?
//Ball.java
import 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.java
public enum Direction {
UP,RIGHT,DOWN,LEFT,STOP;
}
//Point.java
public class Point{
int x,y;
public Point(int x, int y) {
this.x = x;this.y = y;
}
public String toString(){
return "("+x+","+y+")";
}
}
//MainView.java
package 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的第一个点为小球起始位置)?还有,str1,str2中的坐标是何种策略,为什么我如果输str1="1121223233"画出的绿色区域超出九宫格?返回值用UP,RIGHT,DOWN,LEFT,STOP的序列好吗?
-------想问下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);
}
加上红线部分就可以了。
谢谢!我还想再加一个 方向 叫做WAIT,如果为WAIT 就让小球等一个小球运动的单位线段时间,再按后边的方向序列运动