aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes KeyPressed event in canvas using j2me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply locked New topic
Author

KeyPressed event in canvas using j2me

Robin Thapa
Greenhorn

Joined: Aug 23, 2009
Posts: 11
Dear All,

I have two java classes here.

------------------------------------------------------------------------------------
Services.java

public class Services extends MIDlet {

Display display;
private TCanvas canvas = new TCanvas(this);

public Services() {
}

public void startApp() {
Display.getDisplay(this).setCurrent(canvas);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
}
-----------------------------------------------------------------------------------
TCanvas.java

public class TCanvas extends Canvas implements CommandListener {


private Image headerImage = null;
private Image ServiceListImage = null;
private Image contactUsImage = null;
private Image aboutUsImage = null;
public Services parent;
private Command exit, select;
List list;

public TCanvas(Services midlet) {
parent = midlet;
exit = new Command("Exit", Command.EXIT, 0);
addCommand(exit);
select = new Command("Select", Command.SCREEN, 1);
addCommand(select);
setCommandListener(this);


}

protected void paint(Graphics g) {
drawBackground(g);

}

public void drawBackground(Graphics g) {
g.setColor(0x584BA5);
try {
headerImage = Image.createImage("/images/header.png");
serviceListImage = Image.createImage("/images/logo1.png");
aboutUsImage = Image.createImage("/images/logo2.png");
contactUsImage = Image.createImage("/images/logo3.png");
} catch (java.io.IOException e) {
throw new RuntimeException("Unable to load Image: " + e);
}
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(headerImage, 0, 0, Graphics.LEFT | Graphics.TOP);
g.drawImage(serviceListImage, 0, 60, 0);
g.setColor(255, 255, 255);
g.drawString("Services", 23, 60, Graphics.LEFT | Graphics.TOP);
g.drawImage(aboutUsImage, 0, 87, 0);
g.setColor(255, 255, 255);
g.drawString("About Us", 23, 90, Graphics.LEFT | Graphics.TOP);
g.drawImage(contactUsImage, 0, 117, 0);
g.setColor(255, 255, 255);
g.drawString("Contact Us", 23, 120, Graphics.LEFT | Graphics.TOP);

}

public void commandAction(Command c, Displayable d) {
if (c == exit) {
parent.destroyApp(true);
}
}
------------------------------------------------------------------------------------


The problem i have here is i can display the image but i have a problem to navigate through the images&text .ie in the emulator it displays (image and Text).
For eg in emulator i can see:
--------------------------------------
Services
About Us
Contact Us

Exit Show
-------------------------------------------
Now i want to use a keyPressed(int keyCode);
but how do i do that, coz my image is on canvas and im not being able to use the above keyPressed(int keyCode) method.

Please help me.

Robin Thapa


[Thumbnail for services.PNG]

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I have two java classes here.

is that why you posted it in 2 forums here?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Michael is right.
Let us continue the discussion in one place.
Closing this thread.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KeyPressed event in canvas using j2me