本文共 3641 字,大约阅读时间需要 12 分钟。
近期根据视频教程搞了一个JAVA画图程序,做了一个太阳系模型的源码。涉及到一些AWT和面向对象的东西。详细来看一下吧。
先来看一下效果吧:
基本的画图理论就省略了。要实现该程序关键有二个通用的程序:
一、继承Frame的一个窗口初始化程序
public class MyFrame extends Frame {
public void LaunchFrame() {
setSize(Constants.FRAME_WIDTH,Constants.FRAME_HIGH);
setLocation(100,100);
setVisible(true);
setBackground(Color.BLACK);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
new RepaintThread().start();
}
class RepaintThread extends Thread { //启动一个线程反复画图,是实现主程序中的Paint()方法,实现动画效果,很关键!!!!!
public void start() {
while (true) {
repaint();
try {
sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
二、 获取图片资源(在后面的程序中反复用到)
public static Image getImage(String path) {
URL u=GameUtil.class.getClassLoader().getResource(path);
BufferedImage img=null;
try {
img=ImageIO.read(u);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}
}
主程序中使用继承和重载构建了两个类START和PLANET:
一首先Start类,主要功能是画星体。
public class Stars {
Image img;
double x,y;
int width,high;
public void draw(Graphics g) {
g.drawImage(img,(int)x,(int)y,null);
}
public Stars()
{
}
public Stars(String path,double x,double y) {
this.img=GameUtil.getImage(path);
this.width=img.getWidth(null);
this.high=img.getHeight(null);
this.x=x-width/2;
this.y=y-high/2;
}
public Stars(Image img){
this.img=img;
this.width=img.getWidth(null);
this.high=img.getHeight(null);
}
}
二、Planet类继承与Start类,主要实现星体的运行轨迹计算和围绕实现。
public class Planet extends Stars{
/**
* 星体的属性
* */
double longAxis;//长轴
double shortAxis;//短轴
double speed;//速度
double degree;//角度
Stars center;//用于计算星体的中心位置。
boolean issatelite;
public void draw(Graphics g) {
/*
* 画椭圆轨迹
* */
g.drawImage(img, (int)x,(int)y,null);
if(!issatelite){
drawTrace(g);
}
move();
}
public void move(){
x=(center.x+center.width/2-img.getWidth(null)/2)+longAxis*Math.cos(degree);
y=(center.y+center.high/2-img.getHeight(null)/2)+shortAxis*Math.sin(degree);
degree+=speed;
}
public void drawTrace(Graphics g) {
double ovalx,ovaly,ovalwidth,ovalheight;
ovalx=(center.x+center.width/2)-longAxis;
ovaly=(center.y+center.high/2)-shortAxis;
ovalwidth=longAxis*2;
ovalheight=shortAxis*2;
//避免画笔改动
Color c=g.getColor();
g.setColor(Color.RED);
g.drawOval((int)ovalx,(int)ovaly,(int)ovalwidth,(int)ovalheight);
g.setColor(c);
}
public Planet( Stars center,String path, double longAxis,
double shortAxis, double speed) {
super(GameUtil.getImage(path));
this.center = center;
this.x=center.x+longAxis;
this.y=center.y;
//this.img=GameUtil.getImage(path);
this.longAxis = longAxis;
this.shortAxis = shortAxis;
this.speed = speed;
}
public Planet( Stars center,String path, double longAxis,
double shortAxis, double speed,boolean issatelite) {
this(center,path,longAxis,shortAxis,speed);
this.issatelite=issatelite;
}
public Planet(String path, double x, double y) {
super(path, x, y);
}
}
另外在主程序中调用就可以了:
public class MySolarFrame extends MyFrame{
//Image img=GameUtil.getImage("images/sun.png");
Stars sun=new Stars("images/sun.png", Constants.FRAME_WIDTH/2, Constants.FRAME_HIGH/2);
Planet earth=new Planet(sun,"images/earth.png", 220, 180,0.01);
Planet mars=new Planet(sun,"images/3.png", 300, 250,0.02);
Planet venus=new Planet(sun,"images/Venus.png", 100, 80,0.03);
Planet moon=new Planet(earth,"images/moon.png", 60, 40,0.03,true);
public void paint(Graphics g) {
//g.drawImage(img,Constants.FRAME_WIDTH/2-64,Constants.FRAME_HIGH/2-64,null);
sun.draw(g);
earth.draw(g);
mars.draw(g);
venus.draw(g);
moon.draw(g);
}
public static void main(String[] args) {
new MySolarFrame().LaunchFrame();
}
}
程序结束,欢迎大家批评指正,附整个程序的源代码:http://page92.ctfile.com/file/140761906
转载地址:http://sgqsx.baihongyu.com/