1个回答
展开全部
一个简单的j2me练习,比游戏简单但整体结构都差不多:
Midlet.java:
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Midlet extends MIDlet {
MyCanvas my;
My_Canvas my1;
Display display;
public Midlet() {
// TODO Auto-generated constructor stub
my=new MyCanvas();
my1=new My_Canvas();
display=Display.getDisplay(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display.setCurrent(my1);
}
}
My_Canvas.java:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class My_Canvas extends Canvas {
int x = getWidth() / 2;
int y = getHeight() / 2;
protected void keyPressed(int keyCode) {
switch (keyCode) {
case KEY_NUM2:
y -= 5;
break;
case KEY_NUM4:
x -= 5;
break;
case KEY_NUM6:
x += 5;
break;
case KEY_NUM8:
y += 5;
break;
}
this.isEnd();
repaint();
}
protected void paint(Graphics g) {
g.setColor(0x00ffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0);
g.fillArc(x, y, 10, 10, 0, 360);
}
protected void keyRepeated(int keyCode) {
keyPressed(keyCode);
}
public void isEnd(){
if(x>=getWidth()){
x=0;
}
if(y>=getHeight()){
y=0;
}
if(x<0){
x=getWidth();
}
if(y<0){
y=getHeight();
}
}
}
Midlet.java:
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Midlet extends MIDlet {
MyCanvas my;
My_Canvas my1;
Display display;
public Midlet() {
// TODO Auto-generated constructor stub
my=new MyCanvas();
my1=new My_Canvas();
display=Display.getDisplay(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display.setCurrent(my1);
}
}
My_Canvas.java:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class My_Canvas extends Canvas {
int x = getWidth() / 2;
int y = getHeight() / 2;
protected void keyPressed(int keyCode) {
switch (keyCode) {
case KEY_NUM2:
y -= 5;
break;
case KEY_NUM4:
x -= 5;
break;
case KEY_NUM6:
x += 5;
break;
case KEY_NUM8:
y += 5;
break;
}
this.isEnd();
repaint();
}
protected void paint(Graphics g) {
g.setColor(0x00ffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0);
g.fillArc(x, y, 10, 10, 0, 360);
}
protected void keyRepeated(int keyCode) {
keyPressed(keyCode);
}
public void isEnd(){
if(x>=getWidth()){
x=0;
}
if(y>=getHeight()){
y=0;
}
if(x<0){
x=getWidth();
}
if(y<0){
y=getHeight();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询