Vous êtes sur la page 1sur 28

APPLET

By : DHANIS PUSPA M (26262)

BAHASAN
PENDAHULUAN
BEDA APLIKASI DAN APPLET
MEMBUAT APPLET
MEMASUKKAN APPLET KE

HALAMAN WEB
APPLET TAG
JAVA ARCHIVE
PASSING PARAMETERS TO
APPLETS

APPLET - PENDAHULUAN
Program Java yang berada pada

halaman Web yang kompatibel


terhadap Java
Seperti program aplikasi, namun tidak
standalone
Program yang dapat bereaksi
terhadap masukan dari user dan
berubah secara dinamis

BEDA APLIKASI - APPLET


Aplikasi standalone,

bisa di-run dg Java


interpreter (command line).
Pada applet, Java interpreter di-built pada
browser (java plug-in) dan run file class
Java dari sana.
Applet memiliki akses ke struktur yang
disediakan browser,
Applet memiliki beberapa restriksi untuk
kepentingan sekuritas

MEMBUAT APPLET
Buat subclass dari class

Applet dengan
inisialisasi sebagai berikut:
public class myClass extends
java.applet.Applet {
...
}

Method-method penting pada eksekusi

applet (init, start, stop, destroy)


Contoh applet sederhana

Life Cycle pada Applet


Inisialisasi

Muncul saat applet di-load


public void init() {
...
}

Starting

Dipanggil setelah init & menjadi titik awal


setelah applet dihentikan
public void start() {
...
}

Stop

Dipanggil saat browser meninggalkan


dokumen HTML yang berisi applet
public void stop() {
...
}

Destroy

Dipanggil jika applet perlu dihapus dari


memori (otomatis saat browser shut down)
public void destroy() {
...
}

Code Applet Hello Again


11 :import
:import java.awt.Graphics;
java.awt.Graphics;
22 :import
:import java.awt.Font;
java.awt.Font;
33 :import
:import java.awt.Color;
java.awt.Color;
44 ::
55 :public
:public class
class HelloAgainApplet
HelloAgainApplet extends
extends
java.applet.Applet
java.applet.Applet {{
66 ::
77 :: Font
Font ff == new
new Font("TimesRoman",
Font("TimesRoman",
Font.BOLD,
Font.BOLD, 36);
36);
88 ::
99 :: public
public void
void paint(Graphics
paint(Graphics g)
g) {{
10
g.setFont(f);
10 ::
g.setFont(f);
11
g.setColor(Color.red);
11 ::
g.setColor(Color.red);
12
g.drawString("Hello
12 ::
g.drawString("Hello again!",
again!", 5,
5, 40);
40);
13
13 :: }}
14
14 :}
:}

Eksekusi Applet Applet Viewer


Compile file source menjadi file class
Buat file HTML.

Terdapat tag HTML tertentu


yang digunakan untuk menampilkan applet
Browser akan menggunakan informasi yang
dikandung tag tersebut untuk alokasi class file
dan eksekusi applet
Enter appletviewer HelloAgainApplet.html
pada command line

Tampilan Applet

Halaman HTML Sederhana


1:
2:
3:
4:
5:
6:
7:
8:

9:
10:
11:

<HTML>
<HEAD>
<TITLE>This page has an applet on it
</TITLE>
</HEAD>
<BODY>
<P>My second Java applet says:<BR>
<APPLET CODE="HelloAgainApplet.class"
WIDTH=200 HEIGHT=50>
alt="Your browser understands the APPLET tag
but isn't running the applet, for some reason."
Your browser is completely ignoring
the &lt;APPLET&gt; tag!
</APPLET>
</BODY>
</HTML>

ATRIBUT HTML - APPLET


Positioning
LEFT, RIGHT, BOTTOM, TOP, TEXTTOP, MIDDLE,
ABSMIDDLE, BASELINE, ABSBOTTOM, VSPACE,
HSPACE, ALIGN, WIDTH, HEIGHT

Code
CODE, CODEBASE, ARCHIVE, OBJECT, NAME

Java-Challenged Viewer
ALT (MENAMPILKAN PESAN ADANYA APPLET JAVA
PADA SUATU HALAMAN WEB)

Applet LEFT align

Option Alignment Applet

Vertical-Horizontal Space

VSPACE=50 HSPACE=10

TAG HTML - APPLET


Text antara tag <APPLET> dan

</APPLET> ditampilkan browser jika


unable Java
Sehingga untuk browser unable Java tidak
hanya melihat blank page

Memasukkan Applet pada Web Page


