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();
}
}

最后附上代码

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

  1. Java小案例-(逃离迷宫)

    Java小案例-(逃离迷宫) 一,迷宫需求描述: 1,用户输入迷宫图(限制方形):字母1位墙,0为通,e为出口,m为入口,*为已访问的位置,用外围1围住迷宫 2,运行轨迹右,左,下,上 3,判断该迷宫 ...

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

    首先是做一个用户登录的小案例.在此基础上加入其它逻辑. import java.util.Scanner; /* * 模拟登录,给三次机会,并提示还有几次.如果登录成功,就可以玩猜数字小游戏了. * ...

  3. Session与Cookie的原理以及使用小案例>从零开始学JAVA系列

    目录 Session与Cookie的原理以及使用小案例 Cookie和Session所解决的问题 Session与Cookie的原理 Cookie的原理 Cookie的失效时机 小提示 Session ...

  4. 《java入门第一季》之Character类小案例

    /*  * Character 类在对象中包装一个基本类型 char 的值  * 此外,该类提供了几种方法,以确定字符的类别小写字母,数字,等等,并将字符从大写转换成小写,反之亦然  * */ 下面通 ...

  5. 02SpringMvc_springmvc快速入门小案例(XML版本)

    这篇文章中,我们要写一个入门案例,去整体了解整个SpringMVC. 先给出整个项目的结构图:

  6. Java小例子(学习整理)-----学生管理系统-控制台版

    1.功能介绍: 首先,这个小案例没有使用数据库,用集合的形式暂时保存数据,做测试! 功能: 增加学生信息 删除学生信息 修改学生信息 查询学生信息:  按照学号(精确查询)  按照姓名(模糊查询) 打 ...

  7. Session小案例------完成用户登录

    Session小案例------完成用户登录     在项目开发中,用户登陆功能再平常只是啦,当用户完毕username和password校验后.进入主界面,须要在主界面中显示用户的信息,此时用ses ...

  8. MVC 小案例 -- 信息管理

    前几次更新博客都是每次周日晚上到周一,这次是周一晚上开始写,肯定也是有原因的!那就是我的 Tomact 忽然报错,无法启动,错误信息如下!同时我的 win10 也崩了,重启之后连 WIFI 的标志也不 ...

  9. JAVA实用案例之文件导出(JasperReport踩坑实录)

    写在最前面 想想来新公司也快五个月了,恍惚一瞬间. 翻了翻博客,因为太忙,也有将近五个多月没认真总结过了. 正好趁着今天老婆出门团建的机会,记录下最近这段时间遇到的大坑-JasperReport. 六 ...

随机推荐

  1. 【hdoj_2391】FilthyRich

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2391 题目大意:给定一个矩阵,从左上角第一个元素开始到右下最后一个元素,寻找一条路线,使得路线经过的矩阵元 ...

  2. AC日记——[SDOI2017]相关分析 洛谷 P3707

    [SDOI2017]相关分析 思路: 裸线段树: (玄学ac): 代码: #include <bits/stdc++.h> using namespace std; #define max ...

  3. 深入理解String.intern()方法

    首先进入intern()的源码中, 首先说一点:1.7后的JVM为String在方法区中开辟了一个字符串常量池,如果一个String()不是new()出来的,都将在常量池中拿字符. 注释翻译过来就是, ...

  4. 五十一 常用第三方模块virtualenv

    在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-packages目录下. 如果我们要同时开发多个应用程序,那这 ...

  5. go chapter 6 - map array

    遍历 for i,v := range *arr { // 遍历数组,第一个参数为index, 第二个参数为元素 fmt.Println("---------------") fm ...

  6. python3下安装aiohttp遇到过的那些坑

    python3下安装aiohttp遇到过的那些坑 最近需要用到aiohttp这个库,在安装过程中遇到很多坑.google.baidu后,依然没有找到合适的解决方案. 后来通过去python官方的PyP ...

  7. 计算机基础-day3

    网络基础 什么是互联网协议? 互联网协议是计算机网络中为进行数据交换而建立的规则.标准或约定的集合,其定义了计算机如何接入internet,以及接入internet的计算机间通信的一系列统一标准, 为 ...

  8. POJ 2348 Euclid's Game 博弈论

    http://poj.org/problem?id=2348 顺便说,必应翻译真的好用,比谷歌翻译好用100倍. 很难判断这道题的具体博弈类型. 有两种写法,一种是找规律,一种是推理得到关系后循环(或 ...

  9. CodeForces - 1000E We Need More Bosses

    题面在这里! 依然一眼题,求出割边之后把图缩成一棵树,然后直接求最长链就行了2333 #include<bits/stdc++.h> #define ll long long using ...

  10. [SRM478]RandomApple

    题意:有$k$种苹果和$n$个箱子,每个箱子中有一些苹果,先等概率选取$n$个箱子组成集合的非空子集,再从选出的苹果中随机选一个,问每种苹果被选中的概率是多少 设箱子$i$有$cnt_{i,j}$个第 ...