Home > Pemprograman > J2ME Graphics – Part 1

J2ME Graphics – Part 1

Hari ini sepulang dari LSKK, kepikiran harus menguasai game kembali.. dulu pernah belajar game di J2ME dari Mr.Rizal, tapi sudah agak lupa🙂 .. tapi sebelum belajar membuat Game, ada baiknya kita menguasai terlebih dahulu graphics atau class canvas.

Dalam menggunakan UI, kita dapat menggunakan Low Level ataupun HighLevel, dalam contoh berikut saya sharing mengenai cara pembuatan applikasi berikut :

Ketika kita tekan tombol 1 atau 2, maka warna akan berubah menjadi warna lainnya. berikut source codenya :

Class Midlet.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package Menu;

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

/**
* @author Octaviano
*/
public class Midlet extends MIDlet{
private Display display;
CanvasMain cm;
public Midlet() {
display = Display.getDisplay(this);
}

protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {

}

protected void pauseApp() {

}

protected void startApp(){
cm = new CanvasMain(this, display);
display.setCurrent(cm);
}
}

class CanvasMain.java


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package Menu;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
*
* @author Octaviano
*/
public class CanvasMain extends Canvas {
Midlet main;
Display display;
int header = 30;
int footer = 20;
int red = 255;
int green = 178;
int blue = 90;
String kode = "";
int game;
public CanvasMain(Midlet main, Display display)
{
this.main = main;
this.display = display;
setFullScreenMode(true);
}
protected void paint(Graphics g) {
g.setColor(red,green,blue);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), header);
g.fillRect(0, getHeight()-footer, getWidth(), footer);
g.setColor(0x000000);

g.drawLine(0, header, getWidth(), header);
g.drawLine(0, getHeight()-footer, getWidth(), getHeight()-footer);
g.setColor(0x444444);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD, Font.SIZE_SMALL));
g.drawString("1.TITLE", getWidth()/2, 2, Graphics.TOP|Graphics.HCENTER);
g.drawString("2.Left", 10, getHeight()-5, Graphics.BOTTOM|Graphics.LEFT);
g.drawString("3.OPTION", getWidth()/2, getHeight()-5, Graphics.BOTTOM|Graphics.HCENTER);
g.drawString("4.Right", getWidth() - 5, getHeight()-5, Graphics.BOTTOM|Graphics.RIGHT);
g.drawString("Nilai : "+kode+"\nNilai : "+game, getWidth()/2, getHeight()/2, 0);
}

protected void keyPressed(int keyCode) {
kode = Integer.toString(keyCode);
game = getGameAction(keyCode);
switch(keyCode)
{
case 49:
red = 222;
green = 223;
blue = 34;
break;
case 50:
red = 43;
green = 208;
blue = 34;
break;
case 51:
red = 208;
green = 69;
blue = 34;
break;
default:
red = 255;
green = 178;
blue = 90;
break;
}
repaint();
}

}

Categories: Pemprograman
  1. No comments yet.
  1. No trackbacks yet.

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