Hi,
I want to display a logo for about 5 seconds before a form in the application is created. The code I have written is as follows:
I call the above piece of code as follows:Code:import java.io.IOException; import java.util.Timer; import java.util.TimerTask; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; /*this is the j2me class for showing a logo before the main program starts. * The canvas must init with a string containing location of the resource and a timer indicating duration. * The canvas should call paint and repaint only as long as the timer runs. */ public class timedCanvas /*extends Canvas*/ { private String imageURL; private int time2Display; //the no. of seconds the image needs to be displayed. private myCanvas canvas; private Timer tm; private myTimerTask tt; public timedCanvas(String str, int time) { imageURL = str; time2Display = time; canvas = new myCanvas(imageURL); System.out.println("Getting into creating canvas object."); tt = new myTimerTask(canvas); System.out.println("Canvas created. Moving into creating timer task object."); tm.schedule(tt, 0, time2Display); } } class myCanvas extends Canvas { private Image img; private Graphics g; public myCanvas(String str) { try { img = Image.createImage(str); System.out.println("Creating an image at url:" + str); g = img.getGraphics(); System.out.println("Got graphics."); } catch (IOException e) { System.out.println("Caught an exception in image creation."); e.printStackTrace(); } System.out.println("Created image. Now trying to paint."); } protected void paint(Graphics g) { // TODO Auto-generated method stub System.out.println("Inside paint method."); g.setColor(0, 0, 0); g.fillRect(0, 0, getWidth(), getHeight()); g.drawImage(img, getWidth()/2, getHeight()/2, Graphics.HCENTER | Graphics.VCENTER); } } class myTimerTask extends TimerTask { private myCanvas canvas; public myTimerTask(myCanvas canvas) { this.canvas = canvas; System.out.println("Initialized canvas."); } public void run() { // TODO Auto-generated method stub. canvas.repaint(); } }
Every time the program runs, I end up with a Illegal State Exception. I am new to Java/J2ME and would welcome help/critiques on the code.Code:public void form1() { timedCanvas = new timedCanvas("/scfloral.png", 5000); }
Thanks,
Sriram

Reply With Quote



