1

在画布上有一个图像,并且在图像的某些部分触摸,我希望从 pointerPressed() 方法中启动一个新的画布。

是否可以?到目前为止,我已经完成了以下工作:

   protected void pointerPressed(int x, int y){          
        if ((x>=164 && x<=173)&&(y>=24 && y<=36)){               
            disp.setCurrent(new elementDetails());
        }
    }

课程如下:

//class to show detailed information of elements
class elementDetails extends Canvas{
    private Image elmDtlImg;
    public elementDetails(){
        try{
            elmDtlImg = Image.createImage("/details.jpg");
        }
        catch(IOException e){
            System.out.println("Couldn't load Detailed Info image" + e.getMessage());
        }               
    }

    public void paint(Graphics g){
        //set the drawing color to white
        g.setGrayScale(255);
        //draw a big white rectangle over the whole screen (over the previous screen)
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(elmDtlImg, 0, 0, 20);
    }
}

当我运行上面的代码时,什么也没有发生。我的意思是说当前图像不会更改为我试图在画布中显示的新图像。

我的应用程序在指针按下事件后继续运行。它不会崩溃。它正确地向我显示了图像其他部分的坐标。我想要实现的是;当我单击/触摸图像的某些特定点时,它应该加载一个新画布来代替旧画布。

4

1 回答 1

2

通过调用 Display.setCurrent() 方法使画布可见。您可以从 MIDlet 中检索 Display 并将其传递给您的画布,然后使用它。我希望这段代码对您有所帮助:

//MIDlet:

public class MyMIDlet extends MIDlet{
    ...
    final Canvas1 c1;
    final elementDetails c2;
    ...
    public MyMIDlet(){
        c1 = new Canvas1(this);
        c2 = new elementDetails();
    }
    ...
}

//画布1:

public class Canvas1 extends Canvas{
    MyMIDlet myMidlet;
    Display disp;

...
/**
*constructor
*/
public Canvas1(MyMIDlet myMidlet){
    this.MyMIDlet = myMidlet;
    disp = myMidlet.getDisplay();
}
...
public void paint(Graphics g){
    g.setColor(255,255,255);
    g.drawString("canvas1", 0, 0, 0);
}
...

protected void pointerPressed(int x, int y){          
    if ((x>=164 && x<=173)&&(y>=24 && y<=36)){               
        disp.setCurrent(myMidlet.c2);
}
}

//显示元素详细信息的类

class elementDetails extends Canvas{
    private Image elmDtlImg;
    public elementDetails(){
        try{
            elmDtlImg = Image.createImage("/details.jpg");
        }
        catch(IOException e){
            System.out.println("Couldn't load Detailed Info image" + e.getMessage());
        }               
    }
public void paint(Graphics g){
    //set the drawing color to white
    g.setGrayScale(255);
    //draw a big white rectangle over the whole screen (over the previous screen)
    g.fillRect(0, 0, getWidth(), getHeight());
    g.drawImage(elmDtlImg, 0, 0, 20);
}
}   
于 2012-06-12T05:56:30.757 回答