【原创】java 流星划过天空
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage; import javax.swing.JFrame;
import javax.swing.JPanel; public class MeteorFly extends JFrame { final int MAX = 5; // (1~1000)流星的个数
final int SLEEP = 30; // 流星飞行的速度(数值越大,速度越慢)
final int COLORLV = 2; // (2~20)色阶(可改变光晕大小)
final String COLOR = null; // ("#000000"~"#ffffff")光晕颜色(如果不填或null,则为默认颜色)
final int SIZE = 3; // (2~50)流星大小 private MyPanel panel; public MeteorFly() {
panel = new MyPanel();
this.getContentPane().add(panel); this.setSize(800, 400); // 创建窗体
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
} public static void main(String[] args) {
new MeteorFly();
} class MyPanel extends JPanel implements Runnable { Meteor p[]; int AppletWidth, AppletHeight; BufferedImage OffScreen;
Graphics drawOffScreen;
Thread pThread; public MyPanel() {
setBackground(Color.black); //窗体初始化
AppletWidth = 800;
AppletHeight = 400;
p = new Meteor[MAX];
for (int i = 0; i < MAX; i++)
p[i] = new Meteor();
OffScreen = new BufferedImage(AppletWidth, AppletHeight,
BufferedImage.TYPE_INT_BGR);
drawOffScreen = OffScreen.getGraphics();
pThread = new Thread(this);
pThread.start();
} @Override
public void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponents(g);
g.drawImage(OffScreen, 0, 0, this);
} @Override
final public void run() {
while (true) {
// drawOffScreen.clearRect(0, 0, AppletWidth, AppletHeight); //
// 清屏 for (int i = 0; i < MAX; i++) {
drawOffScreen.setColor(p[i].color); // RGB颜色
drawOffScreen.fillOval(p[i].x, p[i].y, SIZE, SIZE);
p[i].x += p[i].mx;
p[i].y += p[i].my;
// if (p[i].x > AppletWidth || p[i].y > AppletHeight) {
// p[i].reset();
// } int x = p[i].x;
int y = p[i].y;
int R = p[i].color.getRed(); // 提取颜色
int G = p[i].color.getGreen();
int B = p[i].color.getBlue();
while (true) {
if (R == 0 && G == 0 && B == 0) {
break;
}
R -= COLORLV; // 尾部颜色淡化
if (R < 0) {
R = 0;
}
G -= COLORLV;
if (G < 0) {
G = 0;
}
B -= COLORLV;
if (B < 0) {
B = 0;
}
Color color = new Color(R, G, B);
x -= p[i].mx; // 覆盖尾部
y -= p[i].my;
drawOffScreen.setColor(color);
drawOffScreen.fillOval(x, y, SIZE, SIZE);
}
if (x > AppletWidth || y > AppletHeight) { // 流星飞出窗口,重置流星
p[i].reset();
}
}
repaint(); try {
Thread.sleep(SLEEP);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } class Meteor { // 流星类
int x, y; // 流星的位置
int mx, my; // 下落速度
Color color; // 流星颜色 public Meteor() {
reset();
} public void reset() {
int rand = (int) (Math.random() * 100); //随机生成流星出现位置
if (rand > 35) {
x = (int) (Math.random() * 600);
y = 0;
} else {
y = (int) (Math.random() * 150);
x = 0;
}
mx = (int) (Math.random() * 2 + 1); //随机生成下落速度和角度
my = (int) (Math.random() * 2 + 1);
if (COLOR == null || COLOR.length() == 0) {
color = new Color(
// 随机颜色
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128);
} else {
color = Color.decode(COLOR);
}
}
} }
本人原创未经允许请勿转载
【原创】java 流星划过天空的更多相关文章
- [原创]java WEB学习笔记95:Hibernate 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]Java性能优化权威指南读书思维导图
[原创]Java性能优化权威指南读书思维导图 书名:Java性能优化权威指南 原书名:Java performance 作者: (美)Charlie Hunt Binu John 译者: 柳飞 ...
- [原创]Java静态代码检查工具介绍
[原创]Java静态代码检查工具介绍 一 什么是静态代码检查? 静态代码分析是指无需运行被测代码,仅通过分析或检查源程序的语法.结构.过程.接口等来检查程序的正确性,找出代码隐藏的错误和缺陷,如参数 ...
- [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]Java性能优化权威指南读书思维导图4
[原创]Java性能优化权威指南读书思维导图4
- [原创]Java性能优化权威指南读书思维导图3
[原创]Java性能优化权威指南读书思维导图3
- [原创]Java性能优化权威指南读书思维导图2
[原创]Java性能优化权威指南读书思维导图2
- [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...
随机推荐
- HW6.11
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HDU-4618 Palindrome Sub-Array 暴力枚举
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4618 直接暴力枚举中心点,在中间如果求不出最大值直接跳过优化下... //STATUS:C++_AC_ ...
- <转>HTML中的table转为excel
转换html 中的table 为excel,firefox浏览器支持,代码如下 <%@ page language="java" contentType="text ...
- iOS 分类和继承
iOS 中分类(Categories) 和 继承(Inherit)有相同的功能,但在一些细节上又有差异,简单介绍一下两者的异同. 分类可以在不知道系统类源代码的情况下,为这个类添加新的方法.分类只能用 ...
- 2016 Mac OS 10.11 CocoaPods的安装问题
CocoaPods的安装问题: 1.首先用淘宝的Ruby镜像来访问CocoaPods,打开终端输入以下命令: (1)gem sources --remove https://rubygems.org ...
- hdu 5495 LCS 水题
LCS Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5495 Descr ...
- android实现弧形进度表盘效果
附件:Cirbar.rar
- [React Native + Firebase] React Native: Real time database with Firebase -- setup & CRUD
Install: npm i --save firebase // v3.2.1 Config Firebase: First we need to require Firebase: import ...
- [AngularJS] Best Practise - Resolve promises in router, defer controllers
See more:http://toddmotto.com/opinionated-angular-js-styleguide-for-teams/ /** * Created by Answer12 ...
- flash builder 4.7 debug via usb device iPhone 4s - device not found
http://forums.adobe.com/message/4865192 Please provide more info on the above issue: 1.What is the m ...