Java小案例(行星移动)

Java小案例

行星移动:参考:三百集

使用软件:idea2017,java

1,图片集:这里  (idea图片源放在target目录下,才能访问到),建议从小往上看。。。

2,定义MyFrame

package my.university;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyFrame extends Frame {

    public void launchFram(){

        setSize(Content.Game_With,Content.Game_High);
        setLocation(100,100);
        setVisible(true);

        new PaintThread().start();

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
   private class  PaintThread extends Thread{
        public void run(){
            while(true){
                repaint();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
   }

}

2,定义ImageUtil类:

package my.university;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

public class ImageUtil {
    private ImageUtil(){}//工具类构成私有的,?

    public static Image getImage(String path){
        URL url=ImageUtil.class.getResource(path);
        /*System.out.println(ImageUtil.class.getClassLoader().getResource(""));
        result is file:/d:/xxxxx/sparkDemo/sparksql/target/classes/*/
        BufferedImage image =null;
        try {
            image=ImageIO.read(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }
}

3,定义常量类,相当于配置文件

package my.university;
public class Content {
    public static final int Game_With=500;
    public static final int Game_High=500;
}

4,定义Start类

package my.university;

import java.awt.*;

public class Start {
    public double x;
    public double y;
    public double with;
    public double height;
    Image image;

    public Start(){}//默认构造起,用于子类继承

    public Start(Image image){
        this.image=image;
        this.with=image.getWidth(null);
        this.height=image.getHeight(null);
    }
    public Start(Image image,double x,double y){
        this(image);
        this.x=x;
        this.y=y;
    }
    public Start(String path,double x,double y){
        this(ImageUtil.getImage(path),x,y);
    }

    public void draw(Graphics g){
        g.drawImage(image,(int)x,(int)y,null);
    }

}

5,定义Plant类

package my.university;

import java.awt.*;

/**
 * Created by Administrator on 2017/8/12.
 */
public class Plant extends Start {
    double longAxis;  //椭圆的长轴
    double shortAxis;  //椭圆的短轴
    double speed;     //飞行的速度
    double degree;
    Start center;
    boolean statellite;

    public Plant(){}
    public Plant(Start center,String path,double longAxis,double shortAxis,double speed){
        super(ImageUtil.getImage(path));

        this.center=center;
        this.x=center.x+longAxis;
        this.y=center.y;

        this.longAxis=longAxis;
        this.shortAxis=shortAxis;
        this.speed=speed;;
    }
    public Plant(Start center,String path,double longAxis,double shortAxis,double speed,Boolean statellite){
       this(center, path, longAxis, shortAxis, speed);
       this.statellite=statellite;
    }

    public Plant(Image image){
        super(image);
    }
    public Plant(Image image,double x,double y){
        super(image, x, y);
    }

    //画轨迹
    public void drawTrace(Graphics g){
        double ovalX,ovalY,ovalWidth,ovalHeight;

        ovalWidth = longAxis*2;
        ovalHeight = shortAxis*2;
        ovalX = (center.x+center.with/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+center.with/2+longAxis*Math.cos(degree);
        y=center.y+center.height/2+shortAxis*Math.sin(degree);
        degree+=speed;
    }

    public void draw(Graphics g){
        super.draw(g);
        move();
        if(!statellite){
            drawTrace(g);
        }

    }

}

6,定以调用类

package my.university;

import java.awt.*;
public class SolarFrame extends MyFrame {
    Image bg=ImageUtil.getImage("images/bg.jpg");
    Start sun=new Start("images/sun.jpg",Content.Game_With/2,Content.Game_High/2);
    Plant mercurys = new Plant(sun, "images/Mercury.jpg", 50, 30, 0.2);
    Plant venus = new Plant(sun, "images/Venus.jpg", 60, 40, 0.3);
    Plant earth=new Plant(sun,"images/Earth.jpg",100,50,0.4);
    Plant mars = new Plant(sun, "images/Mars.jpg", 120, 60, 0.5);
    Plant jupiter = new Plant(sun, "images/jupiter.jpg", 140, 70, 0.6);
    Plant saturn =new Plant(earth,"images/Saturn.jpg",160,80,0.7,true);
    Plant uranus = new Plant(sun, "images/Uranus.jpg", 180, 90, 0.8);
    Plant neptune = new Plant(sun, "images/Neptune.jpg", 200, 100, 0.9);
    Plant moon = new Plant(earth, "images/moon.jpg", 30, 20, 0.3,true);
    public void paint(Graphics g){
        g.drawImage(bg,0,0,null);

        sun.draw(g);
        mercurys.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);
    }
    public static void main(String[] args){
        new SolarFrame().launchFram();
    }
}

最后附上代码

时间: 2024-10-23 12:49:38

Java小案例(行星移动)的相关文章

Java小案例-(逃离迷宫)

一,迷宫需求描述: 1,用户输入迷宫图(限制方形):字母1位墙,0为通,e为出口,m为入口,*为已访问的位置,用外围1围住迷宫 2,运行轨迹右,左,下,上 3,判断该迷宫是否能从入口走到出口,并将搜索过程输出 二,迷宫实现: 1,迷宫元素类MazeCell: package smalldemo.maze; class MazeCell { public int x,y; public MazeCell(){ } public MazeCell(int x,int y){ this.x=x; th

Java小案例——判断所给年份是平年还是闰年

要求:  *  判断用户输入的年份是平年还是闰年 实现代码: import java.util.Scanner; /** * 要求: * 判断用户输入的年份是平年还是闰年 * @author Administration * */ public class Judge { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入一个年份:&quo

Java小案例——对字符串进行加密解密

 要求:  *  对用户输入的每个字符的值进行加密,将解密后的字符串输出  *  对用户输入的已加密字符串进行解密并输出 实现代码: import java.util.Scanner; /** * 要求: * 1.对用户输入的每个字符的值进行加密,将解密后的字符串输出 * 2.对用户输入的已加密字符串进行解密并输出 * @author Administration * */ public class Encryption { public static void main(String[] ar

Java小案例——判断用户输入的月份的季节

 要求:  *  根据用户输入的月份来判断该月季节 实现代码: import java.util.Scanner; /** * 要求: * 根据用户输入的月份来判断该月季节 * @author Administration * */ public class JudgeSeason { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入一个

Java小案例——交换两个数值的三种方法

要求: 互换两个数的值 方法一:借助第三方变量 /** * 借助第三方变量对两个值进行互换 * @author Administration * */ public class ExchangeValue { public static void main(String[] args) { int a = 10; int b = 15; System.out.println("a的值:"+a+",\tb的值:"+b); System.out.println(&quo

Java小案例——使用双重for循环实现杨辉三角的输出

杨辉三角特点分析(如图): *第i行有i列 *每一行的第一个数都为1 *每一行的最后一个数都为1 *当前数(非第一列和最后一列)等于上面一个数+上面一个数的左边的数 实现代码: /** * 要求:输出杨辉三角 * @author Administration * */ public class YangHuiTest { public static void main(String[] args) { //创建二维数组,定义了行,没有定义列 int[][] arr = new int[10][]

java 使用xom对象数据序列化为xml、反序列化、Preferences相关操作小案例

package org.rui.io.xml; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.List; import nu.xom.Document; import nu.

《java入门第一季》之类小案例(模拟用户登录)

首先是做一个用户登录的小案例.在此基础上加入其它逻辑. import java.util.Scanner; /* * 模拟登录,给三次机会,并提示还有几次.如果登录成功,就可以玩猜数字小游戏了. * * 分析: * A:定义用户名和密码.已存在的. * B:键盘录入用户名和密码. * C:比较用户名和密码. * 如果都相同,则登录成功 * 如果有一个不同,则登录失败 * D:给三次机会,用循环改进,最好用for循环. */ public class StringTest2 { public st

Cookie小案例-----记住浏览过的商品记录

Cookie小案例------记住浏览过的商品记录 我们知道,这个功能在电商项目中很常见.这里处理请求和页面显示都是由servlet实现,主要是为了体现cookie的作用, 实现功能如下: 1,点击购买的商品后,显示到另一页面 2,记住用户浏览过的商品,并在页面时中显示 3,当浏览过的数量超过最大值限度时,最下面一个商品被挤下去 4,当浏览过的商品本身就在浏览记录中,显示列表将其从中间移到最上面 显示一打开网站的样子和显示用户的浏览记录: package cn.itcast.cookie; im