Selasa, 31 Mei 2011

Menggambar Splash pada Mobile

splash sama saja dengan loading, perpindahan antara satu tampilan ke tampilan lain.
Buat 2 class, yang pertama class untuk canvas , yang kedua class untuk midlet .

Inilah contoh script untuk class canvasnya :



import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

public class Splash extends GameCanvas implements Runnable{

    boolean runStat;
    boolean TampilGb1;
    boolean TampilGb2;
    boolean TampilStat = true;

    int Timer;
    int x = 0;
   
    public Splash()
    {
        super(true);
    }

    public void Start()
    {
        System.out.println("Running....");
        runStat = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void Stop()
    {
        runStat = false;
    }

    public void Timer()
    {
        Timer++;
        if(Timer == 50)
        {
            TampilGb1 = true;
        }
        else if(Timer == 100 && TampilStat)
        {
            TampilGb2 = true;
        }
        else if(Timer == 250)
        {
            TampilGb1 = false;
            TampilGb2 = false;
        }
    }

    public void drawScreen(Graphics g)
    {
//        System.out.println("Timer" + Timer);
//        System.out.println("x" + x);
        if(TampilGb1)
        {
            g.setColor(x,x,x);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(0,0,0);
            g.drawRect(0, 0, getWidth()-1, getHeight()-1);
           if(TampilStat)
           {
                if(x<250) x+=10;
           }
           else
           {
                if (x>0) x=-10;
           }
        }
        else
        {
            if(Timer == 1)
            {

            }
        }

        if(TampilGb2)
        {
            g.setColor(0,0,0);
            g.drawString("Kelas XII - M", (getWidth()/2)-20, (getHeight()/2)-10, 0);
            g.setColor(0,255,0);
            g.drawString("Rekayasa Perangkat", (getWidth()/2)-20, (getHeight()/2)+10, 0);
            g.drawString("Lunak", (getWidth()/2)-20, (getHeight()/2)+20, 0);
            g.setColor(0,0,255);
            g.drawString("SMKN 4 Bandung", (getWidth()/2)-20, (getHeight()/2)+40, 0);
        }
        flushGraphics();
    }
   
    public void run()
    {
        Graphics g = getGraphics();
        while(runStat == true)
        {
            Timer();
            drawScreen(g);
            try{Thread.sleep(20);}catch(InterruptedException ie){};
        }
    }

}

dan ini contoh script untuk class midletnya :

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

public class drawSplash extends MIDlet
{
    Display d;
    public void startApp()
    {
        Splash sp = new Splash();
        sp.Start();
        if(d == null)
        {
            d = Display.getDisplay(this);
        }
        d.setCurrent(sp);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}


Tampilannya akan seperti ini :










Tidak ada komentar:

Posting Komentar