Java小白初学,试写了一下基础的行星绕行模型

我不知道咋个实现8个小球的绕中间大圆绕行,最多只能让8个小球移动一下位置,百度都百度了两天了,希望有哪个大哥可以帮我瞅瞅,指出问题所在,指点我该如何完善代码呀。

 

这是主体框架

package Star;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class runStar extends JFrame {
    public static final int STAR_WIDTH=1400;
    public static final int STAR_HEIGHT=900;

    private JButton start_btn;
    private JButton stop_btn;

    private myCanvas ms;

    public runStar(){
        this.setTitle("八个球球转圈圈!");
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds((d.width-STAR_WIDTH)/2,(d.height-STAR_HEIGHT)/2,STAR_WIDTH,STAR_HEIGHT);
        this.setLayout(null);
        ms=new myCanvas();
        this.add(ms);
        start_btn = new JButton("开始转圈圈");
        start_btn.setBounds(1260,150,100,55);
        start_btn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                ms.start();
            }
        });
        this.add(start_btn);
        stop_btn = new JButton("停止转圈圈");
        stop_btn.setBounds(1260,250,100,55);
        stop_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ms.stop();
            }
        });
        this.add(stop_btn);
    }
}

这是画布

package Star;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class myCanvas extends Canvas {
    public static final int MYCAVAS_WIDTH = 1200;
    public static final int MYCAVAS_HEIGHT = 850;
    private planet aner;
    private sun su;
    private Timer timer;

    public myCanvas() {
        this.setBackground(new Color(40, 174, 255));
        this.setBounds(0, 0, MYCAVAS_WIDTH, MYCAVAS_HEIGHT);
        timer = new Timer(200, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                aner.move();
                repaint();
            }
        });
        aner = new planet();
        su = new sun();
    }

    public void start() {
        timer.start();
    }

    public void stop() {
        timer.stop();
    }

    public void paint(Graphics g) {//方法覆盖,且自动调用
        su.paint(g);
        aner.paint(g);
    }
}

这是中心球

package Star;

import java.awt.*;

public class sun extends Point {
    public static final int WIDTH=100;
    public int centerx=500;
    public int centery=400;
    public void paint(Graphics g){
        g.setColor(Color.RED);
        g.fillOval(centerx,centery, 100, 100);
    }
}

这是8个小球

package Star;

import java.awt.*;
import java.util.Random;

import static java.lang.Math.PI;

public class planet extends Point {
    public static final int WIDTH = 30;
    private static Random rand = new Random();
    public int y;
    public int x=535 ;
    public double degree=2*PI;
    private sun ss=new sun();


    public void paint(Graphics g) {
        int distance=335;
        for (int i = 0; i <= 7; i++) {
            g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
            g.fillOval(x, distance, WIDTH, WIDTH);
            distance-= 45;
        }
    }

    public void move() {
            x = (int) (ss.centerx+50+ss.WIDTH/2 + WIDTH * Math.cos(degree));
            y = (int) (ss.centery+50+ss.WIDTH/2 + WIDTH * Math.sin(degree));
    }
}

主方法

package Star;

public class  mainx {
    public static void main(String[] args) {
        new runStar().setVisible(true);
    }
}

请大佬帮忙看看吧,救救孩子的自信心吧。感谢

查看全部
qq_46620285
我爱吃布丁
2020/12/05 18:31
  • intellij-idea
  • 开发语言
  • 点赞
  • 收藏
  • 回答
    私信

0个回复