This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply locked New topic

KeyPressed event in canvas using j2me

Robin Thapa

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

I have two java classes here.


public class Services extends MIDlet {

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

public Services() {

public void startApp() {

public void pauseApp() {

public void destroyApp(boolean unconditional) {

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);
select = new Command("Select", Command.SCREEN, 1);


protected void paint(Graphics g) {


public void drawBackground(Graphics g) {
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 ( 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) {

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:
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: 10167

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!]
jQuery in Action, 2nd edition
subject: KeyPressed event in canvas using j2me
Similar Threads
PixelCompare using getRGB()
Detect "dial" button pressed from J2ME program
what is keyPreesed (int parameter)
Help Needed on animation in j2me
Help on KeyPressed()?