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



I have a problem wtih a program. The program logic is like this.......... from a class which exten..

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

I have a problem wtih a program. The program logic is like this.......... from a class which exten..

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

Post New Answer

More J2ME Interview Questions

how many types of string?

2 Answers   Wipro,


What is Mobitex ?

1 Answers  


What is kXML ?

2 Answers  


Explain about TDMA ?

1 Answers  


Explain CVM ?

1 Answers  






What is kSOAP ?

2 Answers  


What is SMS ?

3 Answers  


What is LAN ?

4 Answers  


What is WDP ?

1 Answers  


11 is a group of specifications for wireless networks developed by the Institute of Electrical and Electronics Engineers (IEEE). 802.11 uses the Ethernet protocol and CSMA/CA (carrier sense multiple access with collision avoidance) for path sharing.

0 Answers  


What is WAP ?

1 Answers  


what is midlet>difference beiween midlet and servlet

1 Answers  


Categories