简体   繁体   中英

how to draw a moving animation line between two different points

I'm doing a project about linking immobile points with an animated point. I'm using this and I want to do it with a random number of points which are located randomly. The source code is use for only 2 locations I want to do it with all points like I take the x and y from point 1. I want it to go to other location point 2 and point 3 for example.

PS: I'm a french and not an English speaker. Sorry for the mistakes and I'm new in java.

Panneau.java

package animation;

/**
*
* @author ilies
*/
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Panneau extends JPanel {
 private int posX = -50;
  private int posY = -50;
  public void paintComponent(Graphics g){
    //On choisit une couleur de fond pour le rectangle
    g.setColor(Color.white);
    //On le dessine de sorte qu'il occupe toute la surface
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
    //On redéfinit une couleur pour le rond
    g.setColor(Color.red);
    //On le dessine aux coordonnées souhaitées
    g.fillOval(posX, posY, 50, 50);
  }
  public int getPosX() {
   return posX;
   }
  public void setPosX(int posX) {
    this.posX = posX;
  }
  public int getPosY() {
    return posY;
  }
  public void setPosY(int posY) {
    this.posY = posY;
  }
}  

Fenetre.java

package animation;

/**
 *
 * @author ilies
 */
 import java.awt.Dimension; 
 import javax.swing.JFrame;

public class Fenetre extends JFrame{
  private Panneau pan = new Panneau();
  public Fenetre(){        
   this.setTitle("Animation");
   this.setSize(300, 300);
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   this.setLocationRelativeTo(null);
   this.setContentPane(pan);
   this.setVisible(true);
   go();
   }
   private void go(){

    for(int i = -50; i < pan.getWidth(); i++){
      int x = pan.getPosX(), y = pan.getPosY();
      x++;
      y++;
      pan.setPosX(x);
      pan.setPosY(y);
      pan.repaint();  
      try {
        Thread.sleep(10);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }       
}

Animation.java

package animation;

/**
*
* @author ilies
*/
public class Animation {

   /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
       Fenetre fen = new Fenetre();
       // TODO code application logic here
    }

}

You really need some sort of an interpolation method. Your current code is merely translating across the screen - this is rather different than intentionally moving between two points.

Here is a very simple implementation. A more robust solution (ie for constant speed) would use a speed parameter and the change in time between frames.

public Point interpolate(Point start, Point end, double fraction) {
    int dx = end.x - start.x;
    int dy = end.y - start.y;

    int newX = (int) (start.x + dx * fraction);
    int newY = (int) (start.y + dy * fraction);

    return new Point(newX, newY);
}

Also, you need some random points to interpolate between.

ArrayList<Point> pointList = new ArrayList<>();

Random rand = new Random();

for (int i = 0; i < 2 + rand.nextInt(10); i++) {
   pointList.add(new Point(rand.nextInt(pan.getWidth()), 
                           rand.nextInt(pan.getHeight())));   
}

Finally, you need to replace your animation loop in Fenetre.go() with this:

while (true) {
    // Change the speed by changing the delta value
    for (double i = 0.0; i <= 1.0; i += 0.05) {
        Point p = interpolate(pointList.get(0), pointList.get(1), i);
        pan.setPosX(p.x);
        pan.setPosY(p.y);

        pan.repaint();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // Push the first point to the back of the list and repeat
    pointList.add(pointList.remove(0));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM