Reputation: 23
I have a class which extends canvas.I created one more canvas class. But I couldn't switch between them.
Is it possible to switching between canvases in J2ME?
Upvotes: 1
Views: 1815
Reputation: 14022
import ...
public class MyMIDlet extends MIDlet{
...
final Canvas1 c1;
final Canvas2 c2;
...
public MyMIDlet(){
c1 = new Canvas1(this);
c2 = new Canvas2(this);
}
...
}
import ...
public class Canvas1 extends Canvas implements CommandListener{
MyMIDlet myMidlet;
Display disp;
Command switchDisp;
...
/**
*constructor
*/
public Canvas1(MyMIDlet myMidlet){
this.MyMIDlet = myMidlet;
disp = myMidlet.getDisplay();
switchDisp = new Command("switch", Command.SCREEN, 0);
this.addCommand(switchDisp);
this.setCommandListener(this);
}
...
public void paint(Graphics g){
g.setColor(255,255,255);
g.drawString("canvas1", 0, 0, 0);
}
...
public void commandAction(Command cmd, Displayable displayable){
disp.setCurrent(myMidlet.c2);
}
}
import ...
public class Canvas2 extends Canvas implements CommandListener{
MyMIDlet myMidlet;
Display disp;
Command switchDisp;
...
/**
*constructor
*/
public Canvas1(MyMIDlet myMidlet){
this.MyMIDlet = myMidlet;
disp = myMidlet.getDisplay();
switchDisp = new Command("switch", Command.SCREEN, 0);
this.addCommand(switchDisp);
this.setCommandListener(this);
}
...
public void paint(Graphics g){
g.setColor(255,255,255);
g.drawString("canvas2", 0, 0, 0);
}
...
public void commandAction(Command cmd, Displayable displayable){
disp.setCurrent(myMidlet.c1);
}
}
Displayable object is an object that has the capability of being placed on the display. A Displayable class implements Displayable interface.
The Display class is the display manager that is instantiated for each active MIDlet and provides methods to retrieve information about the device's display capabilities. A canvas is made visible by calling the Display.setCurrent() method.
A canvas implements the Displayable interface.
A Displayable class is a UI element that can be shown on the device's screen while the Display class abstracts the display functions of an actual device's screen and makes them available to you. It provides methods to show or change the current UI element that you want displayed. Thus, a MIDlet shows a Displayable UI element on a Display using the setCurrent(Displayable element) method of the Display class.
Upvotes: 2