画图程序 java_一个JAVA画图程序

本文介绍了一个使用JAVA编写的画图程序,该程序可以创建一个太阳系模型,涉及AWT库和面向对象编程。程序通过继承Frame类初始化窗口,并利用线程实现动态画图效果。同时,文章提供了获取图片资源的方法,并展示了如何通过继承和重载构建Stars和Planet类,以模拟星体的运动轨迹。在主程序中,创建了太阳、地球、火星、金星和月球等星体并设置其运动参数,实现了简单的太阳系动画效果。
摘要由CSDN通过智能技术生成

近期根据视频教程搞了一个JAVA画图程序,做了一个太阳系模型的源码。涉及到一些AWT和面向对象的东西。详细来看一下吧。

先来看一下效果吧:

0818b9ca8b590ca3270a3433284dd417.png

基本的画图理论就省略了。要实现该程序关键有二个通用的程序:

一、继承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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值