Bermain Graphic dengan J2ME

j2me 

Berikut ini gw kasih sample script J2ME buat testing graphics di j2me.  Ada dua file java yg akan dibuat yaitu MyCanvas.java dan scrCanvas.java .  Selamat mencoba

 

Di bawah ini script MyCanvas.java

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

 

public class MyCanvas extends MIDlet implements CommandListener{

 

    private Display display = null;

    private scrCanvas scrCanvasObj = null;

    private Command exit = new Command(“EXIT”,Command.EXIT,1);

    private Command fullScreen = new Command(“Full Screen”,Command.OK,1);

 

    public MyCanvas()

    {

        scrCanvasObj = new scrCanvas();

        scrCanvasObj.addCommand(exit);

        scrCanvasObj.addCommand(fullScreen);

        scrCanvasObj.setCommandListener(this);

    }

 

    public void startApp() {

        if(display == null)

        {

            display = Display.getDisplay(this);

            display.setCurrent(scrCanvasObj);

        }

    }

 

public void pauseApp() {

    }

 

    public void destroyApp(boolean unconditional) {

    }

 

    public void commandAction(Command c,Displayable d)

    {

        if(c == exit)

        {

            notifyDestroyed();

            destroyApp(true);

        }

        else if(c == fullScreen)

        {

            scrCanvasObj.setFullScreenMode(true);

        }

    }

}

 

 

 Di bawah ini script scrCanvas.java

 

import javax.microedition.lcdui.*;

 

public class scrCanvas extends Canvas implements Runnable{

 

    private int x = 0,y = 0;

    private int ax = 1,ay = 1;

 

    public scrCanvas()

    {

        Thread thread = new Thread(this);

        thread.start();

    }

 

    private void backgroundHitam(Graphics g)

    {

        g.setColor(0x000000);

        g.fillRect(0, 0, this.getWidth(), this.getHeight());

    }

 

    private void kotak(Graphics g)

    {

        g.setColor(0xff0000);

        g.fillRect(x,y, 25,25);

    }

 

    private void tulisString(Graphics g)

    {

        g.setColor(0x00ff00);

        g.drawString(“Contoh Canvas”, 1, 1, Graphics.LEFT | Graphics.TOP);

    }

 

 

private void rubahPosisiKotak()

    {

        x+=ax;

        y+=ay;

        if(x<=0 || x+25>=this.getWidth())

            ax*=-1;

        if(y<=0 || y+25>=this.getHeight())

            ay*=-1;

    }

 

    public void paint(Graphics g)

    {

        backgroundHitam(g);

        tulisString(g);

        kotak(g);

    }

 

    public void run()

    {

        while(true)

        {

            repaint();

            rubahPosisiKotak();

            try

            {

                Thread.sleep(1);

            }

            catch(Exception e)

            {

                e.printStackTrace();

            }

        }

    }

}

 

 

 

 

Apabila ada kesalahan tolong comment disini ^_^

a simple man

Posted in Knowledge
One comment on “Bermain Graphic dengan J2ME
  1. bebek says:

    gilaaa mas rengga makin jago aja javanya … jadi dewa nih di lab skrg ….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

March 2009
M T W T F S S
« Feb   May »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
Blog Stats
  • 72,744 hits
%d bloggers like this: