2006/02/19(日) [n年前の日記]
#9 [iappli] iアプリについて勉強中 - thread とか ShortTimer とか System.currentTimeMillis() とか
全然関係ないけど iappli というカテゴリを作って日記をつけることに。今までは java でカテゴリをつけてたけど、iアプリに特化した内容なわけだから、ちとアレだなと。…まあ、自分自身はほとんどカテゴリで日記を読み直したりはしてないんだけど。検索を使わないと量的に無理。
てなわけでサンプルを作って試してみたり。 _2005/10/31 ShortTimerとThread を思い出しつつ。 _第2話 新生FOMAによるシューティングゲームの作成 を参考にさせてもらいつつ。
結果。.jar のサイズ。
むぅ…。jdk1.5.0_06 を使ってるから結果がアレなのだろうか。それとも自分のソースの書き方に無駄があるのかしら。なんか後者っぽい予感。ていうかそもそも、以前と結果が違うような。
とりあえず thread 版でやってくか…。
◎ _iアプリゲームプログラミング 504i・FOMA対応 - クラスファイルのサイズを小さくするには? :
System.currentTimeMillis()メソッドを使ったほうが、ファイルサイズは小さく、実行速度は速くなります。という話を見かけて、「System.currentTimeMillis()って使ったこと無いな…」と。
てなわけでサンプルを作って試してみたり。 _2005/10/31 ShortTimerとThread を思い出しつつ。 _第2話 新生FOMAによるシューティングゲームの作成 を参考にさせてもらいつつ。
結果。.jar のサイズ。
| ShortTimer | 1412 byte |
| Thread | 1406 byte |
| System.currentTimeMillis() | 1418 byte |
とりあえず thread 版でやってくか…。
◎ 比較したプログラム :
_2005/10/31 ShortTimerとThread
と同じなんだけど。
ShortTimer 版。
Thread 版。
System.currentTimeMillis() 版。
eclipse の ウインドウ → 設定 → Java は、
ShortTimer 版。
import com.nttdocomo.ui.Canvas;
import com.nttdocomo.ui.Display;
import com.nttdocomo.ui.Graphics;
import com.nttdocomo.ui.IApplication;
import com.nttdocomo.ui.ShortTimer;
public class IappliTestTimerA extends IApplication {
private ShortTimer tm;
public void start() {
MyCanvas mc = new MyCanvas();
mc.setSoftLabel(MyCanvas.SOFT_KEY_2, "終了");
Display.setCurrent(mc);
tm.start();
}
class MyCanvas extends Canvas {
int counter;
public MyCanvas() {
counter = 0;
tm = ShortTimer.getShortTimer(this, 0, 100, true);
}
public void processEvent(int w_type, int w_param) {
if ((w_type == Display.TIMER_EXPIRED_EVENT) && (w_param == 0)) {
counter++;
repaint();
}
if ((w_type == Display.KEY_PRESSED_EVENT) && (w_param == Display.KEY_SOFT2)) {
terminate();
}
}
public void paint(Graphics g) {
g.lock();
g.clearRect(0, 0, getWidth(), getHeight());
if ((counter & 0x01) == 0) {
g.drawString("test", 120, 120);
}
g.unlock(true);
}
}
}
Thread 版。
import com.nttdocomo.ui.Canvas;
import com.nttdocomo.ui.Display;
import com.nttdocomo.ui.Graphics;
import com.nttdocomo.ui.IApplication;
public class IappliTestTimerB extends IApplication {
public void start() {
MyCanvas mc = new MyCanvas();
mc.setSoftLabel(MyCanvas.SOFT_KEY_2, "終了");
Display.setCurrent(mc);
}
class MyCanvas extends Canvas implements Runnable {
private Thread tm;
int counter;
public MyCanvas() {
counter = 0;
tm = new Thread(this);
tm.start();
}
public void run() {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// e.printStackTrace();
// break;
}
counter++;
repaint();
}
}
public void processEvent(int w_type, int w_param) {
if ((w_type == Display.KEY_PRESSED_EVENT) && (w_param == Display.KEY_SOFT2)) {
terminate();
}
}
public void paint(Graphics g) {
g.lock();
g.clearRect(0, 0, getWidth(), getHeight());
if ((counter & 0x01) == 0) {
g.drawString("test", 120, 120);
}
g.unlock(true);
}
}
}
System.currentTimeMillis() 版。
import com.nttdocomo.ui.Canvas;
import com.nttdocomo.ui.Display;
import com.nttdocomo.ui.Graphics;
import com.nttdocomo.ui.IApplication;
public class IappliTestTimerC extends IApplication {
public void start() {
MyCanvas mc = new MyCanvas();
mc.setSoftLabel(MyCanvas.SOFT_KEY_2, "終了");
Display.setCurrent(mc);
mc.exe();
}
class MyCanvas extends Canvas {
private Graphics g;
private int counter;
public void exe() {
long sleepTime = 0;
try {
g = getGraphics();
counter = 0;
while (true) {
tick();
while (System.currentTimeMillis() < sleepTime + 100) {
}
sleepTime = System.currentTimeMillis();
}
} catch (Exception e) {
}
}
public void paint(Graphics g) {
}
public void tick() {
g.lock();
g.clearRect(0, 0, getWidth(), getHeight());
counter++;
if ((counter & 0x01) == 0) {
g.drawString("test", 120, 120);
}
g.unlock(true);
}
public void processEvent(int w_type, int w_param) {
if ((w_type == Display.KEY_PRESSED_EVENT)
&& (w_param == Display.KEY_SOFT2)) {
terminate();
}
}
}
}
eclipse の ウインドウ → 設定 → Java は、
- インストール済みのJRE : JDK 1.5.0_06
- コンパイラー : 準拠レベル 5.0
- 「デフォルトの準拠設定の使用」をチェック。
- クラスファイル生成 : 変数属性、行番号属性、ソースファイル名のチェックをOFF。
[ ツッコむ ]
以上です。