001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package com.sun.perseus.demo;
033:
034: import java.io.IOException;
035: import java.io.InputStream;
036:
037: import javax.microedition.lcdui.Canvas;
038: import javax.microedition.lcdui.Display;
039: import javax.microedition.lcdui.Displayable;
040: import javax.microedition.lcdui.Font;
041: import javax.microedition.lcdui.Graphics;
042: import javax.microedition.lcdui.Image;
043:
044: /**
045: * The <code>SplashCanvas</code> class is used to display an image as soon as
046: * a MIDlet is started and, possibly, show information about the coming demo
047: * while that demo is loading.
048: */
049: public class SplashCanvas extends Canvas {
050: /**
051: * The minimal amount of time to wait when a splash screen is displayed.
052: */
053: public static final long SPLASH_MIN_LENGTH = 2500; // 2.5s
054:
055: /**
056: * The image this splash screen should show.
057: */
058: Image image;
059:
060: /**
061: * The time the last display started.
062: */
063: private long start;
064:
065: /**
066: * @param image the image this splash screen should show.
067: */
068: public SplashCanvas(final Image image) {
069: this .image = image;
070: }
071:
072: /**
073: * @param imageURL the url for the splash screen image.
074: */
075: public SplashCanvas(final String imageURL) {
076: boolean error = false;
077:
078: if (imageURL == null) {
079: error = true;
080: } else {
081: InputStream splashStream = getClass().getResourceAsStream(
082: imageURL);
083:
084: try {
085: image = Image.createImage(splashStream);
086: } catch (Exception e) {
087: e.printStackTrace();
088: error = true;
089: }
090: }
091:
092: if (error) {
093: // Create simple stub splash screen.
094: System.err.println("Creating image : " + getWidth() + " / "
095: + getHeight());
096: image = Image.createImage(getWidth(), getHeight());
097: System.err.println("Image created");
098:
099: Graphics g = image.getGraphics();
100: System.err.println("Graphics created");
101: g.setColor(255, 255, 255);
102: System.err.println("Color set");
103: g.fillRect(0, 0, getWidth(), getHeight());
104: System.err.println("Background filled");
105: g.setColor(0, 0, 0);
106: System.err.println("Color set 2");
107:
108: Font font = Font.getFont(Font.FACE_MONOSPACE,
109: Font.STYLE_PLAIN, Font.SIZE_SMALL);
110: System.err.println("Font created : " + font);
111: g.setFont(font);
112: System.err.println("Font set");
113: g.drawString("Splash Screen", getWidth() / 2,
114: getHeight() / 2, Graphics.TOP | Graphics.LEFT);
115: System.err.println("String drawn");
116: }
117: }
118:
119: public void paint(Graphics g) {
120: g.setColor(255, 255, 255);
121: g.fillRect(0, 0, getWidth(), getHeight());
122:
123: int x = (getWidth() - image.getWidth()) / 2;
124: int y = (getHeight() - image.getHeight()) / 2;
125: g.drawImage(image, x, y, Graphics.TOP | Graphics.LEFT);
126: }
127:
128: /**
129: * @param display the Display on which this splash screen should paint.
130: */
131: public void display(final Display display) {
132: display.setCurrent(this );
133: start = System.currentTimeMillis();
134: }
135:
136: /**
137: * Switches to the input Display after the minimal time has elapsed.
138: *
139: * @param display the display to switch to.
140: * @param canvas the canvas to set after the minimal amount of time has elapsed.
141: */
142: public void switchTo(final Display display, final Canvas newCanvas) {
143: long end = System.currentTimeMillis();
144: long waitMore = SPLASH_MIN_LENGTH - (end - start);
145:
146: if (waitMore > 0) {
147: try {
148: Thread.currentThread().sleep(waitMore);
149: } catch (InterruptedException ie) {
150: // Do nothing.
151: }
152: }
153:
154: display.setCurrent(newCanvas);
155: }
156:
157: /**
158: * Shows the splash screen and waits for SPLASH_MIN_LENGTH before restoring
159: * the input Displayable.
160: *
161: * @param display the display on which to show the splash screen.
162: * @param displayable the displayable to restore after the help screen has been shown.
163: */
164: public void showAndWait(final Display display,
165: final Displayable displayable) {
166: // Show the splashCanvas for a little while.
167: display.setCurrent(this );
168:
169: Thread th = new Thread() {
170: public void run() {
171: try {
172: Thread.currentThread().sleep(SPLASH_MIN_LENGTH);
173: } catch (InterruptedException ie) {
174: }
175:
176: display.setCurrent(displayable);
177: }
178: };
179:
180: th.start();
181: }
182: }
|