Como posso mover um objeto até um local, percorrendo pelo caminho mais rápido?
Também estou tentando desenhar cada movimento com a classe Graphics2D e usando MouseListener
(Imagem mostra a movimentação do ponto B até o ponto A, movendo-se pelo caminho mais rápido)
Código:
public class NovoClass extends JFrame {
JLabel pontoA = new JLabel(".");
JLabel pontoB = new JLabel(".");
public NovoClass() {
pontoA.setBounds(50, 50, 50, 50);
this.add(pontoA);
pontoB.setBounds(500, 300, 50, 50);
this.add(pontoB);
Timer(5, pontoA, pontoB).start();
}
public Timer Timer(int tempo, Component pontoA, Component pontoB) {
Timer timer = new Timer(tempo, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (pontoA.getX() < pontoB.getX()) {
pontoB.setLocation(pontoB.getX() - 1, pontoB.getY());
}
if (pontoA.getY() < pontoB.getY()) {
pontoB.setLocation(pontoB.getX(), pontoB.getY() - 1);
}
}
});
return timer;
}
}
