📄 threadcanvas2.java
字号:
import javax.microedition.lcdui.*;
public class ThreadCanvas2 extends Canvas implements Runnable {
int x,y;
Thread thd;
Image img;
Graphics grphx;
boolean begin;
public ThreadCanvas2(){
x=this.getWidth()/2-10;
y=0;
img = Image.createImage(100,100);
begin = true;
grphx = img.getGraphics();
thd = new Thread(this);
thd.start();
}
protected void paint(Graphics g) {
// TODO 自动生成方法存根
g.setColor(0);
g.fillRect(0,0,getWidth(),getHeight());
grphx.setColor(255,0,0);
grphx.fillRect(0,0,100,100);
grphx.setColor(0,255,255);
grphx.drawRect(45, y, 10, 10);
grphx.fillRect(45, y, 10, 10);
g.drawImage(img,0, 0, Graphics.LEFT|Graphics.TOP);
}
public void start(){
begin = true;
}
public void pause(){
begin = false;
}
public void run() {
// TODO 自动生成方法存根
while(true){
if(begin){
y+=10;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
if(y>100){
y=0;
}
repaint();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -