Question

On a canvas there is an image and on touch at certain part of the image, I am looking to launch a new Canvas from within pointerPressed() method.

Is it possible? So far I have done the following:

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

and the class is as follows:

//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);
    }
}

When I run the above code nothing happens. I mean to say that the current image does not change to the new one which I am trying to show in the canvas.

My application keeps on running after the pointer pressed event. It does not crashes. It shows me the coords of other parts of the image correctly. What I am trying to achieve is that; when I click/touch at some particular points of the image it should load a new canvas in place of the old one.

Was it helpful?

Solution

A canvas is made visible by calling the Display.setCurrent() method.You would to retrieve Display from your MIDlet and pass it to your canvas,then use it.I hope this snippet code help you:

//MIDlet:

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

//canvas1:

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 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);
}
}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top