Vous êtes sur la page 1sur 4

ELECTRICAL ENGINEERING DEPARTMENT E4162 Programming II

PRACTICAL 7
Title Objective : : Java Applet The objective of this exercise is to expose the students to Java Applet Development: 1. Create a source file 2. Create an HTML file 3. Compile the source file into a .class file 4. Viewing your Applet in 2 ways 1. Java Standard Edition Development Kit (JDK 6) 2. Plain text editor Windows Notepad 3. Personal Computer AST : 7.1.2 L, M, 7.1.3 L

Apparatus

Procedures: Creating Your First Java Applet

Your first applet, IamNewToJava, will simply display the greeting "Hey you! Welcome to the world of Java Programming". To create this program, you will: 1. Create A Source File First, start your Notepad editor and type the following code:

import javax.swing.JApplet; import java.awt.Graphics; public class IamNewToJava extends JApplet { public void paint (Graphics g) { g.drawRect (0,0, getSize ().width - 1, getSize ().height - 1); g.drawString ("Hey you! Welcome to the world of Java Programming !!", 10, 20); } } Save the code with the name IamNewToJava.java (including the quotation marks). From the Save As Type combo box, choose All Files (*.*). In the Encoding combo box, leave the encoding as ANSI. Click the Save button and exit Notepad. 1

2. Creating An HTML File You also need an HTML file to accompany your Applet. Type the following code into a new Notepad document :

<HTML> <HEAD>

<TITLE>This is a Simple Java Applet program</TITLE></HEAD> <BODY> Here is the output of your program : <p> <APPLET CODE = "IamNewToJava.class" WIDTH=320 HEIGHT = 30> </APPLET> </p> </BODY>

</HTML>

Save this code to a file name MyWebPage.html

3. Compile The Source File Into A .class File Click Start > Run and then enter cmd. The shell window should appear. To compile your source file, CHANGE the CURRENT DIRECTORY to the directory where your file is located. For example if you save your file in C:\Java, type the following command at the prompt and press Enter:

cd C:\java Now you are ready to compile. At the prompt, type the following command and press Enter:

javac IamNewToJava.java The compiler has generated a bytecode file, IamNewToJava.class. At the prompt, type dir to see the new file that was generated. Now, that you have a IamNewToJava.class file, you can view the result.

4. 2 Ways To View Your Applet In the same directory, enter the following command at the prompt : AppletViewer MyWebPage.html The next figure shows as below :

You can also view your Applet using a Web Browser. i.e : Internet Explorer Open the file where your file is located. The result is as below :

Congratulations ! Your Applet works !

5. ASSIGNMENT Try this Applet and get the result. A ClockApplet applet :

AST : 7.1 M

import java.applet.*; import java.awt.*; import java.util.*; public class ClockApplet extends Applet implements Runnable { Thread t,t1; public void start () { t=new Thread (this); t.start (); } public void run () { t1=Thread.currentThread (); while (t1==t) { repaint(); try { t1.sleep (1000);} catch (InterruptedException e) {} } } public void paint (Graphics g) { Calendar cal=new GregorianCalendar(); String hour = String.valueOf(cal.get(Calendar.HOUR)); String minute= String.valueOf(cal.get(Calendar.MINUTE)); String second = String.valueOf(cal.get(Calendar.SECOND)); g.drawString(hour + ":"+minute + ":" + second, 20,30); } }

Search TWO (2) new Java Applet Application through an Internet or books and print out the coding and output. Write the URL address of your search.

Vous aimerez peut-être aussi