JAVA写的 太阳系模型编程

到目前为止写的比较难理解的一个程序了,但是也是思路最清晰的一次;
有些小感悟和思考也穿插其中了,还有一些其他的,比如一个方法最好只做一件事,父类子类之间的调用方式还有一个类各个方法之间的调用,帮助我了解了很多;
写出来和大家分享一下。

太阳系主窗口

package cn.Solar;

import java.awt.Graphics;
import java.awt.Image;

import Util.Constant;
import Util.GameUtil;
import Util.MyFrame;

public class SolarFrame extends MyFrame {
	/**
	 * 太阳系主窗口
	 */
	//Image img=GameUtil.getImage("images/xiaoqiu1.jpg");
	Image bj=GameUtil.getImage("images/bj.jpg");
	Star sun = new Star("images/sun1.jpg", Constant.WIGTH/2, Constant.HEIGHT/2); 
	Planet earth = new Planet(sun, "images/earth.jpg", 150, 50, 0.1);
	Planet mars = new Planet(sun, "images/mars.jpg", 200, 70, 0.3);
	Planet moon = new Planet(earth, "images/moon(1).jpg", 30, 15, 0.2,true);
	
	public void paint(Graphics g) {
		g.drawLine(100, 100, 200, 200);
		g.drawImage(bj, 0, 0,null);
		sun.draw(g);
		earth.draw(g);
		mars.draw(g);
		moon.draw(g);
	}
	
	
	public static void main(String[] args) {
		SolarFrame f = new SolarFrame();
		f.launchFrame();
		
	}
	

}

加载窗口

package Util;

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


public class MyFrame extends Frame {	
	/**
	 * 父类:加载窗口
	 */

	public void launchFrame() {
		
		this.setTitle("哈哈");
		this.setVisible(true);
		this.setSize(Constant.WIGTH, Constant.HEIGHT);
		this.setLocation(100, 100);
		
		new PaintThread().start();//启动重画教程
		
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			};
		});	
	}	
	
	class PaintThread extends Thread{
		 public void run() {
			while(true) {
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

工具箱类

package Util;

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

import javax.imageio.ImageIO;

public class GameUtil {
	
	//常用的工具:如加载图片
	//下面即为加载图片的代码
	
	private GameUtil() {};//工具箱最好把构造器私有了。
	
    public static Image getImage(String path) {
    	URL u = GameUtil.class.getClassLoader().getResource(path);
    	BufferedImage img = null;
    	try {
			img = ImageIO.read(u);
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return img;
    }
}

常量类

package Util;
/*
 * 游戏中的常数
*/
public class Constant {
      public  static final int WIGTH = 600;
      public  static final int HEIGHT = 350;
}

中间的主星类(如果是太阳系即为太阳)

package cn.Solar;

import java.awt.Graphics;
import java.awt.Image;

import Util.GameUtil;

public class Star{
	double x,y;
	double height,width;
	Image img;
	
	public Star(Image img) {
		this.img = img;
		this.height = img.getHeight(null);	
		this.width = img.getWidth(null);
	}
	
	public void draw(Graphics g){//把自己给画出来
		g.drawImage(img, (int)x,(int)y, null);
	}
	
	public Star(Image img,double x,double y) {//来一个构造器
		this(img);
		this.y=y;
		this.x=x;
		
		
	}
	
	public Star() {//父类的空构造器
	}
	
	public Star(String imgpath,double x,double y) {//重构一个构造器,传过来路径就可以了;
		this(GameUtil.getImage(imgpath),x,y);//通过this调用另一个构造方法
		
		
	}
}

绕主星运行的行星类(包括卫星)

package cn.Solar;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import Util.GameUtil;

public class Planet extends Star{
	//除了图片坐标,行星沿着某个椭圆运行,长轴,短轴,速度,角度,行星绕着谁飞
	
	double longAxis;
	double shortAxis;
	double speed;
	double degree;
	Star center;
	
	boolean satellite;
	
	public void draw(Graphics g){        //沿着椭圆轨迹飞行
		super.draw(g);
		
		move();
		
		//如果是卫星就不要轨迹了
		if(!satellite) {
			drawTrace(g);
		}
	}
	
	public void drawTrace(Graphics g) {
		double ovalX,ovalY,ovalWidth,ovalHeight;
		
		ovalWidth = longAxis*2;
		ovalHeight = shortAxis*2;
		ovalX = (center.x+center.width/2)-longAxis;
		ovalY=(center.y+center.height/2)-shortAxis;
		
		Color c=g.getColor();
		g.setColor(Color.blue);
		g.drawOval((int)ovalX, (int)ovalY, (int)ovalWidth,(int)ovalHeight);
		g.setColor(c);
	}
	
	public void move() {
		//x=center.x + longAxis*Math.cos(degree);
		//y=center.y + shortAxis*Math.sin(degree);
		
		x=(center.x+center.width/2)-width/2+ longAxis*Math.cos(degree);
		y=(center.y+center.height/2)-height/2+ shortAxis*Math.sin(degree);
		degree+=speed;
	}

	 //子类构造器默认继承父类空构造器,所以父类必须来个构造器
	
	public Planet(Star center,String imgpath,double longAxis, double shortAxis, double speed) {
		
		super(GameUtil.getImage(imgpath));//这里调用父类的构造方法,
		//父类的构造方法中有this.img=img;所以后面的注释掉的就在这里定义了,不需要再次定义了;
		
		this.center = center; 
		
		
		//this.x=(center.x+center.width/2) +longAxis;
		//this.y=(center.y+center.height/2)+shortAxis;
		//这里与上面两行注释掉的是之前的,下面的是之后的;
		//注释前后的变化是,注释前x,y表示的是planet的坐标(本质上应该是坐标的,但是按坐标运转
		//不是很漂亮),注释后x,y代表的是planet的中心,这样跑起来比较美观;想是真的按轨迹跑的;
		
		
		this.x=(center.x+center.width/2) +longAxis-width/2;
		this.y=(center.y+center.height/2)+shortAxis-height/2;
		//this.img=GameUtil.getImage(imgpath);
		
		this.longAxis = longAxis;
		this.shortAxis = shortAxis;
		this.speed = speed;
	}

	
public Planet(Star center,String imgpath,double longAxis, 
		double shortAxis, double speed,boolean satellite) {
		this(center, imgpath, longAxis, shortAxis, speed);
		this.satellite = satellite;
		
	}


	public Planet(Image img, double x, double y) {
		super(img, x, y);
	}

	public Planet(String imgpath, double x, double y) {
		super(imgpath, x, y);
	}
}

下面附上结构图
上面程序的结构图

猜你喜欢

转载自blog.csdn.net/qq_43356439/article/details/83722381