1、效果

1.png

2、代码

(1)定义图形大小的一些常量类


package com.swing.sunModel;

/**
 * 常量类
 * @author may
 *
 */
public class Constant {
    public static final int GAME_WIDTH = 800;
    public static final int GAME_HEIGHT = 600;
    public static final int FIXED_WIDTH = 40;
    public static final int FIXED_HEIGHT = 35;

}

(2)Star.java接口类

package com.swing.sunModel;
import java.awt.Graphics;
/**
 * 行星的抽象类
 * @author may
 *
 */
public interface Star {
    public int getX();
    public int getY();    
    public int getWidth();    
    public int getHeight();    
    public void draw(Graphics g);
}

(3)FixedStar.java恒星类

package com.swing.sunModel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;

public class FixedStar implements Star {
    private int x;//x轴位置
    private int y;//y轴位置
    private int fixed_width;//物体的宽度
    private int fixed_height;//物体的高度
    private List<Star> planets = new ArrayList<Star>();//用于存储行星的容器
    private static Image image = ImageLoadUtil.loadImage("/lib/Sun.png");////恒星的图片        
    public FixedStar(int fixed_width, int fixed_height) {
        //计算出xy的值,是它居中显示
        this.x = (Constant.GAME_WIDTH - fixed_width)/2;
        this.y = (Constant.GAME_HEIGHT - fixed_height)/2;
        this.fixed_width = fixed_width;
        this.fixed_height = fixed_height;
        this.createPlanet();
    }
    /**
     * 创建围绕它转的所有行星
     */
    private void createPlanet() {
        Star earth= new Planet(100, 50, 30, 30, this, "/lib/earth.net.png", 0.09);
        planets.add(earth);
        planets.add(new Planet(20, 15, 15, 15, earth, "/lib/venus.png", 0.3, true));
        planets.add(new Planet(200, 100, 30, 30, this, "/lib/goog_mars.png", 0.08));
        planets.add(new Planet(250, 150, 30, 30, this, "/lib/uranus.png", 0.05));
        planets.add(new Planet(350, 200, 30, 30, this, "/lib/venus.png", 0.03));
    }

    /**
     * 绘画方法
     * @param g
     */
    public void draw(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.red);
        //g.fillOval(this.x, this.y, fixed_width, fixed_height);
        g.drawImage(image, this.x, this.y, fixed_width, fixed_height, null);
        g.setColor(c);
        //画出每个行星
        for(int i = 0; i < this.planets.size(); i++) {
            planets.get(i).draw(g);
        }
    }
    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 void setFixed_width(int fixed_width) {
        this.fixed_width = fixed_width;
    }
    public void setFixed_height(int fixed_height) {
        this.fixed_height = fixed_height;
    }
    @Override
    public int getWidth() {
        return fixed_height;
    }
    @Override
    public int getHeight() {
        return fixed_height;
    }
}

(4)父窗口类MyFrame.java

package com.swing.sunModel;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 定义一个父类,将常用的代码写到此处
 * @author may
 *
 */
public class MyFrame extends Frame {
    
    private static final long serialVersionUID = 8133309356114432110L;
    public MyFrame() {
        this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
        this.setLocationRelativeTo(null);                
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                MyFrame.this.dispose();
            }
        });
        this.setVisible(true);
     }    
}

(5)飞机类Planet.java

package com.swing.sunModel;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;

