用于简单轨道运动的Java动画无法正常工作

我正在尝试模拟和动画以圆周运动绕恒星运行的行星。这是我的行星课(是的,我知道太阳不是行星):

public class Planet extends JPanel{
    //planet starting coordinates
    private int x; // this is the x coordinate
    private int y; // this is the y coordinate
    private int planetRadius; // this is the radius of the planet

//constructor sets where planet is
public Planet(int x,int y,int radius) {
    this.x = x;
    this.y = y;
    this.planetRadius = radius;
}

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;
}

public int getRadius() {
    return this.planetRadius/2;
}}

这是我的TwoVector类:

public class TwoVector {

//laying out member variables
private double x; //private variables to prevent direct access
private double y;

//constructor tells java to create a TwoVector object with member variables x,y,z
public TwoVector(double x,double y){
    this.x = x;
    this.y = y;
}
//method calculates and returns magnitude of our object
double magnitude() {
    return Math.sqrt(x*x + y*y);    
}



//method returns the components of our vector as a string
public String toString() {                  
    String xString = String.valueOf(x);     
    String yString = String.valueOf(y);     
    return "("+xString+","+yString+")";        
}
//calculates dot product of 2 TwoVector objects
public static double scalarProduct(TwoVector vector1,TwoVector vector2) {
    double dotVar = vector1.x*vector2.x + vector1.y*vector2.y;
    return dotVar;
}



//calculates angle in radians,between our 2 vectors,returns angle in degrees
public static double angle(TwoVector vector1,TwoVector vector2) throws Exception {
    double dotVar = TwoVector.scalarProduct(vector1,vector2);
    double magVar1 = vector1.magnitude();
    double magVar2 = vector2.magnitude();
    if (magVar1 == 0 && magVar2 == 0) {
        throw new Exception("magVar1 and magVar2 cannot be zero");
    }
    if (magVar1 == 0) {
        throw new Exception("magVar1 cannot be zero");
    }
    if (magVar2 == 0) {
        throw new Exception("magVar2 cannot be zero");
    }
    double cosThetaRad = dotVar / (magVar1*magVar2);
    double angleRad = Math.acos(cosThetaRad);
    return angleRad;
}}

这是我的主要课程:

public class SolarSystem extends JPanel implements actionListener{

Planet Sun = new Planet(400,300,50);
Planet Mars = new Planet(Sun.getX() + Sun.getRadius() + 70,20);


//SETS SIZE OF THE WINDOW
public Dimension getPreferredSize() {
    return new Dimension(800,600);
}

//paints object when it is updated
//g is referred to to draw things on the screen
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    //background color
    g.setColor(Color.DARK_GRAY);
    g.fillRect(0,800,600);

    //drawing a single planet
    g.setColor(Color.CYAN);
    //this makes x,y the centre of the planet
    g.fillOval(Mars.getX() - Mars.getRadius(),Mars.getY() - Mars.getRadius(),2*Mars.getRadius(),2*Mars.getRadius());

    //drawing the sun
    g.setColor(Color.YELLOW);
    //x,y are centre of sun
    g.fillOval(Sun.getX() - Sun.getRadius(),Sun.getY() - Sun.getRadius(),2*Sun.getRadius(),2*Sun.getRadius());

}

//this method animates the thing

//2D vector that is simply a horizontal line
TwoVector BaseLine = new TwoVector(1,0);
//distance between centre of planet and centre of sun
int orbitalRadius = Mars.getX() - Sun.getX();

public void actionPerformed(actionEvent e) {
    //2D vector from the sun to the planet
    TwoVector SunToPlanet = new TwoVector(Mars.getX() - Sun.getX(),Mars.getY() - Sun.getY());
    //angle between SunToPlanet vector and the BaseLine horizontal vector
    double angle = 0;
    try{
        //calculates current angle between vectors
        angle = TwoVector.angle(SunToPlanet,BaseLine);
        //generates new angle by adding 0.1 radians each time
        angle = angle + 0.1;
        System.out.println(angle);
    }
    catch(Exception er) {
        System.out.println(er);
    }
    //calculates new X and Y coordinates from new angle and casts them to int
    long newXD = Math.round(orbitalRadius*Math.cos(angle) + Sun.getX());
    int newX = (int) newXD;
    long newYD = Math.round(orbitalRadius*Math.sin(angle) + Sun.getY());
    int newY = (int) newYD;
    //sets planet to new x,y coordinates
    Mars.setX(newX);
    Mars.setY(newY);
    repaint();
}


public static void main(String[] args) throws Exception{

    SolarSystem SolarAnimation = new SolarSystem();



    //create the window to store the SolarAnimation
    JFrame window = new JFrame("Solar System");
    //add the SolarAnimation onto the window
    window.add(SolarAnimation);
    //make the window the right size
    window.pack();
    //set default close operation
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);

    //timer to run the actionListener
    //this animates our SolarSystem
    Timer t = new Timer(100,SolarAnimation);
    t.start();
}}

该动画对于0到pi之间的角度效果很好,但是它停止工作,并且一旦“角度”低于0或高于3.18左右时,“行星”就会抖动。如果我将“角度=角度+ 0.1”更改为“角度=角度-0.1”,动画也会在原地抖动。

请帮助。我已经在这里呆了几个小时了。

libailiang 回答:用于简单轨道运动的Java动画无法正常工作

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2909870.html

大家都在问