gpt4 book ai didi

java - 我的程序将在几秒钟后崩溃。为什么?

转载 作者:行者123 更新时间:2023-11-30 05:05:15 26 4
gpt4 key购买 nike

工作了几秒钟,突然就崩溃了。

import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class CanvasUnit extends Canvas {

private Timer timer;
private TimerTaskClass task;
private int Top=13 , Left=48;
private int width = getWidth();
private int height = getHeight();
private static int SWidth = 10;
private static int SHeight = 10;
private int LStep = 5;
private int TStep = 5;

public CanvasUnit() {
timer = new Timer();
task = new TimerTaskClass();
timer.schedule(task,10);
}

public void paint(Graphics g) {

g.setColor(0,0,0);
g.fillRect(0, 0, width, height);
g.setColor(255,255,255);

g.drawRect(Left, Top, SWidth, SHeight);
}
private class TimerTaskClass extends TimerTask{

public final void run(){

if (Left > (width - SWidth)) {
LStep = LStep * (-1);
} else if (Left < 0) {
LStep = Math.abs(LStep);
}
//
if (Top > (height - SHeight)) {
TStep = TStep * (-1);
} else if (Top < 0) {
TStep = Math.abs(TStep);
}

Left = Left + LStep;
Top = Top + TStep;

repaint();

//Run the timer agian
timer = new Timer();
task = new TimerTaskClass();
timer.schedule(task,10);
}

}
}

最佳答案

抱歉,我之前的评论不正确,因为我以为你正在使用 LWUIT,不知道我从哪里得到这个想法。你应该使用类似的东西

import javax.microedition.lcdui.*;

public class CanvasUnit extends Canvas {

private int Top = 13, Left = 48;
private int width = getWidth();
private int height = getHeight();
private static int SWidth = 10;
private static int SHeight = 10;
private int LStep = 5;
private int TStep = 5;

public CanvasUnit() {
Thread t = new Thread(new Runnable() {

public void run() {
while (true) {

if (Left > (width - SWidth)) {
LStep = LStep * (-1);
} else if (Left < 0) {
LStep = Math.abs(LStep);
}
//
if (Top > (height - SHeight)) {
TStep = TStep * (-1);
} else if (Top < 0) {
TStep = Math.abs(TStep);
}

Left = Left + LStep;
Top = Top + TStep;

repaint();

try {
Thread.sleep(10);

} catch (Exception ex) {
}
}
}
});
t.start();
}

public void paint(Graphics g) {

g.setColor(0, 0, 0);
g.fillRect(0, 0, width, height);
g.setColor(255, 255, 255);

g.drawRect(Left, Top, SWidth, SHeight);
}

}

关于java - 我的程序将在几秒钟后崩溃。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5328114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com