public class Planet implements Star {
    private int x;//行星的位置
    private int y;//行星的位置
    private int planet_width;//行星的大小
    private int planet_height;//行星的大小
    private int longXpis; //离恒星的距离,就是椭圆的长轴
    private int shortXpis;//离恒星的距离,就是椭圆的短轴
    private Star fixedStar;//恒星
    private double degree = 1;//角度
    private Image image = null;//行星的图片,由于每个行星不同,所以定义为非静态的
    private double speed = 0.01;//默认的改变角度的速度
    private List<Planet> moon = new ArrayList<Planet>();//定义行星的子行星,如地球的月亮
    private boolean satellite = false;
    public Planet(int x, int y) {
        this.x = x;
        this.y = y;
    }
    /**
     * 
     * @param longXpis 长轴
     * @param shortXpis 短轴
     * @param planet_width 行星的宽度
     * @param planet_height 行星的高度
     * @param fixedStar 中心星体,即恒星
     * @param path 图片的位置
     * @param speed 角度改变的速度
     */
  public Planet(int longXpis, int shortXpis, int planet_width, int planet_height, Star fixedStar, String path,double speed) {
        //定义离中心星体的初始距离
        this(fixedStar.getX() + fixedStar.getWidth() / 2 + longXpis - planet_width / 2,
        fixedStar.getY() + fixedStar.getHeight() / 2 - planet_height / 2);
        this.longXpis = longXpis;
        this.shortXpis = shortXpis;
        this.planet_height = planet_height;
        this.planet_width = planet_width;
        this.fixedStar = fixedStar;
        this.image = ImageLoadUtil.loadImage(path);
        this.speed = speed;
    }
    public Planet(int longXpis, int shortXpis, int planet_width, int planet_height, Star planetStar, String path,
            double speed, boolean satellite) {
        this(longXpis, shortXpis, planet_width, planet_height, planetStar, path,
                speed);
        this.satellite = satellite;
    }
    /**
     * 绘画方法
     * @param g
     */
    public void draw(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.gray);
        //画出行星图片
        g.drawImage(image, this.x, this.y, planet_width, planet_height, null);
        //画出行星的运行轨迹
        if(!satellite) {
            g.drawOval(fixedStar.getX() - (longXpis - this.planet_width / 2),
                    fixedStar.getY() - (shortXpis - this.planet_height / 2), this.longXpis * 2, this.shortXpis * 2);
            
        }
        g.setColor(c);
        //以下的代码使行星做椭圆运动
        this.x = (int) (fixedStar.getX() + longXpis * Math.cos(degree));
        this.y = (int) (fixedStar.getY() + shortXpis * Math.sin(degree));
        if(degree < 2 * Math.PI) {
            degree += speed;
            
        } else {
            degree = 0;
        }
        //画子行星
        for(int i = 0; i < moon.size(); i++) {
            moon.get(i).draw(g);
        }
    }
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
    @Override
    public int getWidth() {
        return planet_width;
    }
    @Override
    public int getHeight() {
        return planet_height;
    }        
}

(6)主类sunMode.java

package com.swing.sunModel;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
public class SunModel extends MyFrame {
    private static final long serialVersionUID = -5330545593499050676L;
    private static Image image = ImageLoadUtil.loadImage("/lib/star.jpg");
    //恒星对象
    private FixedStar fixedStar = new FixedStar(Constant.FIXED_WIDTH, Constant.FIXED_HEIGHT);
    //创建一个缓冲图,用于双缓冲,避免闪烁出现
    private BufferedImage buffImage = new BufferedImage(Constant.GAME_WIDTH, Constant.GAME_HEIGHT, BufferedImage.TYPE_INT_RGB);
    public SunModel() {
        super();
        this.setResizable(false);
        new Thread(new MyThread()).start();
    }
    
    @Override
    public void paint(Graphics g) {
        //将缓冲图直接画上去
        g.drawImage(buffImage, 0, 0, null);
    }    
    //重写update方法,进行双缓冲,将需要显示的物体全画到缓冲图上,然后一次性显示出来
    @Override
    public void update(Graphics g) {
        Graphics sunG = buffImage.getGraphics();
        //将背景图画上
        sunG.drawImage(image, 0, 0, null);
        fixedStar.draw(sunG);
        paint(g);
    }
    public static void main(String[] args) {
        new SunModel();
    }    
    private class MyThread implements Runnable {
        @Override
        public void run() {
            while(true) {
                //调用重画方法
                repaint();
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }                
    }
}

(7)图片加载辅助类ImageLoadUtil.java

package com.swing.sunModel;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageLoadUtil {

    private ImageLoadUtil() {}
    
    public static Image loadImage(String path) {
        
        BufferedImage image = null;
        
        try {
            image = ImageIO.read(ImageLoadUtil.class.getResource(path));
        } catch (IOException e) {
            e.printStackTrace();
        }        
        return image;
     }    
}