JAVA图形操作中FPS的计算(附带随机生成乱数球体用例)
2007-10-17 11:19
399 查看
FPS:即帧 /秒(frames per second)的缩写,也称为帧速率。是指1秒钟时间里刷新的图片的帧数,也可以理解为图形处理器每秒钟能够刷新几次。如果具体到手机上就是指每秒钟能够 播放(或者录制)多少格画面。同时越高的帧速率可以得到更流畅、更逼真的动画。每秒钟帧数(fps)越多,所显示的动作就会越流畅。
在绝大多数图形程序中(以游戏类为典型),执行效率都以FPS作为评估标准。
由于目前JAVA方面缺少相关用例,故完成功能如下图(在本机测试中,最大fps设定为500,实际达到FPS效率在IDE中280左右,单独运行380左右,受系统配置等因素影响):
代码如下:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/fps_test.gif)
(请变更文件后缀为jar)
![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/javafps.gif)
FPS相关操作代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.test;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.text.DecimalFormat;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Title: LoonFramework</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Description:</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Copyright: Copyright (c) 2007</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Company: LoonFramework</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author chenpeng
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @email:ceponline@yahoo.com.cn
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @version 0.1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class FPSListen ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设定动画的FPS桢数,此数值越高,动画速度越快。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static final int FPS = 500;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 换算为运行周期
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static final long PERIOD = (long) (1.0 / FPS * 1000000000); // 单位: ns(纳秒)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// FPS最大间隔时间,换算为1s = 10^9ns
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static long FPS_MAX_INTERVAL = 1000000000L; // 单位: ns
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 实际的FPS数值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double nowFPS = 0.0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// FPS累计用间距时间
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private long interval = 0L; // in ns
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private long time;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//运行桢累计
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private long frameCount = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//格式化小数位数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private DecimalFormat df = new DecimalFormat("0.0");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//开启opengl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void opengl()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.setProperty("sun.java2d.opengl", "True");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.setProperty("sun.java2d.translaccel", "True");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 制造FPS数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void makeFPS() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frameCount++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
interval += PERIOD;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//当实际间隔符合时间时。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (interval >= FPS_MAX_INTERVAL) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//nanoTime()返回最准确的可用系统计时器的当前值,以毫微秒为单位
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long timeNow = System.nanoTime();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获得到目前为止的时间距离
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long realTime = timeNow - time; // 单位: ns
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//换算为实际的fps数值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
nowFPS = ((double) frameCount / realTime) * FPS_MAX_INTERVAL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//变更数值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frameCount = 0L;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
interval = 0L;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
time = timeNow;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public long getFrameCount() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return frameCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setFrameCount(long frameCount) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.frameCount = frameCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public long getInterval() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return interval;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setInterval(long interval) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.interval = interval;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getNowFPS() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return nowFPS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setNowFPS(double nowFPS) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.nowFPS = nowFPS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public long getTime() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return time;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setTime(long time) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.time = time;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getFPS()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return df.format(nowFPS);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
球体类代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.test;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Color;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Graphics;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Title: LoonFramework</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Description:</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Copyright: Copyright (c) 2007</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Company: LoonFramework</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author chenpeng
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @email:ceponline@yahoo.com.cn
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @version 0.1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Ball ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static final int SIZE = 10;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int x, y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected int vx, vy;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Ball(int x, int y, int vx, int vy) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.x = x;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.y = y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.vx = vx;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.vy = vy;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void move() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x += vx;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
y += vy;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (x < 0 || x > BallPanel.WIDTH - SIZE) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vx = -vx;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (y < 0 || y > BallPanel.HEIGHT - SIZE) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vy = -vy;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void draw(Graphics g) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.setColor(Color.RED);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.fillOval(x, y, SIZE, SIZE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
FPS及球体处理用代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.test;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Color;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Dimension;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Frame;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Graphics;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Image;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Panel;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.event.WindowAdapter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.event.WindowEvent;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.image.BufferedImage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Random;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Title: LoonFramework
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* </p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Description:以JAVA获取FPS用演示程序及随机生成乱数球体。(更优化代码内置于loonframework-game框架中)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* </p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Copyright: Copyright (c) 2007
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* </p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Company: LoonFramework
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* </p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author chenpeng
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @email:ceponline@yahoo.com.cn
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @version 0.1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class BallPanel extends Panel implements Runnable ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static final long serialVersionUID = 1L;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static final int WIDTH = 360;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static final int HEIGHT = 360;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 设定最大球体数量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static final int NUM_BALLS = 50;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 定义球体数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Ball[] ball;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 运行状态
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private volatile boolean running = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Thread gameLoop;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 缓存用图形
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Graphics bg;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Image screen = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 生成随机数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Random rand;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// fps监听
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private FPSListen fps = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public BallPanel() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
setPreferredSize(new Dimension(WIDTH, HEIGHT));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
screen = new BufferedImage(WIDTH, HEIGHT, 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bg = screen.getGraphics();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fps = new FPSListen();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//fps.opengl();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 以当前毫秒生成随机数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rand = new Random(System.currentTimeMillis());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ball = new Ball[NUM_BALLS];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 初始化球体参数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < NUM_BALLS; i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int x = rand.nextInt(WIDTH);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int y = rand.nextInt(HEIGHT);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int vx = rand.nextInt(10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int vy = rand.nextInt(10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ball[i] = new Ball(x, y, vx, vy);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 加入Notify
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void addNotify() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.addNotify();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 判断循环条件是否成立
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (gameLoop == null || !running) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gameLoop = new Thread(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gameLoop.start();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 进行线程运作。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void run() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long beforeTime, afterTime, timeDiff, sleepTime;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long overSleepTime = 0L;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int noDelays = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获得精确纳秒时间
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beforeTime = System.nanoTime();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fps.setTime(beforeTime);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
running = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while (running) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gameUpdate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
repaint();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
afterTime = System.nanoTime();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
timeDiff = afterTime - beforeTime;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 换算间隔时间
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sleepTime = (FPSListen.PERIOD - timeDiff) - overSleepTime;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (sleepTime > 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 制造延迟
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Thread.sleep(sleepTime / 1000000L); // nano->ms
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (InterruptedException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获得延迟时间
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
overSleepTime = (System.nanoTime() - afterTime) - sleepTime;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} else ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 重新计算
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
overSleepTime = 0L;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 判断noDelays值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (++noDelays >= 16) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Thread.yield(); // 令线程让步
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
noDelays = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 重新获得beforeTime
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beforeTime = System.nanoTime();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 制造FPS结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fps.makeFPS();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 变更球体轨迹
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private void gameUpdate() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < NUM_BALLS; i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ball[i].move();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 变更图形
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void update(Graphics g) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
paint(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 显示图形
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void paint(Graphics g) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 设定背景为白色,并清空图形
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bg.setColor(Color.WHITE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bg.fillRect(0, 0, WIDTH, HEIGHT);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// FPS数值显示
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bg.setColor(Color.BLUE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bg.drawString("FPS: " + fps.getFPS(), 4, 16);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 分别绘制相应球体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < NUM_BALLS; i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ball[i].draw(bg);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.drawImage(screen, 0, 0, this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Frame frm = new Frame();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setTitle("Java FPS速度测试(由Loonframework框架提供)");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setSize(WIDTH, HEIGHT+20);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setResizable(false);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.add(new BallPanel());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setVisible(true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
frm.addWindowListener(new WindowAdapter()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void windowClosing(WindowEvent e)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.exit(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
在绝大多数图形程序中(以游戏类为典型),执行效率都以FPS作为评估标准。
由于目前JAVA方面缺少相关用例,故完成功能如下图(在本机测试中,最大fps设定为500,实际达到FPS效率在IDE中280左右,单独运行380左右,受系统配置等因素影响):
代码如下:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/fps_test.gif)
(请变更文件后缀为jar)
![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/javafps.gif)
FPS相关操作代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.test;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.text.DecimalFormat;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Title: LoonFramework</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Description:</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Copyright: Copyright (c) 2007</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Company: LoonFramework</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author chenpeng
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @email:ceponline@yahoo.com.cn
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @version 0.1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class FPSListen ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设定动画的FPS桢数,此数值越高,动画速度越快。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static final int FPS = 500;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 换算为运行周期
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static final long PERIOD = (long) (1.0 / FPS * 1000000000); // 单位: ns(纳秒)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// FPS最大间隔时间,换算为1s = 10^9ns
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static long FPS_MAX_INTERVAL = 1000000000L; // 单位: ns
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 实际的FPS数值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double nowFPS = 0.0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// FPS累计用间距时间
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private long interval = 0L; // in ns
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private long time;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//运行桢累计
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private long frameCount = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//格式化小数位数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private DecimalFormat df = new DecimalFormat("0.0");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//开启opengl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void opengl()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.setProperty("sun.java2d.opengl", "True");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.setProperty("sun.java2d.translaccel", "True");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 制造FPS数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void makeFPS() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frameCount++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
interval += PERIOD;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//当实际间隔符合时间时。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (interval >= FPS_MAX_INTERVAL) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//nanoTime()返回最准确的可用系统计时器的当前值,以毫微秒为单位
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long timeNow = System.nanoTime();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获得到目前为止的时间距离
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long realTime = timeNow - time; // 单位: ns
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//换算为实际的fps数值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
nowFPS = ((double) frameCount / realTime) * FPS_MAX_INTERVAL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//变更数值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frameCount = 0L;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
interval = 0L;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
time = timeNow;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public long getFrameCount() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return frameCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setFrameCount(long frameCount) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.frameCount = frameCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public long getInterval() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return interval;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setInterval(long interval) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.interval = interval;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getNowFPS() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return nowFPS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setNowFPS(double nowFPS) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.nowFPS = nowFPS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public long getTime() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return time;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setTime(long time) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.time = time;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getFPS()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return df.format(nowFPS);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
球体类代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.test;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Color;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Graphics;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Title: LoonFramework</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Description:</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Copyright: Copyright (c) 2007</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>Company: LoonFramework</p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author chenpeng
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @email:ceponline@yahoo.com.cn
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @version 0.1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Ball ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static final int SIZE = 10;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int x, y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected int vx, vy;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Ball(int x, int y, int vx, int vy) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.x = x;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.y = y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.vx = vx;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.vy = vy;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void move() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x += vx;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
y += vy;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (x < 0 || x > BallPanel.WIDTH - SIZE) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vx = -vx;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (y < 0 || y > BallPanel.HEIGHT - SIZE) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vy = -vy;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void draw(Graphics g) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.setColor(Color.RED);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.fillOval(x, y, SIZE, SIZE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
FPS及球体处理用代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.test;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Color;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Dimension;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Frame;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Graphics;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Image;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Panel;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.event.WindowAdapter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.event.WindowEvent;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.image.BufferedImage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Random;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Title: LoonFramework
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* </p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Description:以JAVA获取FPS用演示程序及随机生成乱数球体。(更优化代码内置于loonframework-game框架中)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* </p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Copyright: Copyright (c) 2007
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* </p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* <p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Company: LoonFramework
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* </p>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author chenpeng
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @email:ceponline@yahoo.com.cn
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @version 0.1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class BallPanel extends Panel implements Runnable ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static final long serialVersionUID = 1L;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static final int WIDTH = 360;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static final int HEIGHT = 360;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 设定最大球体数量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static final int NUM_BALLS = 50;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 定义球体数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Ball[] ball;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 运行状态
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private volatile boolean running = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Thread gameLoop;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 缓存用图形
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Graphics bg;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Image screen = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 生成随机数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Random rand;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// fps监听
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private FPSListen fps = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public BallPanel() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
setPreferredSize(new Dimension(WIDTH, HEIGHT));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
screen = new BufferedImage(WIDTH, HEIGHT, 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bg = screen.getGraphics();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fps = new FPSListen();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//fps.opengl();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 以当前毫秒生成随机数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rand = new Random(System.currentTimeMillis());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ball = new Ball[NUM_BALLS];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 初始化球体参数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < NUM_BALLS; i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int x = rand.nextInt(WIDTH);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int y = rand.nextInt(HEIGHT);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int vx = rand.nextInt(10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int vy = rand.nextInt(10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ball[i] = new Ball(x, y, vx, vy);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 加入Notify
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void addNotify() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.addNotify();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 判断循环条件是否成立
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (gameLoop == null || !running) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gameLoop = new Thread(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gameLoop.start();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 进行线程运作。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void run() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long beforeTime, afterTime, timeDiff, sleepTime;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long overSleepTime = 0L;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int noDelays = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获得精确纳秒时间
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beforeTime = System.nanoTime();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fps.setTime(beforeTime);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
running = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while (running) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gameUpdate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
repaint();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
afterTime = System.nanoTime();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
timeDiff = afterTime - beforeTime;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 换算间隔时间
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sleepTime = (FPSListen.PERIOD - timeDiff) - overSleepTime;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (sleepTime > 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 制造延迟
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Thread.sleep(sleepTime / 1000000L); // nano->ms
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (InterruptedException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获得延迟时间
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
overSleepTime = (System.nanoTime() - afterTime) - sleepTime;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} else ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 重新计算
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
overSleepTime = 0L;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 判断noDelays值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (++noDelays >= 16) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Thread.yield(); // 令线程让步
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
noDelays = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 重新获得beforeTime
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
beforeTime = System.nanoTime();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 制造FPS结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fps.makeFPS();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 变更球体轨迹
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private void gameUpdate() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < NUM_BALLS; i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ball[i].move();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 变更图形
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void update(Graphics g) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
paint(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 显示图形
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void paint(Graphics g) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 设定背景为白色,并清空图形
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bg.setColor(Color.WHITE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bg.fillRect(0, 0, WIDTH, HEIGHT);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// FPS数值显示
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bg.setColor(Color.BLUE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bg.drawString("FPS: " + fps.getFPS(), 4, 16);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 分别绘制相应球体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < NUM_BALLS; i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ball[i].draw(bg);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.drawImage(screen, 0, 0, this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Frame frm = new Frame();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setTitle("Java FPS速度测试(由Loonframework框架提供)");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setSize(WIDTH, HEIGHT+20);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setResizable(false);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.add(new BallPanel());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setVisible(true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
frm.addWindowListener(new WindowAdapter()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void windowClosing(WindowEvent e)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.exit(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- JAVA图形操作中FPS的计算(附带随机生成乱数球体用例)
- JAVA图形操作中FPS的计算(附带随机生成乱数球体用例)
- JAVA图形操作中FPS的计算(附带随机生成乱数球体用例)
- keytool生成证书 查看证书信息 ,以及java操作的简单用例
- PowerDesigner(八)-面向对象模型(用例图,序列图,类图,生成Java源代码及Java源代码生成类图)面向对象模型 面向对象模型是利用UML(统一建模语言)的图形来描述系统结构的模型,
- java随机生成测试用例
- 1、请编写函数foo(int x, int y, int n) 计算:随机生成x个大小为[1,y]的正整数,它们的和为n的概率是多少?语言仅限于PHP、C/C++、Java中的一种。
- keytool生成证书 查看证书信息 ,以及java操作的简单用例
- JAVA 小功能 随机生成图形验证码
- Java操作Hbase增删改查(附带复合条件查询以及分页查询)
- Java随机生成字母数字组合的验证码
- java生成一年中假日表(包括周末和法定假期),用于计算一年中的工作日
- java生成随机整数总结
- java:随机生成100个不重复的随机数
- 黑马程序员_java基础篇操作对象ObjectStream,管道流,随机存储Day5(下)
- iMOOC学习笔记:Java的Excel读写操作入门实践-利用POI生成Excel文件(一)
- 【Java】随机生成不重复的字母数组
- Java数字格式化,随机生成数字
- 转:随机生成动态验证码 http://www.blogjava.net/JAVA-HE/archive/2007/05/29/120582.aspx
- Java随机生成常用汉字验证码