Tagged: j2me Toggle Comment Threads | Keyboard Shortcuts

  • pit 10:26 am on May 20, 2008 Permalink | Reply
    Tags: effect, explode, , , j2me, ,   

    Let your images explode in J2ME! 

    After a MIDP 1.0 utility to rotate images now time has come for some image fun :)

    J2ME Image explode effect screenshot
    When writing mobile applications, it’s always cool to add some effects or transitions. But, while for example FlashLite has a nice builtin support for them, with J2ME you have to hand-code even the simplest movement (and this is the main reason why most J2ME apps are all but attractive).

    So, here’s a first class that you can use to add an “explode” effect to images in a straightforward way. How to do it? Here we come:

    1. Download the ExplodingImage.java source code and put it straight in your project
    2. Instantiate an ExplodingImage this way:
      //get your Image
      Image sourceImage = Image.createImage("/image.png");
      //and then use it in ExplodingImage constructor
      ExplodingImage image = new ExplodingImage(sourceImage , 5, 8, 8);

      The ExplodingImage constructor accepts the following arguments:

      • An Image instance
      • An int representing the “level” for the exploding effect, that is the strength of the effect itself (higher the level, stronger the effect).
      • The last 2 int arguments represent the horizontal and vertical pieces of the exploded image.
    3. Start the explode effect with the explode() method, that will accept the effect duration as argument:
      image.explode(2000L);
    4. To paint it, simply use its paint() method, very similary to the Graphics drawImage() one. For example, in a Canvas paint() method, you can do something like this:
      protected void paint(Graphics g)
      {
      	g.setColor(0xffffff);
      	g.fillRect(0, 0, width, height);
      	image.paint(g, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
      }

      To give the effect a “smooth” animation, you should paint it quite frequently (let’s say, not once per second :)). So, always using Canvas, a sample code could be like this:

      public void run()
      {
      	while(true)
      	{
      		repaint();
       
      		try
      		{
      			synchronized(this)
      			{
      				wait(50L);
      			}
      		}
      		catch(Exception e)
      		{
      			e.printStackTrace();
      		}
      	}
      }
    5. To test if the effect has ended, you can simply access your ExplodingImage ended instance variable:
      if(image.ended)
      {
      	//effect-end related code
      }
    6. And you’re done! See it in action here: J2ME image explode effect in action

    Sample source code is available here:

     
  • pit 10:53 am on May 19, 2008 Permalink | Reply
    Tags: j2me, midp 1.0, rotate image,   

    Rotating images in J2ME using MIDP 1.0 

    So you’re still using MIDP 1.0 uh? And maybe you need to rotate an image?

    J2ME rotate image screenshot

    If this is your case, you could find useful this simple function:

    public static Image rotateImage(Image image, int angle) throws Exception
    {
    	if(angle == 0)
    	{
    		return image; 
    	}
    	else if(angle != 180 && angle != 90 && angle != 270)
    	{
    		throw new Exception("Invalid angle");
    	}
     
    	int width = image.getWidth();
    	int height = image.getHeight();
     
    	int[] rowData = new int[width];
    	int[] rotatedData = new int[width * height];
     
    	int rotatedIndex = 0;
     
    	for(int i = 0; i < height; i++)
    	{
    		image.getRGB(rowData, 0, width, 0, i, width, 1);
     
    		for(int j = 0; j < width; j++)
    		{
    			rotatedIndex = 
    				angle == 90 ? (height - i - 1) + j * height : 
    				(angle == 270 ? i + height * (width - j - 1) : 
    					width * height - (i * width + j) - 1
    				);
     
    			rotatedData[rotatedIndex] = rowData[j];
    		}
    	}
     
    	if(angle == 90 || angle == 270)
    	{
    		return Image.createRGBImage(rotatedData, height, width, true);
    	}
    	else
    	{
    		return Image.createRGBImage(rotatedData, width, height, true);
    	}
    }

    So, how can you use it? Nothing more than:

    Image original = Image.createImage("/original_image.png");
     
    Image rotated_image = rotateImage(original, 90);
     
  • pit 10:48 am on May 15, 2008 Permalink | Reply
    Tags: , j2me, , , xml parsing   

    How to parse a generic XML file in J2ME with kXML 

    Some days ago I’ve posted J2ME code to parse RSS feeds using kXML library. Today’s code is about parsing a generic XML, so you can use it to parse any XML document you want. Code is splitted in the following two classes.

    XmlNode class

    This is the class representing a single node. We’ll define 2 node types:

    • Text nodes: nodes without a name, contanining only a textual value. For them we’ll define a TEXT_NODE final variable
    • Element nodes: nodes with a tag name, that can have children nodes and/or attributes

    XmlNode class source code is available here: XmlNode.java

    GenericXmlParser class

    This is the class that will actually do XML parsing. We’ll define only one public method, parseXML(), that will accept 2 arguments:

    • a KXmlParser instance, that will be used to do XML parsing
    • a boolean that will tell if whitespace-only text nodes must be ignored or not

    and will return and XmlNode representing the root node of the parsed XML tree.

    GenericXmlParser class source code is available here: GenericXmlParser.java

    Sample usage

    Let’s see how we can use the two classes above. First we must instantiate and initialize a KXmlParser, and then we can call our GenericXmlParser parseXML() method.

    InputStreamReader reader = new InputStreamReader(getClass().getResourceAsStream("/test3.xml"));
     
    KXmlParser parser = new KXmlParser();
     
    parser.setInput(reader);
     
    GenericXmlParser gParser = new GenericXmlParser();
     
    XmlNode xml = gParser.parseXML(parser, true);

    Now, we have our resulting XmlNode that will hold the whole XML tree. We can make a simple dump of it to check if it’s all ok, using this test function:

    void dumpXML(XmlNode node, int deep)
    {
    	for(int i = 0; i < deep; i++)
    	{
    		System.out.print(" ");
    	}
    	System.out.print(node.nodeName + " - ");
     
    	if(node.nodeValue != null)
    	{
    		System.out.print("(" + node.nodeValue + ") - ");
    	}
    	String[] attributes = node.getAttributeNames();
     
    	for(int i = 0; i < attributes.length; i++)
    	{
    		System.out.print(attributes[i] + ": " + node.getAttribute(attributes[i]) + ", ");
    	}
     
    	System.out.println();
     
    	for(int i = 0; i < node.children.size(); i++)
    	{
    		dumpXML((XmlNode)node.children.elementAt(i), deep + 1);
    	}
    }

    Note: This article is available also on Forum Nokia Wiki: How to parse an XML file in J2ME with kXML

     
  • pit 4:59 pm on May 12, 2008 Permalink | Reply
    Tags: file upload, , http, j2me, multipart request, post, , upload image   

    HttpMultipartRequest: simple file uploads with J2ME 

    Today’s code is related to file uploads using J2ME. We’ll use Http POST MultiPart requests to handle and transfer data from J2ME application to server; since there is no builtin support for MultiPart, we’ll have to build the request body by hand.

    j2me file upload sample screenshot

    Code is limited to 1 class only (HttpMultipartRequest), that will be easy to include and easy in your code, just instatiate it with its constructor:

    HttpMultipartRequest(String url, Hashtable params,
    String fileField, String fileName,
    String fileType, byte[] fileBytes)

    and then send data to server (and get response) with its send() method. As you can see from the constructor, this implementation support also parameter passing, that will be posted to server together with the file bytes.

    Detailed and explained code (with sample client and server code) is available here: HTTP Post multipart file upload with J2ME Wiki article. To directly download source code, click here.

     
    • Girish 10:07 am on February 11, 2009 Permalink

      Hi! I have tried using the class in a MIDlet, but I cannot make it work. Can you help me? I’ve posted that on a forum, but I haven’t found any help yet.
      http://discussion.forum.nokia.com/forum/showthread.php?p=541133

    • Kapil Katiyar 9:45 am on September 15, 2011 Permalink

      Thanks

    • sabri 9:29 pm on November 30, 2012 Permalink

      hi …
      thanx for ur help … the code working success full except the picture cann’t shown after saved on server because viewer can’t open this picture because the fi;e appears to be damaged …
      and the size on server is larger than the size on mobile …
      please help me to solve this problem ….
      thanx

  • pit 1:28 pm on May 9, 2008 Permalink | Reply
    Tags: , j2me, , parser, rss,   

    Parsing RSS feeds with J2ME and KXML 

    Some days have passed since last J2ME tutorial, so here is a fresh new one!

    Today we’ll see how parsing a RSS feed with J2me is easy using KXML library, a fast and small XML pull parser, expecially suited for constrained environments like mobile devices. A live sample, parsing the RSS feed of this blog, is available here.

    J2ME Kxml rss parser screenshot

    The detailed explanation of source code is available on my Forum Nokia Wiki article: J2ME RSS Parser with KXml. If you’re interested only in plain source code, you can pick it up here (it includes also the sample midlet you find on the emulator page). To use KXmlRssParser class, you must simply do:

    KXmlRssParser parser = new KXmlRssParser();
     
    Vector rssItems = parser.parse(yourFeedURL);

    and the parse() method will return the complete list of parsed Items, as instances of RssItem class. Source code is of course simplified, for the purpose of this tutorial, as it only considers title, link and description tags of each <item>, but once you understand KXml logic you can extend it, without much effort, to include other infos from RSS feed.

    Other resources you might find useful:

     
    • Mahesh 2:31 pm on June 19, 2008 Permalink

      Hi i m a trainer in my comp, i saw the code its very nice n the standards are maintained i this coding. Currently i m working on xml parsing, so i juz want to know that if i want to display the names(channel names) in a list view on device, then is this the same procedure i need to follow or else there is a diferent way. If so can u plz tell me?

    • Pit 12:23 pm on June 20, 2008 Permalink

      Hi Mahesh,

      the approach and code described in this post works only to parse (and display) RSS feed items, but you can easily adapt it to parse channel names, or any other infos contained within the RSS feed.

      Pit

    • ram 8:09 am on July 8, 2010 Permalink

      I m working on a J2me Project.I ve succesfully implemented Client Login system and am able to get the RSS feed of ContactsI used ur example to parse the RSS feed..

      I want to fetch all the contact names/email ids/photo and display them on screen.

      From the Rss Feed I was able to parse out “contact name”

      The problem is in parsing image and especially email address..
      The Rss feed lines goes something like

      I want to parse out the address from the above line for all contacts.By the way I use KXML library for parsing in j2me..
      Kindly help Plz!

    • Ratnesh 6:12 am on August 29, 2011 Permalink

      Well i am trying to make this but i having one problem . when i am declaring object f KXmlPArser() its showning me error..

      KXmlParser parser = new KXmlParser() ;

      at this particlauar line i am gettign the error.

      i am usingn Eclipse ME
      KXml 2.3

      i urgently need help on this .. I had added classpath also.

  • pit 3:02 pm on May 5, 2008 Permalink | Reply
    Tags: darts, , j2me,   

    J2ME game code: let’s play Darts! 

    Darts is an ideal game for mobile users, since it does only require few seconds for a match, and could be really helpful while waiting for a bus.. (If you must wait a bus in Rome, it’s easy you’ll become a Darts champion within a day :))

    J2ME game darts screenshot

    This simple game was originally included, as easter egg, in the midlet built for 2007 Edition of Rome JavaDay, to help folks being awake during long talks :) For a live test you can go directly to the emulator page.

    2007 Rome JavaDay Midlet screenshot

    Now, It’s source code is fully available to everyone to (ab)use, and please be kind with me for the total lack of comments…

    Here is the source code, and the full sample midlet if you want to try it out on your phone.

     
    • sejal 3:55 pm on April 5, 2011 Permalink

      hey…when i execute this code on wtk..its showing me errors saying dat

      package com.jappit.japdarts.app does not exist
      import com.jappit.japdarts.app.JapDarts;
      ^
      package com.jappit.japdarts.utils does not exist
      import com.jappit.japdarts.utils.ScoreManager;
      ^
      can u plz help me with this..where do i get the functions and variables in the package package com.jappit.japdarts.display; from ?…pls reply asap !!

    • Renniel 4:09 pm on October 8, 2011 Permalink

      where is the code sir?? thnaks

c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel