Membuat Welcome Screen

Di setiap aplikasi mobile, pasti akan ada tampilan welcome screen, yang umumnya hanya menampilkan icon aplikasi dan vendornya. Nah, di bawah ini contoh sederhana dari welcome screen.
Diawali dengan class SplashScreen.java:

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
*
* @author Dyah Fajar
*/
class SplashScreen extends Canvas {

String judul;
Image img;
int lebar, tinggi;
Font typeFont;
Timer timer;
int progress;
private Display dsp;
private Form midlet;
private Canvas cv;

public SplashScreen() {

judul = "Selamat Datang";
lebar = getWidth();
tinggi = getHeight();
typeFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC,
Font.SIZE_MEDIUM);
timer = new Timer();

try {
img = Image.createImage("/img/andro2_small.png");
} catch (IOException ex) {
ex.printStackTrace();
}
}

public void animation() {
timer.schedule(new TimerTask() {
public void run() {
rollOn();
}
}, 500, 300);
}

void setNextScreen(Form cv, Display display) {
this.midlet = cv;
this.dsp = display;
}

public void rollOn() {
if (progress < img.getWidth() - 5) {
progress += 10;
} else {
timer.cancel();
dsp.setCurrent(midlet);
}
repaint();
}

public void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, lebar, tinggi);

g.setColor(0, 0, 0);
g.setFont(typeFont);
g.drawString(judul, (lebar - typeFont.stringWidth(judul)) / 2,
5, g.TOP | g.LEFT);
if (img != null) {
g.drawImage(img, (lebar - img.getWidth()) / 2,
(10 + typeFont.getHeight()), g.TOP | g.LEFT);
}

g.drawRect((lebar - img.getWidth()) / 2, (10 + typeFont.getHeight() +
img.getHeight()) + 10, img.getWidth(), 10);

g.setColor(0, 0, 200);
g.fillRect((lebar - img.getWidth()) / 2 + 2, (10 + typeFont.getHeight() +
img.getHeight()) + 10 + 2,
progress, 8);
}
}

class di atas akan menampilkan progress bar. Di tampilkan pertama kali sebagai welcome screen. Berikutnya adalah class mainnya.

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.*;

/**
* @author Dyah Fajar
*/
public class CanvasMID extends MIDlet {

Display dsp;
SplashScreen cnv;
Form frm;
StringItem str;

public CanvasMID() {
dsp = Display.getDisplay(this);
cnv = new SplashScreen();
frm = new Form("ini form pertama dyah");
str = new StringItem(null, "Selamat datang di halaman aplikasi anda");

frm.append(str);
cnv.setNextScreen(frm, dsp);
}

public void startApp() {
dsp.setCurrent(cnv);
cnv.animation();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
}

Selamat mencoba. ^^
*Sekarang, aku harus kembali membuat project yang gak kunjung ketemu solusinya, hiks..hiks..
Membuat Welcome Screen Membuat Welcome Screen Reviewed by Dyah Fajar Nur Rohmah on 12:29:00 PM Rating: 5

No comments:

Mari komentar dan berdiskusi...