import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author user
*/
public class Midlet extends MIDlet {
private RunBagua runBagua;
public Midlet(){
runBagua=new RunBagua();
}
public void startApp() {
Display.getDisplay(this).setCurrent(runBagua);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class RunBagua extends Canvas implements Runnable {
int[] sin = {0,
1736,
3420,
4999,
6427,
7660,
8660,
9396,
9848,
10000,
9848,
9396,
8660,
7660,
6427,
4999,
3420,
1736,
0,
-1736,
-3420,
-4999,
-6427,
-7660,
-8660,
-9396,
-9848,
-10000,
-9848,
-9396,
-8660,
-7660,
-6427,
-5000,
-3420,
-1736};
int[] cos = {10000,
9848,
9396,
8660,
7660,
6427,
5000,
3420,
1736,
0,
-1736,
-3420,
-4999,
-6427,
-7660,
-8660,
-9396,
-9848,
-10000,
-9848,
-9396,
-8660,
-7660,
-6427,
-5000,
-3420,
-1736,
0,
1736,
3420,
5000,
6427,
7660,
8660,
9396,
9848};
int width;//屏幕的宽
int height;//屏幕的高
int R = 40;//八卦图的半径一半,即鱼头的半径
int centerY;//八挂图的圆心Y坐标
int centerX;//八挂图的圆心x坐标
int[] cTempX = new int[36];
int[] cTempY = new int[36];
public RunBagua() {
width = this.getWidth();
height = this.getHeight();
centerX = width / 2;
centerY = height / 2;
for (int i = 0; i < 36; i++) {
cTempX[i] = centerX + R * cos[i] / 10000;
cTempY[i] = centerY - R * sin[i] / 10000;
}
new Thread(this).start();
}
int m = 9;
int m1 = 27;
protected void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, width, height);//刷屏
int xL1 = cTempX[m] - R;
int yL1 = cTempY[m] - R;
int xL3 = cTempX[m1] - R;
int yL3 = cTempY[m1] - R;
int start1 = m * 10;
int end1 = 180;
int start3 = m1 * 10;
int end3 = 180;
g.setColor(0, 0, 0);
g.drawArc(centerX-2*R, centerY-2*R, 4*R, 4*R, 0, 360);//画边缘边界
g.fillArc(centerX-2*R, centerY-2*R, 4*R, 4*R, start1, end1);//画黑色半圆
g.setColor(255, 255, 255);
g.fillArc(centerX-2*R, centerY-2*R, 4*R, 4*R, start3, end3);//画白色半圆
g.setColor(0, 0, 0);
g.fillArc(xL3, yL3, 2 * R, 2 * R, start3, end3);//画黑色鱼头
g.setColor(255, 255, 255);
g.fillArc(xL1, yL1, 2 * R, 2 * R, start1, end1);//画白色鱼头
g.fillArc(xL3+R-5, yL3+R-5, 10, 10, 0, 360);//画小白鱼眼
g.setColor(0, 0, 0);
g.fillArc(xL1+R-5, yL1+R-5, 10, 10, 0, 360);//画小黑鱼眼
}
/*
* 改变数据让八卦图动起来
*
*/
public void update(){
m = ++m > 35 ? 0 : m;
m1 = ++m1 > 35 ? 0 : m1;
}
public void run() {
while (true) {
update();
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}