Membuat halaman Web yang akan

menampilkan applet dengan


menggunakan bahasa HTML
Load file HTML ke browser untuk
menampilkan applet

Tampilan pada Browser

Aplikasi Applet
Buat halaman HTML untuk load applet

code
Suplai subclass dari class JApplet (public)
Hilangkan method main pada aplikasi
Pindahkan kode inisialisasi dari frame
window constructor ke method init applet
Hilangkan panggilan ke setsize, show
setTitle & setDefaultCloseOperation

Passing Parameters to Applets


Applet dapat menggunakan parameter pada

file HTML
HTML tag <PARAM>, dengan atribut NAME
dan VALUE yang ingin di-define:
<APPLET CODE="MyApplet.class" WIDTH=100 HEIGHT=100>
<PARAM NAME=font VALUE="TimesRoman">
<PARAM NAME=size VALUE="36">
A Java applet appears here.</APPLET>

Passing parameter ke applet

applet di-load

terjadi saat

The MoreHelloApplet class.

1: import java.awt.Graphics;
INSTANCE
2: import java.awt.Font;
VARIABLE
3: import java.awt.Color;
UNTUK NAME
4:
5: public class MoreHelloApplet extends java.applet.Applet {
6:
7: Font f = new Font("TimesRoman", Font.BOLD, 36);
8: String name;
9:
SET VALUE
10: public void init() {
UNTUK NAME
11:
name = getParameter("name");
DENGAN
12:
if (name == null)
METHOD INIT ()
13:
name = "Laura";
14:
15:
name = "Hello " + name + "!";
16: }
17:
MODIFY NAME
18: public void paint(Graphics g) {
UNTUK
19:
g.setFont(f);
MEMPEROLEH
20:
g.setColor(Color.red);
STRING LENGKAP
21:
g.drawString(name, 5, 40);
22: }
23: }

HTML
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

file untuk MoreHelloApplet applet.


<HTML>
<HEAD>
<TITLE>Hello!</TITLE>
</HEAD>
<BODY>
<P>
<APPLET CODE="MoreHelloApplet.class"
WIDTH=200 HEIGHT=50>
<PARAM NAME=name VALUE="Bonzo">
Hello to whoever you are!
</APPLET>
</BODY>
</HTML>

Tampilan MoreHelloApplet

Tampilan MoreHelloApplet
(null version)

SEKURITAS
Applet tidak bisa read atau write ke sistem

file pembaca, menghapus file atau melihat


program apa yang telah di-install di hard
drive.
Applet tidak bisa berhubungan dengan
server jaringan mana pun kecuali yang telah
menyimpan applet, untuk mencegah applet
menyerang sistem lain dari sistem pembaca.
Applets tidak bisa menjalankan programapa
pun pada sistem pembaca.
Applets tidak bisa me-load native program ke
local platform, termasuk shared libraries.

Positioning Attributes
ATTRIBUTE

WHAT IT DOES

LEFT

Places the applet at the left margin of the page. Text that follows
on the page goes in space to the right of the applet.

RIGHT

Places the applet at the right margin of the page. Text that follows
on the page goes in space to the left of the applet.

BOTTOM

Places the bottom of the applet at the bottom of the text in the
current line.

TOP

Places the top of the applet with the top of the current line.

TEXTTOP

Places the top of the applet with the top of the text in the current
line.

MIDDLE

Places the middle of the applet with the baseline of the current
line.

ABSMIDDLE

Places the middle of the applet with the middle of the current line.

BASELINE

Places the bottom of the applet with the baseline of the current
line.

ABSBOTTOM

Places the bottom of the applet with the bottom of the current line.

VSPACE, HSPACE

These optional attributes specify the number of pixels above and


below the applet (VSPACE) and on each side of the applet
(HSPACE).

ALIGN
WIDTH, HEIGHT

Specifies the alignment of the applet.


Gives the width and height of the applet, measured in pixels.

ATTRIBUTE
CODE

Code Attributes
WHAT IT DOES

Gives the name of the applets class file, which


taken relative to the codebase or relative to the
current page.

This optional attribute tells the browser that your


CODEBASE class files are found below the directory indicated
in the CODEBASE attribute.
ARCHIVE

This optional attribute lists the Java archive file or


files containing classes and other resources for
the applet.

OBJECT

Useful for implementing a persistent browser that


automatically reload its applets and has them
return to the same state that they were in where
the browser was closed.

NAME

Used to refer to the applet when scripting. Also


essential when you want two applets on the same
page to communicate with each other directly.

Vous aimerez peut-être aussi