⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test2.java

📁 它是一个精灵的测试程序.适合j2me新手
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.game.*;
import java.io.*;

public class Test2 extends MIDlet implements CommandListener{
  private Command c;
  private Display display;
  private DrawCanvas dc;
  
	public Test2(){
		display=Display.getDisplay(this);
	  dc=new DrawCanvas(display);
	  c=new Command("退出",Command.EXIT,0);
	  dc.addCommand(c);	
	  dc.setCommandListener(this);
	}
	public void startApp(){
		dc.start();
	}
	public void pauseApp(){
	}
  
  public void destroyApp(boolean unconditonal){}
  
  public void commandAction(Command c,Displayable d){
  	if(c.getCommandType()==Command.EXIT){
  		destroyApp(true);
  		notifyDestroyed();
  	}
  }
}




class DrawCanvas extends GameCanvas implements Runnable{
	private Display display;
	private int l,r;
	private Thread t;
  private Image img;
  private Sprite s,yunShi;
  private boolean sleeping,left;
  
  public DrawCanvas(Display d){
  	super(false);
  	display=d;
  	l=3;
  	r=4;
  	sleeping = false;
  	left=true;
  	try{
  		img=Image.createImage("/img.png");
  		s=new Sprite(img,96,96);
  		s.setPosition(50,50);
  		yunShi=new Sprite(Image.createImage("/yunShi.png"),42,35);
  		yunShi.setPosition(100,100);
  		yunShi.setFrame(0);
  	}catch(IOException e){
  		e.printStackTrace();
  		System.out.println("初使化错误!");
  	}
  	t=new Thread(this);
  }
  
  public void start(){
  	display.setCurrent(this);
  	t.start();
  }
  
  public void update(){
  	yunShi.nextFrame();
  	   //s.setFrame(l);
  	if(left==true){
  		s.setFrame(l);
  		l--;
  		if(l==0) l=3;
  	}else if(left==false){
  		s.setFrame(r);
  		r++;
  		if(r==8) r=4;
  	}
    int k=getKeyStates();
  	if((k&LEFT_PRESSED)!=0){
  		left=true;
      s.move(-3,0);
      yunShi.move(-3,0);
  	}else if((k&RIGHT_PRESSED)!=0){
  		left=false;
      s.move(3,0);
      yunShi.move(4,0);
  	}
  	if((k&UP_PRESSED)!=0){
       // System.out.println("KEY UP_PRESSED");
       yunShi.move(0,-4);
        
    }else if((k&DOWN_PRESSED)!=0){
        //System.out.println("KEY Down_PRESSED");
        yunShi.move(0,4);
  	}
  	
  }
  
  public void draw(Graphics g){
  	g.setColor(0x030168);
  	g.fillRect(0,0,getWidth(),getHeight());
  	yunShi.paint(g);
  	s.paint(g);
  	flushGraphics();
  }
  
  public void run(){
  	Graphics g=getGraphics();;
  	while(!sleeping){
  		update();
  		draw(g);
  		try{
  			Thread.sleep(80);
  		}catch(Exception e){
  			System.out.println("Thread failed!");
  		}
  	}
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -