您的位置:首页 > 编程语言 > Java开发

太阳系模型小项目(JAVA)

2015-11-17 17:37 519 查看
学了一段时间的JAVA_SE,跟着JAVA视频做一个太阳系模型

效果图:当然,实际它是动的


首先是定义我自己的一个窗口类,不单单这个项目可以用,以后也可以

代码:

//package cn.zhang.modelutil;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
*
* @author zyq_zhang 定义我的窗口
*/
public class MyFrame extends Frame {
private static final long serialVersionUID = 1L;
protected MyFrame(String s){
super(s);
}
public void launchFrame() {
this.setSize(Constant.FRAME_WIDETH, Constant.FRAME_HIGHTH);
this.setLocation(100, 4);
this.setVisible(true);
//设置监听,关闭窗口
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new FrameThread().start();//开启线程
}
/**
*
* 重画!!
*
*/
class FrameThread extends Thread {
public void run() {
while (true) {//死循环,不断重画
repaint();
try {
Thread.sleep(40);//间隔40ms画
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

}
下一个是工具类(工具类一般将构造方法私有),这里主要是加载图片

代码:

package cn.zhang.modelutil;

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(){}//将构造方法私有
/**
* 加载图片,所有图片需在项目里建一个文件夹或者包存起来,图片类Image
*/
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 cn.zhang.modelutil;
/**
*
* @author zyq_zhang
* 常量类,包含模型的常量数据
*
*/
public class Constant {
public static final int FRAME_WIDETH=900;   //窗口宽度
public static final int FRAME_HIGHTH=730;   //窗口高度
}
星球类

代码:

package cn.zhang.modelRealization;

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

import cn.zhang.modelutil.GameUtil;

/**
*
* @author zyq_zhang 星球类
*/
public class Star {
Image img;// 星球的图片
double x, y;
int width, height;

public Star() {
}

public Star(Image img, double x, double y) {
this.img = img;
this.x = x;
this.y = y;
this.width = img.getWidth(null);
this.height = img.getHeight(null);
}

public Star(String imgpath, double x, double y) {
this(GameUtil.getImage(imgpath), x, y);
}

public Star(Image img) {
this.img = img;
this.width = img.getWidth(null);
this.height = img.getHeight(null);
}

public void draw(Graphics g) {
g.drawImage(img, (int) x, (int) y, null);
}
}
行星类

代码:

package cn.zhang.modelRealization;

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

import cn.zhang.modelutil.GameUtil;

/**
*
* 行星类,继承星球类
*/
public class Planet extends Star {

double longAxis;// 长轴
double shortAxis;// 短轴
double speed;// 速度
double degree;// 角度
Star center;// 绕着那个星球飞
boolean satellite;//是否是行星,默认是,true表示不是行星

public Planet(String Imgpath, double longAxis, double shortAxis, double speed, double degree, Star center) {

this.longAxis = longAxis;
this.shortAxis = shortAxis;
this.speed = speed;
this.degree = degree;
this.center = center;
}

public Planet() {

}

public Planet(Star center, String imgpath, double longAxis, double shortAxis, double speed) {
super(GameUtil.getImage(imgpath));
this.center = center;
this.x = center.x + longAxis;
this.y = center.y;

this.longAxis = longAxis;
this.shortAxis = shortAxis;
this.speed = speed;

/*this.width = img.getWidth(null); this.height = img.getHeight(null);*/

}

public Planet(Star center, String imgpath, double longAxis, double shortAxis, double speed, boolean satellite) {

this(center, imgpath, longAxis, shortAxis, speed);
this.satellite = satellite;
}

public void draw(Graphics g) {
super.draw(g);
starmove();
if (!satellite) {
this.drawTrace(g);
}
}

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

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

private 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 starmove() {
// 沿着椭圆轨迹飞行
x = center.x + center.width / 2 + longAxis * Math.cos(degree);
y = center.y + center.height / 2 + shortAxis * Math.sin(degree);
degree += speed;
}

}
主代码:

注意: 在Java编写具有连贯变化的窗口程序时,通常的办法是在子类中覆盖父类的paint(Graphics)方法,在方法中使用实现窗口重绘的过程。连贯变换的窗口会不断地调用update(Graphics)函数,该函数自动的调用paint(Graphics)函数。这样就会出现闪烁的情况。

为了解决这一问题,可以应用双缓冲技术。可以通过截取上述过程,覆盖update(Graphics)函数,在内存中创建一个与窗口大小相同的图形,并获得该图形的图形上下文(Graphics),再将图片的图形上下文作为参数调用paint(Graphics)函数(paint(Graphics)中的GUI函数会在图片上画图),再在update(Graphics)函数调用drawImage函数将创建的图形直接画在窗口上。

package cn.zhang.modelRealization;

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

import cn.zhang.modelutil.Constant;
import cn.zhang.modelutil.GameUtil;
import cn.zhang.modelutil.MyFrame;

/**
*
* @author zyq_zhang 太阳系主窗口
*
*/
public class SolarFrame extends MyFrame {
private Image iBuffer;
private Graphics gBuffer;
SolarFrame(String s) {
super(s);
}

private static final long serialVersionUID = 1L;

Image bg = GameUtil.getImage("Picture\\Background_image.jpg");// 背景图片
Star Sun = new Star("Picture\\sun.jpg", Constant.FRAME_WIDETH / 2, Constant.FRAME_HIGHTH / 2);
Planet mercury = new Planet(Sun, "Picture\\Mercury.jpg", 110, 50, 0.3);// 水星
Planet venus = new Planet(Sun, "Picture\\Venus.jpg", 150, 90, 0.4);// 金星
Planet earth = new Planet(Sun, "Picture\\earth.jpg", 190, 130, 0.1);// 地球
Planet mars = new Planet(Sun, "Picture\\Mars.jpg", 230, 170, 0.3);// 火星
Planet jupiter = new Planet(Sun, "Picture\\Jupiter.jpg", 270, 210, 0.4);// 木星
Planet saturn = new Planet(Sun, "Picture\\Saturn.jpg", 310, 250, 0.3);// 土星
Planet uranus = new Planet(Sun, "Picture\\Uranus.jpg", 340, 290, 0.2);// 天王星
Planet neptune = new Planet(Sun, "Picture\\Neptune.jpg", 380,330, 0.1);// 海王星
Planet moon = new Planet(earth, "Picture\\moon.jpg", 30, 20, 0.2, true);// 月球

public void paint(Graphics g) {
Color c = g.getColor();
g.drawImage(bg,0,0,null);
Sun.draw(g);
mercury.draw(g);
venus.draw(g);
earth.draw(g);
mars.draw(g);
jupiter.draw(g);
saturn.draw(g);
uranus.draw(g);
neptune.draw(g);
moon.draw(g);
this.setVisible(true);
g.setColor(c);
}
//JAVA双缓冲  ,防止屏幕闪烁
public void update(Graphics scr)
{
if(iBuffer==null)
{
iBuffer=createImage(this.getSize().width,this.getSize().height);
gBuffer=iBuffer.getGraphics();
}
gBuffer.setColor(getBackground());
gBuffer.fillRect(0,0,this.getSize().width,this.getSize().height);
paint(gBuffer);
scr.drawImage(iBuffer,0,0,this);
}
public static void main(String[] args) {
new SolarFrame("太阳系模型").launchFrame();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: