MODUL 7
APPLET
A. Objective
To understand how was java applet run.
B. Basic Theory
Applet
is a java program which has a purpose to web browser environment. Applet is a
subclasses of Panel which located in java.applet package, it make things which
can located in panel also can located in applet.
As
a program for web browser, applet completed with fitur to comunicate with host
server and between applet. But, applet designed not for do an operation to
client computer because a security reason.
C. Tools and
Materials
·
Laptop
·
NetBeans Software
·
Java Development Kit (JDK)
D. Steps
1. Make a new class
applet file named “TesApplet.java” , write down program code as below
/**
*
* @author DESY
*/
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);
}
}
2. Compile and
run the following program code
3. After the
program compiled will shown a file named “TesApplet.class”. this file will
called in every HTML script.
4. Next, make a
HTML file as code below, 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 = 8>
</applet>
<br>
</body>
</html>
5.
Run the HTML file via web browser
Exercices
1.
Modify file TesApplet.java
2.
Next, compile, so the result will shown
Name :
Desy Puspitasari
NIM :
L200134021
Smt :
4
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
/**
*
* @author DESY
*/
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 = "Nama : Desy Puspitasari";
String ucapan1 = "NIM : L200134021";
String ucapan2 = "Smt : 4";
FontMetrics fontMetrics = this.getFontMetrics(font);
int
posisiX = xPusat-(fontMetrics.stringWidth(ucapan)/2);
grafik.drawString(ucapan, posisiX, yPusat);
grafik.drawString(ucapan1, posisiX, yPusat+30);
grafik.drawString(ucapan2, posisiX,
yPusat+60);
}
}
The Result:
2nd Experiment
1.
Make a java file named “TesParameter1.java”,
write down the program code below
/**
*
* @author DESY
*/
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.
Compile the following code
3.
Next, make the HTML file to access following
applet, as below
<html>
<head>
<title>Akses Parameter</title>
</head>
<body>
<applet code =
"TesParameter2.class" widht=250 height=200>
<param name = nama
value="Dedi Gunawan">
</applet>
</body>
</html>
4.
Save as the file “aksesparameter.html”
5.
Next, open the browser
Task
1.
Make a java applet file which shown name, nim,
major, semester
2.
Use parameter
3.
Analyze the program code.
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
/**
*
* @author DESY
*/
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 = "Nama
: Desy Puspitasari";
String ucapan1 = "NIM
: L200134021";
String ucapan2 = "Prodi
: Informatika";
String ucapan3 = "Smt
: 4";
FontMetrics fontMetrics = this.getFontMetrics(font);
int posisiX = xPusat-(fontMetrics.stringWidth(ucapan)/2);
grafik.drawString(ucapan, posisiX, yPusat);
grafik.drawString(ucapan1, posisiX, yPusat+30);
grafik.drawString(ucapan2, posisiX, yPusat+60);
grafik.drawString(ucapan3, posisiX, yPusat+90);
}
}
The Result:
Tidak ada komentar:
Posting Komentar