j2me canvas 画了个游戏菜单,按钮不会调用
j2mecanvas画了个游戏菜单,按钮不会调用。怎么实现按钮的调用啊,比如我画了了“新游戏”怎么实现我点了“新游戏”就能调用处程序啊。我自己写了一个小程序,程序继承MI...
j2me canvas 画了个游戏菜单,按钮不会调用。
怎么实现按钮的调用啊,比如我画了了“新游戏”
怎么实现我点了“新游戏”就能调用处程序啊。
我自己写了一个小程序,程序继承MIDlet .想用“新游戏”来测试能不能调用的起,结果不行
这个是我要调用的例子用来实现点”新游戏“启动的
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ImageMutable extends MIDlet
implements CommandListener
{ private ImageMutable gg;
private Command cmdExit;
private Command cmdAlert;
private Alert alert;
public ImageMutable()
{
cmdExit = new Command("退出", Command.EXIT, 1);
cmdAlert = new Command("图片警示", Command.SCREEN, 2);
}
public void startApp()
{
Form form = new Form( "测试可修改图像类 ");
form.addCommand( cmdExit );
form.addCommand( cmdAlert );
form.setCommandListener(this);
Display.getDisplay(this).setCurrent(form);
alert = new Alert( "About", "图像警示",
null, AlertType.INFO );
}
public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }
public void commandAction(Command command, Displayable
screen)
{
if (command == cmdExit)
{
notifyDestroyed();
}
else if( command == cmdAlert )
{
alert.setImage( copyImage() );
Display.getDisplay(this).setCurrent( alert );
}
}
public Image copyImage()
{
int w = Display.getDisplay(this).getCurrent().getWidth()/2;
int h = Display.getDisplay(this).getCurrent().getHeight()/2;
Image tmp = Image.createImage( w, h );
Graphics g = tmp.getGraphics();
g.setGrayScale( 255 );
for(int i=0; i<h; i++)
{
g.drawLine( 0, i, w, i );
g.setColor( 255-i, 0, 0 );
}
try
{
Image image = Image.createImage("/plane.png");
g.drawImage(image,
tmp.getWidth()/2, tmp.getHeight()/2,
Graphics.HCENTER | Graphics.BOTTOM);
}
catch(Exception e)
{
System.out.println(e.getMessage()) ;
}
return tmp;
}
public void ImageMutable() {
// TODO 自动生成方法存根
}
}
这个是CANVAS里的按钮代码
public void keyPressed(int keyCode){
//根据用户输入更新selected的值,并重新绘制屏幕
int action = this.getGameAction(keyCode);
switch(action){
case Canvas.FIRE:
printLabel(selected);
break;
case Canvas.DOWN:
selected = (selected+1)%2;
break;
case Canvas.UP:{
if(--selected < 0){
selected+=2;
}
break;
}
default:
break;
}
repaint();
serviceRepaints();
}
//响应按钮
public void disposeFire(){
switch(selected){
case 1:
?????怎么写
}
}
//showNotify()在paint()之前被调用
public void showNotify(){
System.out.println("showNotify() is called");
}
private void printLabel(int selected){
System.out.println(LABELS[selected]);
}
} 展开
怎么实现按钮的调用啊,比如我画了了“新游戏”
怎么实现我点了“新游戏”就能调用处程序啊。
我自己写了一个小程序,程序继承MIDlet .想用“新游戏”来测试能不能调用的起,结果不行
这个是我要调用的例子用来实现点”新游戏“启动的
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ImageMutable extends MIDlet
implements CommandListener
{ private ImageMutable gg;
private Command cmdExit;
private Command cmdAlert;
private Alert alert;
public ImageMutable()
{
cmdExit = new Command("退出", Command.EXIT, 1);
cmdAlert = new Command("图片警示", Command.SCREEN, 2);
}
public void startApp()
{
Form form = new Form( "测试可修改图像类 ");
form.addCommand( cmdExit );
form.addCommand( cmdAlert );
form.setCommandListener(this);
Display.getDisplay(this).setCurrent(form);
alert = new Alert( "About", "图像警示",
null, AlertType.INFO );
}
public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }
public void commandAction(Command command, Displayable
screen)
{
if (command == cmdExit)
{
notifyDestroyed();
}
else if( command == cmdAlert )
{
alert.setImage( copyImage() );
Display.getDisplay(this).setCurrent( alert );
}
}
public Image copyImage()
{
int w = Display.getDisplay(this).getCurrent().getWidth()/2;
int h = Display.getDisplay(this).getCurrent().getHeight()/2;
Image tmp = Image.createImage( w, h );
Graphics g = tmp.getGraphics();
g.setGrayScale( 255 );
for(int i=0; i<h; i++)
{
g.drawLine( 0, i, w, i );
g.setColor( 255-i, 0, 0 );
}
try
{
Image image = Image.createImage("/plane.png");
g.drawImage(image,
tmp.getWidth()/2, tmp.getHeight()/2,
Graphics.HCENTER | Graphics.BOTTOM);
}
catch(Exception e)
{
System.out.println(e.getMessage()) ;
}
return tmp;
}
public void ImageMutable() {
// TODO 自动生成方法存根
}
}
这个是CANVAS里的按钮代码
public void keyPressed(int keyCode){
//根据用户输入更新selected的值,并重新绘制屏幕
int action = this.getGameAction(keyCode);
switch(action){
case Canvas.FIRE:
printLabel(selected);
break;
case Canvas.DOWN:
selected = (selected+1)%2;
break;
case Canvas.UP:{
if(--selected < 0){
selected+=2;
}
break;
}
default:
break;
}
repaint();
serviceRepaints();
}
//响应按钮
public void disposeFire(){
switch(selected){
case 1:
?????怎么写
}
}
//showNotify()在paint()之前被调用
public void showNotify(){
System.out.println("showNotify() is called");
}
private void printLabel(int selected){
System.out.println(LABELS[selected]);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询