import java.util.List;
class WatcherThread implements Runnable {
    boolean flag = true;
    private Ball ball;
    private List<Point> points;    WatcherThread(Ball ball, List<Point> points) {
        this.ball = ball;
        this.points = points;
    }    @Override
    public void run() {
        while (flag) {
           if (points.size() >= 1){ if (ball.getX()== points.get(0).getX() && ball.getY() == points.get(0).getY()&&points.size()<= 1) {//等于1说明就剩下最后一步,只要等他move完,线程就可以停止了。 flag = false; }else { if (ball.getX() == points.get(0).getX() && ball.getY() == points.get(0).getY()) { // 拿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(20);
                ball.move();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        }
    }    private Direction getDir(Point point, Point point1){//线程监听临界状况,在判断方向。
        if(point1.getX()>point.getX()){
            return Direction.LEFT;
        }
        if(point1.getY()>point.getY()){
            return Direction.UP;
        }
        if(point1.getY()<point.getY()){
            return Direction.DOWN;
        }
        if(point1.getX()<point.getX()){
            return Direction.RIGHT;
        }
        return null;
    }
}
enum Direction{//支持上下左右,你可以扩展的。
    UP,LEFT,RIGHT,DOWN;
}
class Point {
    private int x;
    private int y;
    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;
    }
}
class Ball {
    static final int SPEED=50;
    Direction dir;
    private int x;
    private int y;
    
    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;
    }
    void move(){
        //根据方向和速度,怎么move?????
    }
}
目的是让一个小球任意给定的起点,通过九宫格的一系列路径,到达终点。比如给定坐标(0,0)——(0,1)——(1,1)——(1,2)
如何实现一个小球沿指定坐标间的直线运动,从起始点运动到终点?  给位大神!看看哪里需要改的,帮忙完善以下代码!给个move函数,还有主函数!万分感激!!!

解决方案 »

  1.   


    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;
    }
    }move方法就这么简单的
      

  2.   

    注意你两点的距离要能整除speed,不然要改代码
      

  3.   


    谢谢!虽然很笨,这点我还是知道滴......还有main函数哦~~~
      

  4.   

    楼主自己看着办,写个main函数。,瞬间无语了。帮你写个。好好考虑下逻辑吧。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{
    public static final int ROWS = 30;
    public static final int COLS = 30;
    public static final int BLOCK_SIZE = 15;
    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(200, 200);
    this.setSize(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
    this.addWindowListener(new WindowAdapter() {
    @Override
    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(30,120));
    points.add(new Point(30,60));
    points.add(new Point(300,60));
    points.add(new Point(300,300));
    new MainView(points).launch();
    } @Override
    public void paint(Graphics g) {
    super.paint(g);
    Color c = g.getColor();
    g.setColor(Color.GRAY);
    g.fillRect(0, 0, COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
    g.setColor(Color.DARK_GRAY);
    // 画出横线
    for (int i = 1; i < ROWS; i++) {
    g.drawLine(0, BLOCK_SIZE * i, COLS * BLOCK_SIZE, BLOCK_SIZE * i);
    }
    for (int i = 1; i < COLS; i++) {
    g.drawLine(BLOCK_SIZE * i, 0, BLOCK_SIZE * i, BLOCK_SIZE * ROWS);
    }
    g.setColor(c);
    ball.draw(g);
    } @Override
    public void update(Graphics g) {// 双缓冲
    if (offScreenImage == null) {
    offScreenImage = this.createImage(COLS * BLOCK_SIZE, ROWS
    * BLOCK_SIZE);
    }
    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.green);
    g.fillOval(x-10, y-10, 20, 20);
    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;
    }
    }
    }