13,100
社区成员
发帖
与我相关
我的任务
分享
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author Administrator
*/
public class Yundong extends MIDlet implements CommandListener{
static Display dis;
Yundong1 yd;
String[] str = {"直线运动","斜线运动","圆周运动","椭圆运动","抛物线运动","波浪线运动"};
static List list;
Command select;
public Yundong(){
list = new List("运动大全",Choice.EXCLUSIVE,str,null);
select = new Command("select",Command.OK,1);
list.addCommand(select);
list.setCommandListener(this);
dis = Display.getDisplay(this);
}
public void startApp() {
dis.setCurrent(list);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
int index = list.getSelectedIndex(); // 获得选中的LIST的INDEX下标
yd = new Yundong1();
yd.setIndex(index);
dis.setCurrent(yd); //显示切换
}
}
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
/*
* JUST FOR FAN --JED
*/
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
*
* @author Administrator
*/
public class Yundong1 extends Canvas implements Runnable, CommandListener {
private int width, height;
private boolean flag;
private int index;
private Image ship;
private int shipx, shipy;
private int v = 10; // 速度
private int[] sin = new int[360];
private int[] cos = new int[360];
private int jiaodu = 0; // 初始角度
Command back;
public Yundong1() {
try {
width = this.getWidth();
height = this.getHeight();
flag = true;
shipx = shipy = 10;
ship = Image.createImage("/myship.png");
//初始化SIN COS数组
for (int i = 0; i < sin.length; i++) {
sin[i] = (int) (Math.sin(Math.PI / 180 * i) * 10000);
cos[i] = (int) (Math.cos(Math.PI / 180 * i) * 10000);
System.out.println(sin[i]);
}
//当选择的是波浪线的时候 重新初始化Y坐标
if (index == 5) {
shipy = height / 2;
}
back = new Command("back", Command.BACK, 1);
new Thread(this).start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void setIndex(int index) {
this.index = index;
}
protected void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, width, height);
g.drawImage(ship, shipx, shipy, 20);
//判断显示什么运动
switch (index) {
case 0: //直线运动
shipx += 10; // 横向运动
//shipy+=10; //纵向运动
break;
case 1: // 斜线运动
shipx = shipx + v * cos[30] / 10000;
shipy = shipy + v * sin[60] / 10000;
//等同于 shipx += x方向上的速度分量 shipy += y方向上的速度分量
break;
case 2: //圆周运动
g.setColor(255, 0, 0);
g.drawArc(width / 2 - 50, height / 2 - 50, 100, 100, 0, 360);
//假定圆心为(width/2,height/2),半径为50
shipx = width / 2 + 50 * cos[jiaodu] / 10000; //对X坐标更新
shipy = height / 2 - 50 * sin[jiaodu] / 10000; //对Y坐标更新
jiaodu += 5; //角度改变并做一个越界的判断
if (jiaodu > 360) {
jiaodu = 0;
}
break;
case 3: //椭圆运动
g.setColor(255, 0, 0);
g.drawArc(width / 2 - 80, height / 2 - 50, 160, 100, 0, 360);
//同上,不过椭圆的a = 80,b=50
shipx = width / 2 + 80 * cos[jiaodu] / 10000;//对X坐标更新
shipy = height / 2 - 50 * sin[jiaodu] / 10000;//对Y坐标更新
jiaodu += 5;//角度改变并做一个越界的判断
if (jiaodu > 360) {
jiaodu = 0;
}
break;
case 4: //抛物线运动
//假定向下抛物线运动 p = 20
shipy += 5;
shipx = shipy * shipy / (40);
break;
case 5: //波浪线运动
//将波浪线画出来
g.setColor(255, 0, 0);
int n = 0;
for (int i = 0; i < sin.length; i++) {
n += 10;
if (n > 359) {
n = 0;
}
g.drawLine(10 + i, height / 2 + 50 * sin[n] / 10000, 10 + i, height / 2 + 50 * sin[n] / 10000);
}
// 画出中轴
g.drawLine(10, height / 2, width, height / 2);
//这里假设沿X轴方向运动,以height/2左右震动
shipx += 1; //X方向上均匀增加
shipy = height / 2 + 50 * sin[jiaodu] / 10000;//Y方向上以中轴为中心上下摆动
jiaodu += 10;//角度改变并做一个越界的判断
if (jiaodu > 359) {
jiaodu = 0;
}
break;
}
this.addCommand(back);
this.setCommandListener(this);
}
public void run() {
while (flag) {
try {
repaint();
Thread.sleep(500);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public void commandAction(Command arg0, Displayable arg1) {
Yundong.dis.setCurrent(Yundong.list);
}
}