Title: Getting Started
1Getting Started
- Cognitive Systems Modeling Lab.
2Contents
- JDK 1.2 Installation
- HelloWorldApp Application
- HelloWordl Applet
3JDK 1.2 Installation
- JDK
- Java Development Kit
- Notepad
- Microsoft - Visual J
- SYMANTEC - Visual Café
- Inprise - Borland JBuilder
- Oracle - JDeveloper
- Metrowerks - CodeWarrior Professional
4JDK 1.2 Installation
5JDK 1.2 Installation
6JDK 1.2 Installation
7JDK 1.2 Installation
8JDK 1.2 Installation
9JDK 1.2 Installation
10JRE 1.2 Installation
11JDK 1.2 Installation
12JDK 1.2 Installation
13Hello World Application
- Create a Java Source File
- Compile the Source File
- Run the Application
14Create a Java Source File
- class HelloWorldApp
- public static void main(String args)
- System.out.println("Hello World!")
//Display the string. -
-
15The Anatomy of a Java Application
- Defining a Class
- The main Method
- Using Classes and Objects
16Defining a Class
- class HelloWorldApp . . . .
-
- Class Cefinition
- class name . . .
17The main Method
- public static void main(String args)
- . . . .
- Every Java application must contain a main method
- public indicates that the main method can be
called by any object - static indicates that the main method is a class
method - void indicates that the main method doesn't
return any value
18Using Classes and Objects
- System.out.println("Hello World!")
- System.out is the full name of the out variable
in the System class - an instance of the PrintStream class
- a class provided with the Java development
environment - When the System class is loaded into the
application, it instantiates PrintStream and
assigns the new PrintStream object to the out
class variable - Now we have an instance of a class, we can call
one of its instance methods
19Create a Java Source File
- Using a text editor, create a file named
HelloWorldApp.java
20Compile the Source File
- UNIX javac HelloWorldApp.java
- Win32 javac HelloWorldApp.java
21Run the Application
- UNIX java HelloWorldApp
- Win32 java HelloWorldApp
22Hello World Applet
- Create a Java Source File
- Compile the Source File
- Create an HTML File that Includes the Applet
- Run the Applet
23Create a Java Source File
- import java.applet.Applet
- import java.awt.Graphics
- public class HelloWorld extends Applet
- public void paint(Graphics g)
- g.drawString("Hello world!", 50, 25)
-
24The Anatomy of a Java Applet
- Importing Classes and Packages
- Defining an Applet Subclass
- Implementing Applet Methods
- Running an Applet
25Importing Classes and Packages
- The first two lines of the following listing
import two classes used in the applet - import java.applet.Applet
- Import java.awt.Graphics
- The java.applet package
- classes that are essential to Java applets
- The java.awt package
- the most frequently used classes in the Abstract
Window Toolkit (AWT), which provides the Java
graphical user interface (GUI)
26Importing Classes and Packages
- public class HelloWorld extends
java.applet.Applet public void
paint(java.awt.Graphics g) g.drawString("Hello
world!", 50, 25)
27Defining an Applet Subclass
- public class HelloWorld extends Applet .
- extends indicates that HelloWorld is a subclass
of the Applet class - Applet class
- browser loads a page containing an applet, the
browser sends a request to the applet, telling
the applet to initialize itself and start
executing
28Implementing Applet Methods
- implement the paint method
- public void paint(Graphics g)
g.drawString("Hello world!", 50, 25) - applets can implement two more methods
29Running an Applet
- ltAPPLETgt tag
- browser should load the class whose compiled code
HelloWorld.class - browser looks for this file in the same directory
as the HTML document that contains the tag - browser creates an instance of the class
- If you include an applet twice in one page, the
browser loads the class file once and creates two
instances of the class. - The WIDTH and HEIGHT attributes are like the same
attributes in an ltIMGgt tag
30Create a Java Source File
- Create a file named HelloWorld.java
31Compile the Source File
- UNIX javac HelloWorld.java
- Win32 javac HelloWorld.java
32Create an HTML File that Includes the Applet
- ltHTMLgt
- ltHEADgt
- ltTITLEgt A Simple Program lt/TITLEgt
- lt/HEADgt
- ltBODYgt
- Here is the output of my program
- ltAPPLET CODE"HelloWorld.class" WIDTH150
HEIGHT25gt - lt/APPLETgt
- lt/BODYgt
- lt/HTMLgt
33Create an HTML File that Includes the Applet
34Run the Applet