×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    14

    Image display in j2me

    Hi,

    I am developing applications using the Nokia N97 SDK.

    I am trying to use a menu to display a set of images. My problems are as follows:

    1. The image that is displayed stays on for only a few seconds. The display then goes back to the main menu. The menu I am in goes as follows:
    Main Menu -> Image display menu -> Image displayed.

    So once the image displays, it goes back to Main Menu. I have added Command Back, but to no avail. What can I do to make sure that the image stays as long as I do not push the back button?

    2. The image is displayed over the menu screen. How can I get the image to be displayed on a blank screen?

    The code I have written so far is as follows:
    MyCanvas.java: Class to extend the Canvas Class.

    Code:
    import java.io.IOException;
    
    import javax.microedition.lcdui.Canvas;
    
    import javax.microedition.lcdui.Graphics;
    
    import javax.microedition.lcdui.Image;
    
    
    
    public class MyCanvas extends Canvas{
    
    	private Image img;  //this is responsible for loading the image.
    
    	
    
    	public MyCanvas(String str) {  //constructor for the class.
    
    		try {
    
    			String temp = "/";
    
    			temp += str;
    
    			//img = Image.createImage("/scfloral10.png");
    
    			System.out.println("The location at which the image is stored:");
    
    			System.out.print(temp);
    
    			img = Image.createImage(temp);
    
    		} catch(Exception e) {
    
    			e.printStackTrace();
    
    		}
    
    	}
    
    
    
    	protected void paint(Graphics g) {  //this calls function to paint image on canvas.  
    
    		g.drawImage(img, 0, 0, Graphics.HCENTER & Graphics.VCENTER);  //draws an image onto the canvas with img, the x and y co-ordinates of the anchor point and the anchor point for positioning the image.
    
    	}
    
    }
    And the section of code where the MIDlet handles image display

    Code:
    public void showFlowerImages() {
    
    	/*displays the guest book.
    
    	 * Steps:
    
    	 * 1. Get a canvas.
    
    	 * 2. Load image
    
    	 * 3. Paint on canvas.
    
    	 * 4. Implement back and refresh buttons.
    
    	 */
    
    	playForm_imageDisplay = new Form("Flower pictures");
    
    	System.out.println("Inside showFlowerImages");
    
    	but= new Button[6];
    
    	System.out.print(but.length);
    
    	cmpSize = 6;
    
    	
    
    	but[0] = new VoiceButton("scfloral10.png");
    
    	but[1] = new VoiceButton("scfloral11.png");
    
    	but[2] = new VoiceButton("scfloral12.png");
    
    	but[3] = new VoiceButton("scfloral13.png");
    
    	but[4] = new VoiceButton("scfloral14.png");
    
    	but[5] = new VoiceButton("scfloral15.png");
    
    	
    
    	backButt = new Button("Back");   //THIS HAS BEEN CHANGED.
    
    	backButt.addActionListener(this);
    
    	
    
    	///////////////////////////////////////////////////////////
    
    	try {
    
    		GridLayout gd = new GridLayout(3,2);
    
    		c= new Container(gd);
    
    	}catch (Exception e) {
    
    		new Dialog("Exception construcing grid").showModeless();
    
    	}
    
    	
    
    	System.out.println("Added new buttons and created a new grid.");
    
    	
    
    	for(int k=0;k<but.length;k++) {
    
    		but[k].setFocusPainted(true);
    
    		but[k].addActionListener(this);
    
    		but[k].addFocusListener(this);
    
    		but[k].setFocusable(true); 
    
    		c.addComponent(k,but[k]);
    
    	}
    
    	//////////////////////////////////////////////////////////
    
    	display = new Command("Display") {
    
    		public void actionPerformed(ActionEvent evt) {
    
    			playForm_imageDisplay.removeCommand(display); //remove display command.
    
    			playForm_imageDisplay.addCommand(back, 0);
    
    			for(int i = 0;i < but.length; i++) {
    
    				if(but[i].hasFocus() == true) {
    
    					System.out.println("Got the image to display");
    
    					System.out.print(but[i].getText());
    
    					imageDisplay(but[i].getText());
    
    					break;
    
    				}
    
    			}
    
    		}
    
    	};  playForm_imageDisplay.addCommand(display, 0); //add display to the same point.
    
    	System.out.println("Outside Image Display");
    
    
    
    	back = new Command("Back2") {
    
    		public void actionPerformed(ActionEvent evt) {
    
    			System.out.println("Back button has been pressed.");
    
    			//playForm_imageDisplay.removeCommand(back);
    
    			//playForm_imageDisplay.addCommand(display, 0);
    
    			playForm_imageDisplay.show();
    
    		}
    
    	};   playForm_imageDisplay.addCommand(display, 0);
    
    
    
    	//////////////////////////////////////////////////////
    
    	playForm_imageDisplay.setLayout(new BorderLayout());
    
    	playForm_imageDisplay.addComponent(BorderLayout.NORTH ,c);
    
    	playForm_imageDisplay.addComponent(BorderLayout.SOUTH, backButt);
    
    
    
    	playForm_imageDisplay.addCommandListener(this);
    
    	playForm_imageDisplay.setScrollable(true);
    
    	playForm_imageDisplay.setTransitionOutAnimator(Transition3D.createCube(300, true));
    
    	playForm_imageDisplay.show();
    
       }
    
    
    
    private void imageDisplay(String str) { //this function displays the image.
    
    	System.out.print(str);
    
    	canvas = new MyCanvas(str);
    
    	javax.microedition.lcdui.Display.getDisplay(this).setCurrent(null);
    
    	javax.microedition.lcdui.Display.getDisplay(this).setCurrent(canvas);
    
       }
    
    }
    3. Also, I wanted to know how I can access images etc. from the res folder. Please correct me if I am wrong, but that is the default folder for placing all images and wav files. How do I access something placed in the res/ folder, from, say the MyCanvas constructor?

    I have been googling all day and with very little to show for it.
    It would be great if you could help me out with this.

    Thanks.

  2. #2
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: Image display in j2me

    Quote Originally Posted by sriram_shankar View Post

    1. The image that is displayed stays on for only a few seconds. The display then goes back to the main menu. The menu I am in goes as follows:
    Main Menu -> Image display menu -> Image displayed.

    So once the image displays, it goes back to Main Menu. I have added Command Back, but to no avail. What can I do to make sure that the image stays as long as I do not push the back button?

    2. The image is displayed over the menu screen. How can I get the image to be displayed on a blank screen?


    3. Also, I wanted to know how I can access images etc. from the res folder. Please correct me if I am wrong, but that is the default folder for placing all images and wav files. How do I access something placed in the res/ folder, from, say the MyCanvas constructor?

    Thanks.
    1.As far as I can comment on this is that, you have to control you flow of the code/thread so that the control obey your code abd hace move back if you press the command Back processed in the actionPerformed().

    2. Just paint/draw the blank screen and then image on the blank screen...that's all..
    3. Well that's correct indeed and is the default way ti put the all resources and then access the same from there right away...however you can also find the images in the RES file as the bundle and then
    you can load that bundle and can use in the app...As far as I have look at your code you are workign with the LWUIT and hence you create the file using the LWUIT editor...but that is complex procedure so I would recommed that you put the images and other resources files in the res folder and simply read....
    There are several types of images in LWUIT, most of which can be stored either
    individually in the Java archive (JAR™) or packaged as part of a resource bundle.

    To load an image stored in the JAR file, use the following code:
    Image image = Image.createImage("/images/duke.png");

    The Image tag supports the following attributes:

    name The name of the resource (defaults to the name of the file name).
    file The file that would be used for the image (required)
    pack True or false. whether to store a packed image. Defaults to False

    Once loaded, the image is ready to be used as a background image of a component
    or even as an icon for a component that can contain an image.
    Last edited by raj_J2ME; 2010-07-10 at 20:36. Reason: format
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    14

    Re: Image display in j2me

    hi,

    thanks for your replies. i was able to solve the problem by extending the imageItem class. now the image stays on as long as the "Back" button is not pressed.

    thanks for all your help!
    sriram

Similar Threads

  1. Display image with built in image viewer
    By pixsta in forum Symbian Media (Closed)
    Replies: 3
    Last Post: 2012-04-29, 20:51
  2. Display Large Image on device's display
    By fecxelmin in forum Mobile Java General
    Replies: 4
    Last Post: 2010-04-22, 10:24
  3. Replies: 3
    Last Post: 2009-07-29, 14:00
  4. problem in display an image button on canvas(Background image)
    By mramin05 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 3
    Last Post: 2008-11-12, 04:51

Posting Permissions

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