#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    35
    Rep Power
    9

    Can't Create Image Item


    I'm trying to create an image item for an add button however the same thing will be used for edit and delete icons as well. The issue i'm having is that the system just won't load the image.

    Project Directory: C:\Program Files\Samsung_SDK_111_beta\apps\Shopping List\
    Project Files Reside in: C:\Program Files\Samsung_SDK_111_beta\apps\Shopping List\src\shoppingList
    Image Location: C:\Program Files\Samsung_SDK_111_beta\apps\Shopping List\src\shoppingList\images\icons\

    java Code:
     
    // shoppingList.java
    package shoppingList;
     
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
     
    public class shoppingList extends MIDlet { 
    	// Variables
    	Form mainForm = new Form("Shopping List");
    	Display display = Display.getDisplay(this);
    	Image AddButton1 = Image.createImage ("images/icons/add.png");
     
    	// Constructors
    	public shoppingList() {
    		mainForm.append (AddButton1);
    	}	
    	//-----------------------------------------------------------
     
    	// startApp Method
    	public void startApp() throws MIDletStateChangeException {
    		display.setCurrent (mainForm);
    	}
    	//-----------------------------------------------------------
     
    	// pauseApp Method
    	public void pauseApp() {
    	}
    	//-----------------------------------------------------------
     
    	// destroyApp Method
    	public void destroyApp(boolean unconditional) {
    		notifyDestroyed();
    	}
    	//-----------------------------------------------------------
     
    } // End Program


    This is the basic stripped down version that still has issues, i don't have it in a try catch block because i was hoping it would give a more descriptive error. The final code will reside in its own file and be inside a try catch block to avoid crashes.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0
    The same issue has been a pain in the butt for me as well. After a lot of trial-and-error, I've found this seems to be the smoothest, most cross-platform compatible way to load images:

    java Code:
    URL imageURL = JFrame.class.getResource("/images/icons/add.png");
    ImageIcon myIcon = new ImageIcon(imageURL);
    Image myImage = myIcon.getImage();
    For your application, all you'll need are the first two lines to do something like this:

    java Code:
    URL imageURL = JFrame.class.getResource("/images/icons/add.png");
    ImageIcon myIcon = new ImageIcon(imageURL);
     
    JButton myButton = new JButton();
    myButton.setIcon(myIcon);
    And if your complaint with that is, "Oh, it's too many lines; I don't want to declare that many variables," then just put it all in one line:

    java Code:
    myButton.setIcon(new ImageIcon(JFrame.class.getResource("/images/icons/add.png")));

IMN logo majestic logo threadwatch logo seochat tools logo