?? ballcanvas.java
字號:
import javax.microedition.lcdui.*;
public class BallCanvas extends javax.microedition.lcdui.Canvas implements Runnable{
static java.util.Random random = new java.util.Random();
int posX=5, posY=5; //小球顯示位置
int ballSize = 10; //小球尺寸
Display display; //顯示器
public BallCanvas(Display display){ //構造函數(shù)
super();
this.display=display;
}
public void run() { //線程的主方法
while (true){
this.posX = (random.nextInt()>>>1) % (this.getWidth()-20) + 10; //生成小球位置X坐標
this.posY = (random.nextInt()>>>1) % (this.getHeight()-20) + 10; //生成Y坐標
try {
Thread.sleep(100); //線程休眠
} catch (InterruptedException e) {}
repaint(); //重繪屏幕
}
}
void start() {
display.setCurrent(this); //設置當前屏幕
Thread t = new Thread(this);
t.start(); //開始線程運行
repaint();
}
void destroy() {
}
protected void paint(Graphics g) {
int x = g.getClipX(); //獲取剪切區(qū)位置
int y = g.getClipY();
int w = g.getClipWidth(); //獲取剪切區(qū)大小
int h = g.getClipHeight();
g.setColor(0xffffff); //設置當前顏色
g.fillRect(x, y, w, h); //繪制填充矩形
g.setColor(0);
g.fillArc(posX, posY, ballSize, ballSize, 0, 360); //繪制小球
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -