I have a problem wtih a program.
The program logic is like this..........
from a class which extends midlet class i create object of
a class(say canobj(word)) which extends canvas class by
passing a string.
the next statement is display.setCurrent(canobj);
Everything is fine but the paint(Graphics g) method is not
executed. What may the problem. please suggest me the
answer.
Answers were Sorted based on User's Feedback
Answer / susanta mandal
With the same flow,the paint(Graphics g) will execute.
If not executes then do not display.setCurrent(canobj) here.
Go to the Canvas class & take a Display object & initialize
it with the MIDlet call display object.Then from the Canvas
class constructor call a method,where first call repaint()
method,then serviceRepaint() method,then call
display.setCurrent(this).It will work like bellow..
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TestView extends MIDlet {
CanvasView canObj;
static Display DISPLAY;
public void startApp() {
String word = "Hi Susanta";
DISPLAY = Display.getDisplay(this);
canObj = new CanvasView(word,this);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
}
class CanvasView extends Canvas implements CommandListener{
Display display;
String str="";
Command exit;
TestView midlet;
public CanvasView(String str,TestView midlet){
this.midlet = midlet;
display = TestView.DISPLAY;
this.str = str;
exit = new Command("Exit",Command.EXIT,0);
addCommand(exit);
setCommandListener(this);
callPaint();
}
protected void paint(Graphics g){
setFullScreenMode(true);
g.setColor(0,0,200);
g.fillRect(getWidth()/2-40,getHeight()/2-20,80,40);
g.setColor(255,255,255);
g.drawString(str,getWidth()/2,getHeight()/2-10,g.HCENTER|g.TOP);
}
public void callPaint(){
repaint();
serviceRepaints();
display.setCurrent(this);
}
public void commandAction(Command command,Displayable disp){
if(command == exit){
midlet.destroyApp(true);
}
}
}
Is This Answer Correct ? | 4 Yes | 1 No |
Answer / shelly
Hey,
check out the following code. See if u get any clue from
this. Your display needs to be initialised first..hope ur
doin tat in ur code.. unless u do tat ur paint wudn't be
called at all. try to display a form instead of a canvas and
see if display is causing the problem. hope this was helpful
to u.
public class Mainscreen extends MIDlet implements
CommandListener{
Display display;
public Mainscreen(){
display = Display.getDisplay(this);
setcommandListener(this);
}
public void startApp(){
Form f = new Form("ShowCanvas"){
f.addCommand(Command.OK);
f.setcommandListener(this);
display.setcurrent(f);
}
}
public void DestroyApp(boolean t){
notifyDestroyed();
}
public void pauseApp(){
}
public void commandAction(Command c ,Displayable d){
if(c==Command.OK){
showDetails();
}
}
}
public void showDetails() {
Canvas canvas = new Canvas() {
protected void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0, 0, 0);
g.setFont(font_med);
g.drawString("HI anonymous",10, 0, 0);
}
};
canvas.addCommand(Commands.Back);
canvas.setCommandListener(this);
display.setcurrent(canvas);
}
Is This Answer Correct ? | 2 Yes | 0 No |
What is SIM ?
Explain Generic Connection Framework ?
What is CVM
what is jar size of the application?
What is j2me in mobile computing?
What is optional package ?
what is the application compatability?
What is CLDC ?
What is PNG ?
What is cHTML ?
What is RMI Profile
Explain i-appli ?