mygamecanvas.java
来自「3D手机游戏开发实例源代码」· Java 代码 · 共 103 行
JAVA
103 行
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import com.mascotcapsule.micro3d.v3.Util3D;
public class MyGameCanvas extends GameCanvas implements Runnable {
private boolean isRun; // 根据thread显示有无loop处理的flag
private int R; // 外侧的圆的半径
private int r; // 内侧的圆的半径
private int m; // 内侧圆的中心与描绘点的距离
private int theta; // 内侧圆的中心的转动角
private int cx, cy; // 外侧圆的中心坐标
private int preX, preY; // 1step前的坐标值
/**
* MyGameCanvas的constructor
*/
public MyGameCanvas() {
super(true);
}
/**
* 初始各个变量,清除画面
*/
public void reset(int r, int m) {
cx = getWidth() / 2;
cy = getHeight() / 2;
R = getWidth() / 2;
this.r = r;
this.m = m;
theta = 0;
preX = cx + R - r + m;
preY = cy;
// 画面的清除
Graphics g = getGraphics();
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
}
/**
* thread的开始
*/
public void start() {
isRun = true;
Thread t = new Thread(this);
t.start();
}
/**
* 根据thread执行的方法
*/
public void run() {
Graphics g = getGraphics();
while(isRun) {
tick();
draw(g);
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
stop();
}
}
}
/**
* 每经过一定时间的处理
*/
private void tick() {
// 更新转动角度
theta += 50;
}
/**
* 描绘的更新
*/
private void draw(Graphics g) {
g.setColor(255, 0, 0);
// 坐标值的计算
int x = cx + (R - r) * Util3D.cos(theta) / 4096 +
m * Util3D.cos(theta - theta * R / r) / 4096;
int y = cy - (R - r) * Util3D.sin(theta) / 4096 -
m * Util3D.sin(theta - theta * R / r) / 4096;
// 线的描绘
g.drawLine(preX, preY, x, y);
// 将目前的坐标值保存在preX, preY中
preX = x;
preY = y;
flushGraphics();
}
/**
* thread的停止
*/
public void stop() {
isRun = false;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?