图片:
大家看了前面我写的教程,回去在自己电脑上运行了吗?
是不是很有意思呢?或者说太单调,而不会动觉得乏味呢?
别急,现在教大家如何在手机屏上显示图片,而且让她动起来:P
本程序需要图片文件,大家可以用自己的图片文件放到你自己建立的项目的目录下的
/RES目录下. 比如你建立了girl这个项目,那么在你安装的WTK22目录下的apps里有个叫girl的目录. ..\WTK22\apps\girl\res\ 注意图片的大小最好不要过大,因为屏幕
本身尺寸就不是很大,建议 50X50左右,这样会得到好的效果:P
PS:希望大家看了我的教程,能回个贴,大家回了贴就是给我最大的动力.使我有继续写
下去的动力. 谢谢 为了中国的游戏事业大家一起加油吧~~~~~~!!!
----------------------------------------------------------------------------------------------------------
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ufo extends MIDlet implements CommandListener
{
private ufocanvas canvas;
public void startApp()
{
if(canvas==null)
{
canvas=new ufocanvas(Display.getDisplay(this));
Command exitCommand=new Command("Exit",Command.EXIT,0);
canvas.addCommand(exitCommand);
canvas.setCommandListener(this);
}
canvas.start();
}
public void pauseApp()
{
}
public void destroyApp(boolean b)
{
canvas.stop();
}
public void commandAction(Command c,Displayable s)
{
if(c.getCommandType()==Command.EXIT)
{
destroyApp(true);
notifyDestroyed();
}
}
}
------------------------------------------------------------------------------------------------------
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.*;
import java.util.*;
public class ufocanvas extends GameCanvas implements Runnable
{
private Display display;
private boolean sleep;
private long framedelay;
private Random rand;
private Sprite ufoSprite;
private int ufoXmove,ufoYmove;
private Thread t;
private int frameWidth,frameHeight;
private int imageWidth,imageHeight;
public ufocanvas(Display d)
{
super(true);
display=d;
framedelay=33;
}
public void start()
{
display.setCurrent(this);
try
{
ufoSprite=new Sprite(Image.createImage("/1.png"));
ufoSprite.setPosition(0,0);
}
catch(Exception e)
{
e.printStackTrace();
System.err.println("File loading is erro...");
}
frameWidth=getWidth();
frameHeight=getHeight();
imageWidth=ufoSprite.getWidth();
imageHeight=ufoSprite.getHeight();
///////////////////////////////////////////////////////
//System.out.println(imageWidth+" "+imageHeight);
ufoXmove=ufoYmove=2;
sleep=false;
rand=new Random();
t=new Thread(this);
t.start();
}
public void run()
{
Graphics g=getGraphics();
while(!sleep)
{
updraw();
draw(g);
try
{
Thread.sleep(framedelay);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void stop()
{
sleep=true;
}
public void updraw()
{
ufoSprite.move(ufoXmove,ufoYmove);
if(ufoSprite.getX()+imageWidth>=frameWidth)
ufoXmove=-ufoXmove;
if(ufoSprite.getY()+imageHeight>=frameHeight)
ufoYmove=-ufoYmove;
if(ufoSprite.getX()<=0)
ufoXmove=-ufoXmove;
if(ufoSprite.getY()<=0)
ufoYmove=-ufoYmove;
}
public void draw(Graphics g)
{
g.setColor(0,0,0);
g.fillRect(0,0,frameWidth,frameHeight);
ufoSprite.paint(g);
flushGraphics();
}
}
希望大家能回贴,回贴是给我写下去的最大动力!