月亮绕地球转

//Sky.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Sky extends JLabel implements ActionListener {   //标签类JLabel的子类--刻画天空
   Earth earth;                            
   Timer timer;                                               //计时器
   double pointX[]=new double[360],                           //double型数组pointX刻画水平坐标-地球相对于天空的
          pointY[]=new double[360];                           //double型数组pointY刻画垂直坐标-地球相对于天空的
   int w=400,h=400,i=0;
   Sky() {
     timer=new Timer(100,this);                               //创建timer--振铃间隔是100毫秒--当前Sky对象为其监视器
     earth = new Earth();                                     //构造了一个地球(实际上是一个标签对象)
     earth.setSize(200,200);                                  //地球(标签对象)大小为200*200
     add(earth);                                              //地球位于刻画天空的标签里(标签也可以是容器)
     pointX[0]=0;                                             //地球运动轨道的半径h/2
     pointY[0]=h/2; 
     double angle=1*Math.PI/180;                                          //刻度为1度-弧度 
     for(int i=0;i<359;i++) {                                             //计算出数组中各个元素的值--圆上的坐标点
       pointX[i+1]=pointX[i]*Math.cos(angle)-pointY[i]*Math.sin(angle);   //以圆中心为(0,0),第一个点为(0,h/2),顺时
       pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle);   //针旋转1弧度后的坐标
     }
     for(int i=0;i<360;i++) {           
       pointX[i]=0.5*pointX[i]+w/2;                                       //坐标缩放平移--将圆中心变为(w/2,h/2)
       pointY[i]=0.5*pointY[i]+h/2;                                       //轨道圆大小缩小1倍
     }
     timer.start();                                                       //计时器启动--每隔100毫秒就会触发ActionEvent
   }
   public void actionPerformed(ActionEvent e) {
       i=(i+1)%360;                                                       //0~359循环变化
       earth.setLocation((int)pointX[i]-100,(int)pointY[i]-100);          //设置earth对象(标签)在sky对象(标签)上的位置
   }
}

//Earth.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Earth extends JLabel implements ActionListener {  //标签类JLabel的子类--刻画地球
   JLabel moon;                                                //标签类--刻画(显示)月亮的外观
   Timer timer;                                                //计时器
   double pointX[]=new double[360],                            //double型数组pointX刻画水平坐标-月亮相对于地球的        
          pointY[]=new double[360];                            //double型数组pointY刻画垂直坐标-月亮相对于地球的
   int w=200,h=200,i=0;
   Earth() {
     timer=new Timer(20,this);//【代码1】                                 //创建timer,振铃间隔是20毫秒当前Earth对象为其监视器    
     setIcon(new ImageIcon("earth.jpg"));                      //设置地球(标签)的图标为earth.jpg
     setHorizontalAlignment(SwingConstants.CENTER);                       //设置地球(标签)对齐方式为居中
     moon=new JLabel(new ImageIcon("moon.jpg"),SwingConstants.CENTER);    //构造月亮(标签)对象
     moon.setSize(60,60);                                                 //设置月亮(标签)大小
     add(moon);//【代码2】                                                           //月亮(标签)放到刻画地球的标签对象里
     pointX[0]=0;                                                         //月亮运动轨道的半径h/2
     pointY[0]=h/2;
     double angle=1*Math.PI/180;                                          //刻度为1度 
     for(int i=0;i<359;i++) {                                             //计算出数组中各个元素的值--圆上的坐标点
       pointX[i+1]=pointX[i]*Math.cos(angle)-pointY[i]*Math.sin(angle);   //以圆中心为(0,0),第一个点为(0,h/2),顺时
       pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle);   //针旋转1弧度后的坐标
     }
     for(int i=0;i<360;i++) {           
       pointX[i]=0.8*pointX[i]+w/2;                                       //坐标缩放平移--将圆中心变为(w/2,h/2)    
       pointY[i]=0.8*pointY[i]+h/2;                                       //轨道圆大小缩小1倍
     }
     timer.start();//【代码3】                                                       //计时器启动--每隔100毫秒就会触发ActionEvent
   }
   public void actionPerformed(ActionEvent e) { 
       i=(i+1)%360;                                                       //0~359循环变化
       moon.setLocation((int)pointX[i]-30,(int)pointY[i]-30);             //设置moon对象(标签)在earth对象(标签)上的位置
   }
}

//MainClass.java

import javax.swing.*;
public class MainClass {
      public static void main(String args[]) {
      Sky sky= new Sky();                                            //构造了一个天空(标签对象)
      JFrame frame = new JFrame();                                   //构造了一个框架(窗体)
      frame.setTitle("月亮绕地球转");
      frame.add(sky);                                                //将天空(标签)置于框架(窗体)里
      frame.setSize(400,300);
      frame.setVisible(true);
      frame.setLocationRelativeTo(null);                             //设置窗体的位置--居中
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().setBackground(java.awt.Color.white);    //将窗体转换为一个容器并设置其背景色为白色
    }
}

earth.jpg       moon.jpg

原文地址:https://www.cnblogs.com/liao-pxsoftware15/p/7523849.html