Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Oct 2008

    problem in display an image button on canvas(Background image)

    hi guys,
    I am trying to develop a application where there is an image as a background and on the background I want to display a button. By pressing the button a certain method will run to do something.
    For that I use canvas . When I run the application only the background image is displayed but there is no button in there.
    Here is the code
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.io.IOException;
    * @author Amin
    public class ButtonOnCanvas extends MIDlet implements CommandListener {
        private Display disp;
        public Command task1=new Command("task1", Command.ITEM, 1);
        private MyCanvas canvas = new MyCanvas(this);
        private Command back;
        private String regMessage;
    public ButtonOnCanvas()
    disp = Display.getDisplay(this);
    back = new Command("back",Command.BACK,2);
    regMessage = "";
    public void startApp(){    
        canvas = new MyCanvas(this);
    public void commandAction(Command command,Displayable screen){
        if (command == back){
        else if (command == task1) {
            regMessage="task1 is ok";
                new Thread(new senderer()).start();
            catch (Exception e){
                System.out.print("Error in sending");
    public class senderer implements Runnable {
        public void run ()  {
            try {
               //Do some task;
            catch (Exception e)     {
             System.out.println("Error in processing");
             e.printStackTrace ();
    public void pauseApp() {
    public void destroyApp(boolean unconditional) {
    public void exitMIDlet() {
      public Display getDisplay() {
        return disp;
      class MyCanvas extends Canvas implements CommandListener {
      private Command exit;
      private ButtonOnCanvas buttonOnCanvas;
      private Image image = null;
      public MyCanvas(ButtonOnCanvas buttonOnCanvas) {
        this.buttonOnCanvas = buttonOnCanvas;
        exit = new Command("Exit", Command.EXIT, 1);
            //create a button
        Image button=Image.createImage("/boly.gif");
        ImageItem imageitem=new ImageItem("", button, ImageItem.LAYOUT_CENTER,"");
        }catch (Exception e) {
      protected void paint(Graphics g) {
        int width = getWidth();
            int height = getHeight();
            // Fill the background using black
            g.fillRect(0, 0, width, height);        
            // Load an image from the MIDlet resources to set as background
            if (image == null) {
                try {
                    image = Image.createImage("/sonali.gif");
                } catch (IOException ex) {
                    g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
             g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
      public void commandAction(Command command, Displayable display) {
        if (command == exit) {
    I can not understand there is the problem.
    Would you like to give a hand on this code.
    Please help me as I am new in j2me.


  2. #2
    Super Contributor
    Join Date
    Apr 2007

    Re: problem in display an image button on canvas(Background image)

    well u are only drawing image (ur background)
    g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
    as far as i can see...
    just make an Image for the Button and not an ImageItem, then draw the button Image just like u draw the background...

  3. #3
    Super Contributor
    Join Date
    Mar 2008
    The Capital of INDIA

    Thumbs up Re: problem in display an image button on canvas(Background image)

    Hi mramin05,
    First of all the reason that your command is not visible is as follow -
    @ You have used in your class Canvas..right..However Command can only be used with Form.
    @ If you want to have the command then just extend your class from Form...

    Please have some idea about the Canvas and Form...then you will come to know what component can be used with which class.

    I am giving you these links,

    I hope after reading these links,you will be clear about the basic concepts..
    Hope these lines helps you,
    Thanks with Regards,

    R a j - The K e r n e l

    Join Delhi-NCR Nokia Developer's Community,

  4. #4
    Regular Contributor
    Join Date
    Jul 2008
    Faridabad(Delhi NCR)

    Re: problem in display an image button on canvas(Background image)

    try this i m Sure it wil help

    Anshu Chauhan
    J2me Developer

Similar Threads

  1. Opening a JPEG Image
    By ummarbhutta in forum Mobile Java Media (Graphics & Sounds)
    Replies: 8
    Last Post: 2007-02-15, 07:34
  2. Problem with sending image
    By ckt in forum Symbian
    Replies: 5
    Last Post: 2007-01-03, 09:27
  3. how to cut some part of Image
    By mshouab in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2006-08-04, 09:05
  4. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 16:31

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts