zoukankan
html css js c++ java
在手机上动起来的文字
主程序:
/**/
/*
* GameMIDlet.java
*
* Created on 2006年4月28日, 下午9:31
*/
import
javax.microedition.midlet.MIDlet;
import
javax.microedition.lcdui.
*
;
import
javax.microedition.lcdui.Display;
import
javax.microedition.midlet.MIDletStateChangeException;
/** */
/**
*
*
@author
hero
*
@version
*/
public
class
GameMIDlet
extends
MIDlet
{
static
GameMIDlet instance;
static
Display display;
private
GameScreen gameScreen;
public
GameMIDlet()
{
instance
=
this
;
gameScreen
=
new
GameScreen(
false
);
//
产生实例
display
=
Display.getDisplay(
this
);
}
protected
void
startApp()
throws
MIDletStateChangeException
{
gameScreen.start();
//
启动线程,不知道什么原因,在这一句出现stop的现象,有可能是自己用F5时,设置了断点
display.setCurrent(gameScreen);
}
protected
void
pauseApp()
{
gameScreen.stop();
}
public
void
destroyApp(
boolean
flag)
throws
MIDletStateChangeException
{
gameScreen.stop();
}
}
次程序:
/**/
/*
* GameScreen.java
*
* Created on 2006年4月28日, 下午10:22
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import
javax.microedition.lcdui.
*
;
import
javax.microedition.lcdui.game.
*
;
import
java.util.Random;
/** */
/**
*
*
@author
hero
*/
final
class
GameScreen
extends
GameCanvas
implements
Runnable
{
/** */
/**
Creates a new instance of GameScreen
*/
private
Graphics graphics;
//
保存图形环境实例
private
boolean
runningFlag
=
false
;
//
线程运行标志
private
static
final
long
TIME_PER_FRAME
=
100
;
//
每一帧的周期
private
int
width;
//
屏幕宽度
private
int
height;
//
屏幕高度
private
Random random
=
new
Random();
//
设置一个随机数;
public
static
final
int
BLUE
=
0x000000ff
;
private
int
x,y,vx,vy;
String str
=
"
J2ME游戏世界
"
;
public
GameScreen(
boolean
flag)
{
super
(flag);
graphics
=
getGraphics();
//
取得图形环境实例
width
=
getWidth();
height
=
getHeight();
init();
}
void
init()
{
x
=
width
/
2
;
y
=
height
/
2
;
vx
=
random.nextInt(
2
)
+
1
;
//
nextInt(int i)定义在cldc 1.1中
vy
=
random.nextInt(
2
)
+
1
;
}
private
void
input()
{
}
private
void
logic()
{
x
+=
vx;
y
+=
vy;
Font font
=
graphics.getFont();
if
(x
<=
0
||
x
+
font.stringWidth(str)
>=
width)
{
vx
=-
vx;
}
if
(y
<=
0
||
y
+
20
>=
height)
{
vy
=-
vy;
}
}
private
void
render(Graphics g)
{
g.setColor(BLUE);
g.fillRect(
0
,
0
, width, height);
g.setColor(
0x00ffffff
);
g.drawString(str,x,y,Graphics.LEFT
|
Graphics.TOP);
}
public
synchronized
void
start()
{
if
(
!
runningFlag)
{
runningFlag
=
true
;
Thread th
=
new
Thread(
this
);
//
启动线程
th.start();
}
}
public
synchronized
void
stop()
{
runningFlag
=
false
;
}
public
void
run()
{
while
(runningFlag)
{
long
startTime
=
System.currentTimeMillis();
input();
logic();
render(graphics);
flushGraphics();
long
elapsedTime
=
System.currentTimeMillis()
-
startTime;
if
(elapsedTime
<
TIME_PER_FRAME)
{
try
{
Thread.sleep(TIME_PER_FRAME
-
elapsedTime);
}
catch
(InterruptedException ex)
{
}
}
}
}
}
查看全文
相关阅读:
学习Jammendo代码的心路历程(二)ViewFlipper数据的填充
学习Jammendo代码的心路历程(一)简单的淡出效果实现
面向对象的一小步:添加ActiveRecord的Scope功能
关于token和refresh token
一个极为简单的方法实现本地(离线)yum安装rpm包
Yii2基本概念之——生命周期(LifeCycle)
一篇文章说透Nginx的rewrite模块
PHP Session 常用的函数
Session 的原理及最佳实践
Yii2基本概念之——配置(Configurations)
原文地址:https://www.cnblogs.com/Dreamfly/p/388479.html
最新文章
ASP.NET Core 中的 ServiceProvider
Java设计模式六大原则-2
Java设计模式六大原则-1
对象导论系列---每个对象都提供服务
对象导论系列---被隐藏的具体实现
对象导论系列---每个对象都至少有一个接口
OOP导论系列---抽象过程
Java九阳真经论述及愿景
UML架构设计师必备神器
【ABAP系列】SAP ABAP 为表维护生成器创建事务代码
热门文章
【HANA系列】对话SAP全球CEO孟鼎铭:未来最大的发展机遇属于中国中小企业
【FIORI系列】SAP 一文读懂SAP Fiori是什么
【ABAP系列】SAP ABAP DATA
【公众号系列】信息不回电话不接,程序员真的那么忙?
【ABAP系列】SAP MAC GUI750安装过程
【JAVA系列】使用JavaScript实现网站访问次数统计代码
【HANA系列】SAP HANA SQL IFNULL和NULLIF用法与区别
【HANA系列】SAP HANA SQL去除字符串空格
多线程CountDownLatch和Join
JAVA循环依赖
Copyright © 2011-2022 走看看