Download J2ME WheelItem CustomItem

October 6th, 2007 Posted in J2ME / JavaME

This is an example of J2ME / JavaME CustomItem called WheelItem. It does nothing beside showing a spinning wheel on the phone screen. WheelItem is a perfect candidate to be displayed on a wait form screen replacing Gauge class while your application is performing lengthy operating.

WheelItem Screenshot

JAVA:
  1. import javax.microedition.midlet.*;
  2. import javax.microedition.lcdui.*;
  3. import java.util.*;
  4.  
  5. public class WheelItem extends CustomItem
  6. {
  7.   private int mCount,mMaximum;
  8.   private int mInterval;
  9.   private int mWidth,mHeight,mX,mY,mRadius;
  10.   private String mMessage;
  11.    
  12. public WheelItem(String title)
  13. {
  14.     super(title);
  15.     mCount=0;
  16.     mMaximum = 36;
  17.     mInterval = 100;
  18.  
  19.     TimerTask task = new TimerTask() {
  20.         public void run() {
  21.             mCount = (mCount+1) % mMaximum;
  22.             repaint();
  23.         }
  24.     };
  25.     Timer timer = new Timer();
  26.     timer.schedule(task,0,mInterval);
  27. }
  28.     public void setString(String string)
  29.     {
  30.         mMessage = string;
  31.         repaint();
  32.     }
  33.    
  34.     public void paint(Graphics g, int iWidth, int iHeight) {
  35.         mWidth = iWidth;
  36.         mHeight = iHeight;
  37.         int halfWidth = mWidth/2;
  38.         int halfHeight = mHeight/2;
  39.  
  40.         mRadius = Math.min(halfWidth,halfHeight);
  41.         mX = halfWidth - mRadius/2;
  42.         mY = halfHeight - mRadius/2;
  43.  
  44.         int theta = -(mCount*360/mMaximum);
  45.  
  46.         g.setColor(255,255,255);
  47.         g.fillRect(0,0,mWidth,mHeight);
  48.         g.setColor(0,0,0);
  49.         g.drawArc(mX,mY,mRadius,mRadius,0,360);
  50.         g.fillArc(mX,mY,mRadius,mRadius,theta+90,90);
  51.         g.fillArc(mX,mY,mRadius,mRadius,theta+270,90);
  52.        
  53.         if (mMessage !=null)
  54.         {
  55.             g.drawString(mMessage,mWidth/2,mHeight,Graphics.BOTTOM | Graphics.HCENTER);
  56.         }
  57.     }
  58.   public int getMinContentWidth() { return 80; }
  59.   public int getMinContentHeight() { return 80; }
  60.   public int getPrefContentWidth(int width) {return getMinContentWidth();}
  61.   public int getPrefContentHeight(int height) {return getMinContentHeight();}
  62. }

Download example application with WheelItem source : WaitFormDemo.zip

Ported (rather poorly) from Wheel canvas written by Jonathan Knudsen from his article - Networking, User Experiences and Threads.

Any suggestions or improvements are welcomed!

 

Possibly Related Posts...

One Response to “Download J2ME WheelItem CustomItem”

  1. Nedal Says:

    Download example application with WheelItem source : WaitFormDemo.zip
    ——————-
    page not found !!



Leave a Reply