Download Introduction to Java - Lancaster University

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Java
In this section we will learn how how to use Java and write our first Java Applet:
 The Java Development Kit
 Running a Java Program
 Java Applets
 Creating an Applet
 Creating the Web Page
 Running the Program
 Running in a Web Browser
 What does it mean?
 Things to remember
PHY281
Introduction to Java
Slide 1
The Java Development Kit
Java was developed by Sun Microsystems in 1995.
To use Java we will use the Java 2 Platform Standard Edition (J2SE)
Software Development Kit (J2SDK) (previously the Java Development Kit
(JDK)).
There are several (confusing) version of Java:
Java 1 : JDK 1.0.x, JDK 1.1.x
Java 2 : J2SDK 1.2.x, J2SDK 1.3.x, J2SDK 1.4.x, ...
We will be using J2SDK 1.4.0
Note: Not all web browsers support all versions.
You can download the JDK free from http://java.sun.com/
However for Windows it is 35MB plus 30MB for the documentation!
PHY281
Introduction to Java
Slide 2
Running a Java Application
You write
Java code
using an
editor
Text Editor
Java code:
MyProg.java
You run the
Java
compiler
'javac'
javac MyProg.java
Bytecode:
MyProg.class
You execute the
bytecode with
the command
'java'
PHY281
java MyProg
Introduction to Java
You save the
file with a
.java
extension
This creates
a file of
bytecode
with a .class
extension
Output
Slide 3
Java Applets
Applets are programs designed to run as part of a Web Page (Applet = little
application).
Applets are similar to normal Java Applications but have extra security
features to prevent a downloaded Applet damaging your computer or
transmitting information from it. For instance an Applet cannot:
•Access local files
•Delete local files
•Run another program
•Find out your name
•Connect to another host
PHY281
Introduction to Java
Slide 4
Running a Java Applet
You write Java
code using an
editor
Java code:
MyApp.java
Text Editor
You run the Java
compiler 'javac'
javac MyApp.java
You save the
file with a
.html extension
You write a
web page in
html using an
editor
Text Editor
You can view the applet with
the command 'appletviewer'
PHY281
Bytecode:
MyApp.class
Web page:
MyApp.html
appletviewer MyApp.html
Introduction to Java
You save the
file with a .java
extension
This creates a file of
bytecode with a
.class extension
You can view the web
page from a web browser
Web
Browser
Window
Slide 5
Creating an Applet
 Open "Notepad" (Start  Programs  Other  Notepad)
 Type this in:
 Save As
"Greetings.java"
(Put the " "
round the name
otherwise it
adds .txt to the
end!)
import java.awt.*;
import java.applet.Applet;
public class Greetings extends Applet {
public void paint(Graphics g) {
g.drawString("Hello World!", 50, 50);
}
}
 Open a DOS Window
(Start  MS-DOS Prompt)
G:\> javac Greetings.java
G:\>
 Type javac Greetings.java
 If you type dir Greetings.* you should see Greetings.java and Greetings.class
If it gives an error check you typed it in exactly right.
PHY281
Introduction to Java
Slide 6
Creating the Web Page
In order to run an applet you have to embed it in a web page using a special
<applet> tag e.g:
<applet code="name.class" width=www height=hhh></applet>
Using Notepad type in the following and
save it as "Greetings.html":
Size of the applet in pixels
<html>
<head>
<title>Greetings Applet</title>
</head>
<body>
<applet code="Greetings.class" width=300 height=200 ></applet>
</body>
</html>
PHY281
Introduction to Java
Slide 7
Running the Program
In the DOS window type appletviewer Greetings.html
G:\> appletviewer Greetings.html
You should see
something like this:
PHY281
Introduction to Java
Slide 8
Running in a Web Browser
In Netscape go to the File menu then Open Page ...
Press Choose File...
Find your file Greetings with the Netscape symbol alongside it
(Greetings.html) - click on it and press Open (or double click on it)
Back in the Open Page dialog press Open
You should see something like:
Title
Your greeting
Message
PHY281
Introduction to Java
Slide 9
What does it mean?
This line announces that the program
(class) can be run by anyone (public), is
called Greetings and is an Applet.
These 2 lines tell the computer to include
(import) two standard libraries awt
(Abstract Window Toolkit) and applet.
import java.awt.*;
import java.applet.Applet;
This line declares what
follows in the { } as a
method called paint.
public class Greetings extends Applet {
public void paint(Graphics g) {
g.drawString("Hello World!", 50, 50);
}
}
This line tells the computer to
display some text ( a string) on
the screen.
PHY281
This is what is displayed
Introduction to Java
This is where it is displayed in
pixels across and down from
the top left hand corner
Slide 10
Things to remember
Everything in Java is case sensitive - Paint is not the same as paint.
The name of the class i.e. Greetings should match the name of the file Greetings.java
(not greetings.java).
Curly brackets { and } are used to group parts of the program called blocks together.
Blocks can be nested inside other blocks but each { must be matched with a }.
Most statements require a semi-colon ; at the end. A statement can continue on the
next line if necessary.
Spaces are not important - it is recommended to indent blocks for clarity.
import java.awt.*;
import java.applet.Applet;
public class Greetings extends Applet {
public void paint(Graphics g) {
g.drawString("Hello World!", 50, 50);
}
}
PHY281
Introduction to Java
Slide 11
An easier way -Java IDE's
In this section we will briefly learn how about Integrated Development
Environments (IDE) for Java:
 What are IDE's
 Metrowerks CodeWarrior
 Borland JBuilder
 Eclipse
 PhysEdit
PHY281
Introduction to Java
Slide 12
What are IDEs?
Integrated Development Environments are programs that are designed to
simplify the development of large programs. Several features they might have
are:
• The ability to edit several files at once.
• Syntax highlighting - different parts of the code are shown in different
colours e.g. language keywords (public, class etc) in blue.
• The ability to compile the code using a button or menu item and to see
the messages in another window.
• To run the program using a button or menu item.
• To drag components from a 'toolkit' to a design window.
• The ability to automatically generate lines of code.
• To show a tree like view of your classes.
• Extensive debugging facilities.
PHY281
Introduction to Java
Slide 13
Java IDEs
Visual Basic is an IDE.
For C++ some popular IDEs are Borland C++ and Microsoft Visual C++.
For Java there are several including Metrowerks, CodeWarrior, Borland
JBuilder and Eclipse. They are really designed for large complex programs and
are somewhat over complicated for simple programs. They are also hard to
install on the Lancaster University servers!
Steve Lloyd developed a simple IDE for a similar course called PhysEdit which
has much less functionality than the others but is hopefully reasonably
straightforward and sufficient for our needs. You can download it for your
home computer if you wish (assuming you have Windows), although no liability
is taken!
PHY281
Introduction to Java
Slide 14
PhysEdit
PHY281
Introduction to Java
Slide 18
PhysEdit - Creating a Project
1. Click on New Project
(or File  New
Porject...)
6. View the applet
(executes
appletviewer for
you).
2. Enter the name of
the project e.g.
Greetings
5. Compile the code
(executes javac for
you).
3. The program creates
some skeleton code and
a html file.
4. Add your code to
the Java file.
PHY281
Introduction to Java
Slide 19