Hallo,
ich habe ein Problem mit der Bewegung auf meinem Radar.
Der Kreis der sich bewegt ist bei einer hohen Steigung zu schnell und man sieht nicht mehr die Bewegung. Hat jemand eine Idee wie man das lösen kann, sodass immer die gleiche Bewegungsgeschwindigkeit vollzogen wird?
import javax.swing.*;
import java.awt.*;
public class Animation {
private int startx = 300;//Startpunkt
private int starty = 300;//Startpunkt
private int zielx = 100;//Zielpunkt
private int ziely = 440;//Zielpunkt
public static void main(String[] args){
Animation a = new Animation();
a.go();
}
public void go(){
JFrame frame = new JFrame();
frame.setSize(577, 597);
frame.setResizable(false);
MyPanel panel = new MyPanel();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
double m = calculateAlterationRate(startx, starty, zielx, ziely); //Steigung einer Geraden.
//Routenberechnung(FEHLERHAFT!!)
//Der If-Block macht überhaupt nichts richtig.
if (Math.abs(m) < 1){
double reciprocal = 1/m;
if(ziely >= starty){
while(!(zielx-10 <= startx && startx <= zielx+10) || !(ziely-10 <= starty && starty <= ziely)){
startx = (int)Math.round(startx - reciprocal);
starty++;
panel.repaint();
try{
Thread.sleep(200);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
else{
while(!(zielx-10 <= startx && startx <= zielx+10) || !(ziely-10 <= starty && starty <= ziely)){
startx = (int)Math.round(startx - reciprocal);
starty--;
panel.repaint();
try{
Thread.sleep(200);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
//Dieser Else-Block hier scheint(!) korrekt zu laufen. Zumindest habe ich noch kein Gegenbeispiel gefunden.
else{
if(zielx >= startx){
while(!(zielx-10 <= startx && startx <= zielx+10) || !(ziely-10 <= starty && starty <= ziely)){
starty = (int)Math.round(m + starty);
startx++;
panel.repaint();
try{
Thread.sleep(200);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
else{
while(!(zielx-10 <= startx && startx <= zielx+10) || !(ziely-10 <= starty && starty <= ziely)){
starty = (int)Math.round(starty - m);
startx--;
panel.repaint();
try{
Thread.sleep(200);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
private double calculateAlterationRate(int startx,int starty,int zielx,int ziely){
return (ziely - starty)/(zielx - startx); //Steigung einer Gerade
}
class MyPanel extends JPanel {
//der Radar mit einem Kreis
public void paint(Graphics g){
g.setColor(Color.gray);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(new Color((float)0.2, (float)0.2, (float)0.2));
g.fillOval(0, 0, 570, 570);
g.setColor(Color.black);
g.fillOval(35, 35, 500, 500);
g.setColor(Color.white);
g.drawOval(35, 35, 500, 500);
g.setColor(new Color((float)0.0, (float)1.0, (float)0.5));
drawCircles(g);
g.setColor(Color.white);
g.fillOval(startx, starty, 20, 20);
}
private void drawCircles(Graphics g){
g.drawOval(235, 235, 100, 100);
g.drawOval(185, 185, 200, 200);
g.drawOval(135, 135, 300, 300);
g.drawOval(85, 85, 400, 400);
}
}
}