Java 使月球绕地球旋转

Java 使月球绕地球旋转,java,jpanel,Java,Jpanel,我在创造一个简单的太阳系。地球绕着太阳转。现在我正在努力让月球绕地球转,同时让地球绕太阳转。 谢谢你的帮助! 地球代码: import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Earth extends JPanel implements ActionListener{ Timer t=ne

我在创造一个简单的太阳系。地球绕着太阳转。现在我正在努力让月球绕地球转,同时让地球绕太阳转。 谢谢你的帮助! 地球代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Earth extends JPanel implements ActionListener{
    Timer t=new Timer(100,this);
    private int x;
    private int y;
    private int height;
    private int width;
    private Color color;
    double angle;
    
    public Planetet(int x,int y,int height,int width,Color color) {
    this.x=x;
    this.y=y;
    this.height=height;
    this.width=width;
    this.color=color;}
        
    
    
     public void paint(Graphics g) {
            //Extra Code for Screen clearing
            g.setColor(getBackground());
            boolean rotation=true;

            int a, b;
            if (rotation) {
                int width = getWidth();
                int height = getHeight();
                a = (int) (Math.cos(angle) * (width / 3) + (width / 2));
                b = (int) (Math.sin(angle) * (height / 3) + (height / 2));
            } else {
                a = getWidth()/2 - gjatsia/2;
                b = getHeight()/2 - gjersia/2;
            }

            g.setColor(ngjyra);
            g.fillOval(a, b, gjatsia, gjersia);
            t.start();
        }
    public void actionPerformed(ActionEvent e) {
        angle+=0.1/2;
        if (angle>(2*Math.PI))
        angle=0.1;
        repaint();}
        
    
    
    
    public static void main (String [] args) {
        
        JFrame MainFrame=new JFrame();
        MainFrame.getContentPane().setBackground( Color.black );
        MainFrame.setSize(600,600);
        Sun sun=new Sun(250,250,50,50,Color.YELLOW);
        Earth earth=new Earth(400,270,20,20,Color.blue);
        Moon moon=new Moon(400,270,10,10,Color.GRAY);

        MainFrame.add(sun);
        MainFrame.setVisible(true);

        MainFrame.add(earth);

        MainFrame.setVisible(true);
        MainFrame.add(moon);

        MainFrame.setVisible(true);
    }



    

}
太阳代码:

import javax.swing.*;
import java.awt.*;
public class Sun extends JComponent{
    
    private int x;
    private int y;
    private int height;
    private int width;
    private Color color;
    double angle;
    
    public Sun(int x,int y,int height,int width,Color color) {
    this.x=x;
    this.y=y;
    this.height=height;
    this.width=width;
    this.color=color;}
        
    
    
    public void paint(Graphics g) {
        

        g.setColor(color);
        g.fillOval(x, y, height, width);
    }}
月球代码(需要更正):


这样想:一切都围绕着某个东西旋转——A围绕B旋转——B围绕C旋转——C围绕D旋转。。。如何在代码中表示这种关系?也许通过给每个物体一个它所旋转的物体的参照

此外,如果您有多个具有相同或非常相似行为的对象,则可以将此行为抽象到父(或抽象)类中,这样可以避免多次编写相同的代码


希望这能帮助您思考代码。

这不是堆栈溢出的工作方式。我们不会帮你完成家庭作业。请参加,访问并阅读,学习如何有效地使用此网站。@JimGarrison不是我的家庭作业,但欢迎任何帮助。顺便说一句,谢谢你的建议!很抱歉,在最后一天发布了几个几乎相同的问题。这强烈暗示这是一个类分配,有几个人来这里找人为他们编写代码。@JimGarrison啊,好的。我只是想在如何连接月球和地球并围绕地球旋转的想法上得到帮助!!
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Moon extends JComponent implements ActionListener{
    Timer t=new Timer(100,this);
    private int x;
    private int y;
    private int height;
    private int width;
    private Color color;
    double angle;

    public Moon(int x,int y,int height,int width,Color color) {
    this.x=x;
    this.y=y;
    this.height=height;
    this.width=width;
    this.color=color;}

    
    public void paint(Graphics g) {
        int width =getWidth();
        int height=getHeight();
         int a = (int) (Math.cos(angle) * (width/6) + (width/4));
         int b = (int) (Math.sin(angle) * (height/6) + (height/4));

        g.setColor(ngjyra);
        g.fillOval(a, b, gjatsia, gjersia);
        t.start();
    }
    
    public void actionPerformed(ActionEvent e) {
        angle-=0.5/2;
        if (angle>(2*Math.PI))
        angle=0.0;
        repaint();}}