1. Java Applications and Applets
© 2008, University of Colombo School of Computing
1
Applications and Applets • Applications and Applets both are programs written in Java • Depending on the way these programs are run/executed they vary from each other
© 2008, University of Colombo School of Computing
2
What is an Application type of Java Program
© 2008, University of Colombo School of Computing
3
An Application… Start a suitable
© 2008, University of Colombo School of Computing
4
An Application… Start a suitable
We are going to use the Notepad utility as the Editor (ie. To type the Java source program) © 2008, University of Colombo School of Computing
5
An Application… Introduce a class class FirstApp{
} © 2008, University of Colombo School of Computing
6
An Application… Start a suitable
So here we have opened an untitled notepad document
© 2008, University of Colombo School of Computing
7
An Application… Introduce the main method class FirstApp{ public static void main(String args[ ]) {
} } © 2008, University of Colombo School of Computing
8
An Application… Lets declare two variables class FirstApp{ public static void main(String args[ ]) { int myNum1,myNum2; } }
© 2008, University of Colombo School of Computing
9
An Application… Lets declare two variables class FirstApp{ public static void main(String args[ ]) { int myNum1,myNum2; } }
myNum2 myNum1
© 2008, University of Colombo School of Computing
10
An Application… Lets initialize two variables class FirstApp{ public static void main(String args[ ]) { int myNum1,myNum2; myNum1=3; myNum2=6; } } © 2008, University of Colombo School of Computing
11
An Application… Lets initialize two variables class FirstApp{ public static void main(String args[ ]) { int myNum1,myNum2; myNum1=3; myNum2=6; } }
myNum2 myNum1 © 2008, University of Colombo School of Computing
12
An Application… Add two values and display the total on the screen class FirstApp{ public static void main(String args[])
{
int myNum1,myNum2; myNum1=3;myNum2=6; System.out.println(“Total”+(myNum1+myNum2)); } } © 2008, University of Colombo School of Computing
13
An Application… Add two values and display them on the screen class Application{ public static void main(String args[])
{
int myNum1,myNum2; myNum1=3;myNum2=6; Total 9 System.out.println(“Result”+(myNum1+myNum2)); } } © 2008, University of Colombo School of Computing
14
An Application… When saving the file
• • • •
File name must agree with the class name File extension is .java So our file name is FirstApp.java Java is case sensitive – FirstApp and firstapp are two different words in Java
© 2008, University of Colombo School of Computing
15
An Application… Save the file class FirstApp{ public static void main(String args[])
{
int myNum1,myNum2; myNum1=3;myNum2=6; System.out.println(“Total”+(myNum1+myNum2)); } } © 2008, University of Colombo School of Computing
16
An Application… When compiling
• javac FirstApp.java t u tp u o
Java Compiler
FirstApp.class
© 2008, University of Colombo School of Computing
17
An Application… When executing
• java FirstApp
t u tp u o
Java Interpreter © 2008, University of Colombo School of Computing
18
What is an Applet • Tiny Java program, dynamically downloaded across the network • An intelligent program not just an animation or media file • It can react to user input and dynamically change
© 2008, University of Colombo School of Computing
19
Applets… • • • • •
They are on the Internet server Transported over the Internet Automatically installed Run as part of a web document After arriving in the client machine it has limited access to resources of the client machine – high security © 2008, University of Colombo School of Computing
20
Your first applet Lets say Hello using an applet Open a suitable editor to work with Here we use notepad utility © 2008, University of Colombo School of Computing
21
Applets • It begins with two import statements – Use import statement to bring certain classes, or entire packages into visibility
• The first statement imports the Abstract Window Toolkit(AWT) package • Applets interact with the user through the AWT, not through the console-based I/O classes • The AWT contains support for a window based graphical interface © 2008, University of Colombo School of Computing
22
Applets • The second import statement imports the applet package • It contains the class Applet • Every applet that you create must be a subclass of Applet – What is a subclass…? We call it inheritance, getting features of a super class to a sub class © 2008, University of Colombo School of Computing
23
Your first applet…
Lets say Hello using an applet
import java.awt.*; import java.applet.*;
© 2008, University of Colombo School of Computing
24
Your first applet… Lets say Hello using an applet
Extend(Inherit) the existing features from the applet class to our own
© 2008, University of Colombo School of Computing
25
Applets….. • program declares the class MyApplet • This class must be declared as public because it will be accessed by code that is outside the program
© 2008, University of Colombo School of Computing
26
Your first applet… Lets say Hello using an applet import java.applet.*; import java.awt.*;
public class MyApplet extends Applet {
}
Our class Name
© 2008, University of Colombo School of Computing
27
Your first applet… Lets say Hello using an applet Make use of the paint method in the Applet class There is one parameter which is an instance of the graphic class in AWT
© 2008, University of Colombo School of Computing
28
Applets • Inside MyApplet, paint() method is declared • paint() is called each time the applet must redisplay its output • paint method has one parameter of type Graphics • This parameter contains the graphics content © 2008, University of Colombo School of Computing
29
Your first applet… Lets say Hello using an applet import java.applet.*; import java.awt.*; public class MyApplet extends Applet { public void paint(Graphics g) {
} }
© 2008, University of Colombo School of Computing
30
Your first applet… Lets say Hello using an applet Draw the string
on the graphical user interface
© 2008, University of Colombo School of Computing
31
Applets • Inside the paint() method is a call to drawString(), which is a member method of the Graphics class • This method outputs a string beginning at the specified x,y location (coordinates) • It has the following general form Void drawString(String message, int x , int y)
© 2008, University of Colombo School of Computing
32
In a Java windows upper left corner is location 0,0 0,0 Java window
© 2008, University of Colombo School of Computing
33
Your first applet… Lets say Hello using an applet import java.applet.*; import java.awt.*; public class MyApplet extends Applet { public void paint(Graphics g) { g.drawString(“Hello”, 20, 20); } }
© 2008, University of Colombo School of Computing
34
Your first applet… Lets say Hello using an applet import java.applet.*; import java.awt.*; public class MyApplet extends Applet { public void paint(Graphics g) { g.drawString(“Hello”, 20, 20); } } © 2008, University of Colombo School of Computing
35
Your first applet… Lets say Hello using an applet
Save the file giving the class name as the file name with .java file extension
© 2008, University of Colombo School of Computing
36
Your first applet… Lets say Hello using an applet
Compile the source file
© 2008, University of Colombo School of Computing
37
Your first applet… Lets say Hello using an applet Java compiler
© 2008, University of Colombo School of Computing
38
Your first applet… Lets say Hello using an applet
Can you do this…? © 2008, University of Colombo School of Computing
39
Applets.. • Notice that the applet does not have a main() method unlike java application programs • Applet cannot be run from the command prompt like we do an application type of java program
© 2008, University of Colombo School of Computing
40
Running an applet • There are two ways one can run an applet – Executing the applet within a Java compatible web browser such as Internet Explorer or Netscape Navigator – Using an appletviewer
© 2008, University of Colombo School of Computing
41
Running an applet HTML( Hyper Text Markup Language) Basics • To execute an applet in a web browser or appletviewer write a HTML text file using notepad
© 2008, University of Colombo School of Computing
42
Structure of HTML file
My first applet © 2008, University of Colombo School of Computing
43
Running an applet • Within the body tag include the applet tag
• Then save the file giving .html or .htm file extension with any name (Myapp.html) © 2008, University of Colombo School of Computing
44
Running an applet • Executing the applet within a Java compatible web browser such as Internet Explorer
© 2008, University of Colombo School of Computing
45
Running an applet Using a java compatible browser
© 2008, University of Colombo School of Computing
46
Running an applet • Using appletviewer – To execute the MyApplet in the applet viewer one has to use an html file – Type the following command in the command prompt E.g.:
C:\java>appletviewer myapp.html
© 2008, University of Colombo School of Computing
47
Running an applet
© 2008, University of Colombo School of Computing
48