Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
    Bookmark Topic Watch Topic
  • New Topic

KeyPressed event in canvas using j2me  RSS feed

 
Robin Thapa
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
services.PNG
[Thumbnail for services.PNG]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Report post to moderator
> I have two java classes here.

is that why you posted it in 2 forums here?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Michael is right.
Let us continue the discussion in one place.
Closing this thread.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!