Download - 1. Java Applications and Applets
-
8/14/2019 1. Java Applications and Applets
1/48
-
8/14/2019 1. Java Applications and Applets
2/48
2008, University of Colombo School of Computing 2
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
-
8/14/2019 1. Java Applications and Applets
3/48
2008, University of Colombo School of Computing 3
What is an Application
type of Java Program
-
8/14/2019 1. Java Applications and Applets
4/48
-
8/14/2019 1. Java Applications and Applets
5/48
2008, University of Colombo School of Computing 5
An ApplicationStartStarta suitablea suitable
We are going to use theNotepadutility as the Editor(ie. To type the Java source
program)
-
8/14/2019 1. Java Applications and Applets
6/48
2008, University of Colombo School of Computing 6
An ApplicationIntroduce a classIntroduce a class
class FirstApp{
}
-
8/14/2019 1. Java Applications and Applets
7/48
2008, University of Colombo School of Computing 7
An ApplicationStartStarta suitablea suitable
So here we have opened
an untitled notepad
document
-
8/14/2019 1. Java Applications and Applets
8/48
2008, University of Colombo School of Computing 8
An ApplicationIntroduce the main methodIntroduce the main method
class FirstApp{
public static void main(String args[ ]) {
}
}
-
8/14/2019 1. Java Applications and Applets
9/48
2008, University of Colombo School of Computing 9
An ApplicationLets declare two variablesLets declare two variables
class FirstApp{
public static void main(String args[ ]) {
int myNum1,myNum2;
}
}
-
8/14/2019 1. Java Applications and Applets
10/48
2008, University of Colombo School of Computing 10
An ApplicationLets declare two variablesLets declare two variables
class FirstApp{
public static void main(String args[ ]) {
int myNum1,myNum2;
}
}myNum1
myNum2
-
8/14/2019 1. Java Applications and Applets
11/48
2008, University of Colombo School of Computing 11
An ApplicationLets initialize two variablesLets initialize two variables
class FirstApp{
public static void main(String args[ ]) {
int myNum1,myNum2;
myNum1=3; myNum2=6;
}}
-
8/14/2019 1. Java Applications and Applets
12/48
2008, University of Colombo School of Computing 12
An ApplicationLets initialize two variablesLets initialize two variables
class FirstApp{
public static void main(String args[ ]) {
int myNum1,myNum2;
myNum1=3; myNum2=6;
}
}
myNum1
myNum2
-
8/14/2019 1. Java Applications and Applets
13/48
2008, University of Colombo School of Computing 13
An ApplicationAdd two values and display the total on the screenAdd 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));
}
}
-
8/14/2019 1. Java Applications and Applets
14/48
2008, University of Colombo School of Computing 14
An ApplicationAdd two values and display them on the screenAdd two values and display them on the screen
class Application{
public static void main(String args[]) {
int myNum1,myNum2;myNum1=3;myNum2=6;
System.out.println(Result+(myNum1+myNum2));
}
}
Total 9
-
8/14/2019 1. Java Applications and Applets
15/48
2008, University of Colombo School of Computing 15
An Application 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
When saving the fileWhen saving the file
-
8/14/2019 1. Java Applications and Applets
16/48
2008, University of Colombo School of Computing 16
An ApplicationSave the fileSave the file
class FirstApp{
public static void main(String args[]) {
int myNum1,myNum2;myNum1=3;myNum2=6;
System.out.println(Total+(myNum1+myNum2));
}
}
-
8/14/2019 1. Java Applications and Applets
17/48
2008, University of Colombo School of Computing 17
An Application javac FirstApp.javaWhen compilingWhen compiling
JavaCompiler
o
utp
ut
FirstApp.class
-
8/14/2019 1. Java Applications and Applets
18/48
2008, University of Colombo School of Computing 18
An Application java FirstAppWhen executingWhen executing
JavaInterpreter
outp
ut
-
8/14/2019 1. Java Applications and Applets
19/48
2008, University of Colombo School of Computing 19
What is an Applet
Tiny Java program, dynamicallydownloaded across the network
An intelligent program not just an
animation or media file
It can react to user input and dynamically
change
-
8/14/2019 1. Java Applications and Applets
20/48
2008, University of Colombo School of Computing 20
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
-
8/14/2019 1. Java Applications and Applets
21/48
2008, University of Colombo School of Computing 21
Your first applet
Lets sayLets say HelloHellousing an appletusing an applet
Open a suitable
editor to work with
Here we use notepad
utility
-
8/14/2019 1. Java Applications and Applets
22/48
2008, University of Colombo School of Computing 22
Applets It begins with two import statements
Use import statement to bring certain classes, orentire 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 basedgraphical interface
-
8/14/2019 1. Java Applications and Applets
23/48
2008, University of Colombo School of Computing 23
Applets
The second import statement importsthe applet package
It contains the class Applet
Every applet that you create must be asubclassof Applet What is a subclass?
We call it inheritance, getting features of asuper class to a sub class
-
8/14/2019 1. Java Applications and Applets
24/48
2008, University of Colombo School of Computing 24
import java.awt.*;
import java.applet.*;
Lets say Hellousing an applet
Your first appletYour first applet
-
8/14/2019 1. Java Applications and Applets
25/48
2008, University of Colombo School of Computing 25
Extend(Inherit)
the existing features from
the applet class to our own
Lets sayLets say HelloHellousing an appletusing an appletYour first appletYour first applet
-
8/14/2019 1. Java Applications and Applets
26/48
2008, University of Colombo School of Computing 26
Applets.. program declares the class MyApplet
This class must be declared as publicbecause it will be accessed by code that isoutside the program
-
8/14/2019 1. Java Applications and Applets
27/48
2008, University of Colombo School of Computing 27
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet {
}
Our className
Lets sayLets say HelloHellousing an appletusing an appletYour first appletYour first applet
-
8/14/2019 1. Java Applications and Applets
28/48
2008, University of Colombo School of Computing 28
Make use of the paint method in theApplet class
There is one parameter
which is an instance of the graphic
class in AWT
Lets sayLets say HelloHellousing an appletusing an appletYour first appletYour first applet
-
8/14/2019 1. Java Applications and Applets
29/48
2008, University of Colombo School of Computing29
Applets Inside MyApplet, paint() method is
declared paint() is called each time the applet must
redisplay its output
paint method has one parameter of typeGraphics
This parameter contains the graphicscontent
-
8/14/2019 1. Java Applications and Applets
30/48
2008, University of Colombo School of Computing30
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet {
public void paint(Graphics g) {
}
}
Lets sayLets say HelloHellousing an appletusing an appletYour first appletYour first applet
-
8/14/2019 1. Java Applications and Applets
31/48
2008, University of Colombo School of Computing
31
Your first applet
Draw the string
on the graphical user interface
Lets sayLets say HelloHellousing an appletusing an applet
-
8/14/2019 1. Java Applications and Applets
32/48
2008, University of Colombo School of Computing
32
Applets Inside the paint() method is a call to
drawString(), which is a member methodof the Graphics class
This method outputs a string beginning atthe specified x,y location (coordinates)
It has the following general form
Void drawString(String message, int x , int y)
-
8/14/2019 1. Java Applications and Applets
33/48
2008, University of Colombo School of Computing
33
In a Java windows upper left
corner is location 0,0
0,0
Java window
-
8/14/2019 1. Java Applications and Applets
34/48
2008, University of Colombo School of Computing
34
Your first applet
import java.applet.*;import java.awt.*;
public class MyApplet extends Applet {
public void paint(Graphics g) {g.drawString(Hello, 20, 20);
}
}
Lets sayLets say HelloHellousing an appletusing an applet
-
8/14/2019 1. Java Applications and Applets
35/48
2008, University of Colombo School of Computing
35
Your first applet
import java.applet.*;import java.awt.*;public class MyApplet extends Applet {
public void paint(Graphics g) {
g.drawString(Hello, 20, 20);
}
}
Lets sayLets say HelloHellousing an appletusing an applet
-
8/14/2019 1. Java Applications and Applets
36/48
2008, University of Colombo School of Computing
36
Your first applet
Save the file giving the class name
as the file name with .java fileextension
Lets sayLets say HelloHellousing an appletusing an applet
-
8/14/2019 1. Java Applications and Applets
37/48
2008, University of Colombo School of Computing
37
Your first applet
Compile the source file
Lets sayLets say HelloHellousing an appletusing an applet
-
8/14/2019 1. Java Applications and Applets
38/48
2008, University of Colombo School of Computing
38
Your first applet
Lets sayLets say HelloHellousing an appletusing an applet
Java compiler
-
8/14/2019 1. Java Applications and Applets
39/48
2008, University of Colombo School of Computing
39
Your first applet
Lets sayLets say HelloHellousing an appletusing an applet
Can you do this?
-
8/14/2019 1. Java Applications and Applets
40/48
2008, University of Colombo School of Computing
40
Applets..
Notice that the applet does not have a
main() method unlike java applicationprograms
Applet cannot be run from thecommand prompt like we do anapplication type of java program
-
8/14/2019 1. Java Applications and Applets
41/48
2008, University of Colombo School of Computing 41
Running an applet There are two ways one can run an applet
Executing the applet within a Java compatibleweb browser such as Internet Explorer orNetscape Navigator
Using an appletviewer
-
8/14/2019 1. Java Applications and Applets
42/48
2008, University of Colombo School of Computing 42
Running an appletHTML( Hyper Text Markup Language)
Basics To execute an applet in a web browser or
appletviewer write a HTML text file usingnotepad
-
8/14/2019 1. Java Applications and Applets
43/48
2008, University of Colombo School of Computing 43
My first applet
Structure of HTML fileStructure of HTML file
-
8/14/2019 1. Java Applications and Applets
44/48
2008, University of Colombo School of Computing 44
Running an applet
Within the body tag include the applet tag
Then save the file giving .html or .htm fileextension with any name (Myapp.html)
-
8/14/2019 1. Java Applications and Applets
45/48
2008, University of Colombo School of Computing 45
Running an applet
Executing the applet within a Javacompatible web browser such as InternetExplorer
R i l t
-
8/14/2019 1. Java Applications and Applets
46/48
2008, University of Colombo School of Computing 46
Running an applet
Using a java compatible browserUsing a java compatible browser
-
8/14/2019 1. Java Applications and Applets
47/48
2008, University of Colombo School of Computing 47
Running an applet Using appletviewer
To execute the MyAppletin the applet viewerone has to use an html file
Type the following command in the command
promptE.g.:
C:\java>appletviewer myapp.html
R i l t
-
8/14/2019 1. Java Applications and Applets
48/48
2008, University of Colombo School of Computing 48
Running an applet