Professional Documents
Culture Documents
Modul 7 OOP
Modul 7 OOP
APPLET
A. OBJECTIVE
Understanding interface as java program mean for web browser environment. Capable to
use applets features.
B. BASIC THEORY
Applet is subclass of panel located in java.applet package, that why things that capable
located in panel also capable to be located in applet.
As a program mean for web browser, applet is equipped with feature to communicating
with host server and more, communicating between applets. Although applet was not
created for doing operation to client computer for safety.
C. TOOLS
1. Computer.
2. NetBeans application.
3. Web browser.
D. EXPERIMENT
1.
EXPERIMENT 1.
1.1. WORK STEPS
1.1.1.Create new interface named as IntLampu.java, write down the following source
code:
public class TesApplet extends Applet{
public void paint (Graphics grafik){
Font font=new Font("sanserif",Font.BOLD,20);
grafik.setFont(font);
grafik.setColor(Color.blue);
int xPusat=this.getSize().width/2;
int yPusat=this.getSize().height/2;
String ucapan="Selamat Belajar Java Applet";
FontMetrics fontMetrics=this.getFontMetrics(font);
int posisiX=xPusat-(fontMetrics.stringWidth(ucapan)/2);
grafik.drawString(ucapan,posisiX,yPusat);
}
}
1.1.2.Compile and run the program code above.
1.1.3.After the program is compiled, file TesApplet.class will appears. This is the file will
be called in HTML script.
1.1.4.Next create HTML file as following code, save as TesApplet.html.
<html>
<head>
<title>Selamat Belajar Java Applet</title>
</head>
<body>
<h1>Tes Applet</h1>
<applet code="TesApplet.class" width=250 height=80>
</applet>
<br>
</body>
</html>
1.1.5.Run the HTML file through web browser.
1.2. EXERCISES
1.2.1.Modify TesApplet.java file.
public class NewApplet extends Applet{
public void paint (Graphics grafik){
Font font=new Font("sanserif",Font.BOLD,20);
grafik.setFont(font);
grafik.setColor(Color.blue);
int xPusat=50;
int yPusat=this.getSize().height/2;
String ucapan="Nama:Dykson A. Rizky Pradana";
String ucapan1="Nim:L200134023";
String ucapan2="Smt:4";
FontMetrics fontMetrics=this.getFontMetrics(font);
grafik.drawString(ucapan,xPusat,yPusat-20);
grafik.drawString(ucapan1,xPusat,yPusat);
grafik.drawString(ucapan2,xPusat,yPusat+20);
}
}
1.2.2.Next compile so the result shows as below:
2.
Nama
: Your name
Nim
: Your student id
Smt
: current smester
EXPERIMENT 2
2.1. WORK STEPS
2.1.1. Create a java file named as TesParameter1.java", write down the program
code below:
import java.awt.*;
import java.applet.Applet;
public class TesParameter1 extends Applet {
String nama;
public void init(){
nama=getParameter("nama");
if(nama==null)
nama="coy";
nama="hai "+nama+"!!!";
}
public void paint (Graphics grafik){
grafik.drawString(nama,10,25);
}
}
2.1.2. Compile the program code.
2.1.3. Next create HTML file for accessing applet in first experiment, write down the
following code:
<html>
<head>
<title>Akses Parameter</title>
</head>
<body>
<applet code="TesParameter2.class" width=250 height=200>
<param name=nama value="Dedi Gunawan">
</applet>
</body>
</html>
2.1.4. Save file as aksesparameter.html.
2.1.5.
E.
ANALYSIS
In second experiment we studied about passing a parameter which is sent by java applet throught
HTML file using <PARAM> tag.
F. TASK
1. Create java applet that is showing name, student id, course, term.
2. Use parameter.
Solution:
public class NewApplet extends Applet{
String nama,nim,jurusan,semester;
public void init(){
nama=getParameter("nama");
nim=getParameter("nim");
jurusan=getParameter("jurusan");
semester=getParameter("semester");
nama="nama: "+nama;
nim="nim: "+nim;
jurusan="jurusan: "+jurusan;
semester="semester: "+semester;
}
public void paint(Graphics grafik){
grafik.drawString(nama,10,25);
grafik.drawString(nim,10,35);
grafik.drawString(jurusan,10,45);
grafik.drawString(semester,10,55);
}
}
<html>
<head>
<title>New Applet</title>
</head>
<body>
<applet codebase="classes" code="TesApplet/NewApplet.java" width=250
height=200 >
<param name="nim" value="L200134023" >
<param name="nama" value="DK" >
<param name="jurusan" value="informatika" >
<param name="semester" value="4" >
</applet>
</body>
</html>
3. Analyze the program code.
Program code analyze: there are four variables as parameters created in NewApplet.java
class. The program code allow null result if there is no value for each parameter. Since there
are four parameters so we call all of them in html file to show the detail information.