Discussion Board

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

    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
    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);
        disp.setCurrent(canvas);
    }
    public void commandAction(Command command,Displayable screen){
        if (command == back){
            destroyApp(false);
            notifyDestroyed();
        }
        else if (command == task1) {
            regMessage="task1 is ok";
            try{
                new Thread(new senderer()).start();
            }
            catch (Exception e){
                System.out.print("Error in sending");
                e.printStackTrace();
            }
        }    
    }
    
    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() {
        destroyApp(true);
        notifyDestroyed();
      }
    
      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);
        addCommand(exit);
        try{
            //create a button
        Image button=Image.createImage("/boly.gif");
        ImageItem imageitem=new ImageItem("", button, ImageItem.LAYOUT_CENTER,"");
        addCommand(task1);
        imageitem.setDefaultCommand(task1);
        }catch (Exception e) {
          }
        setCommandListener(this);
        }
    
      protected void paint(Graphics g) {
        int width = getWidth();
            int height = getHeight();
            // Fill the background using black
            g.setColor(0);
            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.setColor(0xffffff);
                    g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
                    return;
                }
            }        
             g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
      }
    
      public void commandAction(Command command, Displayable display) {
        if (command == exit) {
          buttonOnCanvas.exitMIDlet();
        }
      }
      
    }
    }
    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.

    Amin

  2. #2
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    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
    Location
    The Capital of INDIA
    Posts
    4,328

    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,
    http://www.j2mesalsa.com/elearning/hilevel.html
    http://discussion.forum.nokia.com/fo...d.php?t=118466
    http://java.sys-con.com/node/36304

    I hope after reading these links,you will be clear about the basic concepts..
    Hope these lines helps you,
    Thanks
    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
    Location
    Faridabad(Delhi NCR)
    Posts
    343

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

    try this i m Sure it wil help

    http://java.sys-con.com/node/36304
    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
  •  
×