You are on page 1of 8

Versi1.

0 1|P a g e

Modul Praktikum Thread


1. Tujuan
Setelah menyelesaikan aktifitas praktek ini diharapkan peserta dapat mengetahui
cara mengimplementasikan kelas Thread pada sebuah game mobile.

2. LatarBelakang
Setelah mempelajari tentang kelas Thread pada Modul Thread (JENI 4) marilah kita
coba mengimplementasikan pengetahuan kita dengan membuat sebuah proyek game
sederhana yang menggunakan kelas Thread.


3. Percobaan
Percobaan1:MembuatClassMidletThreadBackground
import j avax. mi cr oedi t i on. mi dl et . *;
import j avax. mi cr oedi t i on. l cdui . *;

public class Mi dl et Thr eadBackgr ound extends MI Dl et {
private Di spl ay di spl ay;
private Thr eadBackgr oudCanvas t hr eadCanvas = new Thr eadBackgr oudCanvas( ) ;

public void st ar t App( ) {
di spl ay = Di spl ay. get Di spl ay( this) ;
t hr eadCanvas. st ar t ( ) ;
di spl ay. set Cur r ent ( t hr eadCanvas) ;
}

public Display get Di spl ay( ) {
r et ur n di spl ay;
}
public void pauseApp( ) { }

public void dest r oyApp( bool ean uncondi t i onal ) {
exi t ( ) ;
}

S
Y
S
T
O
O
L
S

D
E
M
O











8|ank age has been |ntent|ona||y |nserted
here.
http:]]www.systoo|sgroup.com]

Versi1.0 3|P a g e

Modul Praktikum Thread



public void st op( ) {
i sPl ay = false;
}

// method run abstrak dari Runnable Mejalankan pertamakali pada graphics
public void r un( ) {
Gr aphi cs g = get Gr aphi cs( ) ;
while ( i sPl ay == true) {
x++;
if( x<- 120) {
x=0;
}else {
x=x- 5;
}
dr awScr een( g) ;
t r y { Thr ead. sl eep( del ay) ; }
cat ch ( I nt er r upt edExcept i on i e) { }
}
}

// Method drawScreen untuk menggambar Graphics
private void dr awScr een( Gr aphi cs g) {
try {
g. set Col or ( 255, 255, 255) ;
g. f i l l Rect ( 0, 0, get Wi dt h( ) , get Hei ght ( ) ) ;
I mage i mage = I mage. cr eat eI mage( " / gbr - bg. png" ) ;
g. dr awI mage( i mage, x, ( get Hei ght ( ) - i mage. get Hei ght ( ) ) / 2,
Gr aphi cs. TOP | Gr aphi cs. LEFT) ;
}catch( Except i on e) { }
f l ushGr aphi cs( ) ;
}
}
S
Y
S
T
O
O
L
S

D
E
M
O











8|ank age has been |ntent|ona||y |nserted
here.
http:]]www.systoo|sgroup.com]

Versi1.0 5|P a g e

Modul Praktikum Thread


public void st ar t App( ) {
di spl ay = Di spl ay. get Di spl ay( t hi s) ;
t hr eadCanvas. st ar t ( ) ;
di spl ay. set Cur r ent ( t hr eadCanvas) ;
}

public Di spl ay get Di spl ay( ) {
r et ur n di spl ay;
}

public void pauseApp( ) { }

public void dest r oyApp( bool ean uncondi t i onal ) {
exi t ( ) ;
}

public void exi t ( ) {
t hr eadCanvas. st op( ) ;
Syst em. gc( ) ;
not i f yDest r oyed( ) ;
}
}

Percobaan4:MembuatClassThreadObjectCanvas
MenggunakankelasThreaduntukmelakukanpergerakangambarobyekpadagamemobile:(tambahkan
scriptscriptyangberwarnamerah)
import j avax. mi cr oedi t i on. l cdui . *;
import j avax. mi cr oedi t i on. l cdui . game. *;

public class Thr eadObj ect Canvas extends GameCanvas implements Runnabl e {
private bool ean i sPl ay; // gambar akan berjalan apabila bernilai true
private l ong del ay; // di beri nilai konstanta
private i nt cur r ent X, cur r ent Y; // Pergerakan untuk koordinat 'X dan Y'
private i nt wi dt h; // untuk lebar screean
private i nt hei ght ; // untuk tinggi screean
private i nt x=0;
private i nt sw=0;
S
Y
S
T
O
O
L
S

D
E
M
O











8|ank age has been |ntent|ona||y |nserted
here.
http:]]www.systoo|sgroup.com]

Versi1.0 7|P a g e

Modul Praktikum Thread


// Method drawScreen untuk menggambar Graphics
private void dr awScr een( Gr aphi cs g) {
try {
g. set Col or ( 255, 255, 255) ;
g. f i l l Rect ( 0, 0, get Wi dt h( ) , get Hei ght ( ) ) ;
I mage i mage = I mage. cr eat eI mage( " / gbr - bg. png" ) ;
g. dr awI mage( i mage, x, ( get Hei ght ( ) - i mage. get Hei ght ( ) ) / 2,
Gr aphi cs. TOP | Gr aphi cs. LEFT) ;
St r i ng oby=" " ;
i f ( sw==0) {
oby=" / obyek1. png" ;
}el se{
oby=" / obyek2. png" ;
}
I mage i mage2 = I mage. cr eat eI mage( oby) ;
g. dr awI mage( i mage2, 40, ( get Hei ght ( ) - i mage. get Hei ght ( ) ) / 2,
Gr aphi cs. TOP | Gr aphi cs. LEFT) ;
}catch( Except i on e) { }
f l ushGr aphi cs( ) ;
}
}
S
Y
S
T
O
O
L
S

D
E
M
O











8|ank age has been |ntent|ona||y |nserted
here.
http:]]www.systoo|sgroup.com]

You might also like