Java in 4 hours - Seton Hall University
Download
Report
Transcript Java in 4 hours - Seton Hall University
Java
Quick & Dirty
By Bert Wachsmuth
Overview
We will cover:
We will need:
What is Java
Using and Writing Java applets
Getting more information
Knowledge of HTML and web page creation
We will get:
Basic understanding of Java but we will
NOT become the world’s foremost Java
programmer
What is
Java
Java
is a programming language that:
Is exclusively object oriented
Has full GUI support
Has full network support
Is platform independent
Executes stand-alone or “on-demand” in
web browser as applets
Simple Example
Web page with
Java applet
HTML code for
above web page
Simple Example
Java code for
TickerApplet
Example
Summary
To create a web page with an Applet
you need:
1.
A Java-aware web browser (usually beyond
your control)
2.
3.
A web page including a special HTML tag
called <APPLET> tag
One or more Java class files (produced
separately from Java source code)
Using Java
Applets
To use Java applets, you:
Locate appropriate class file(s)
Learn how to configure applet
Embed the Applet into web page
<applet codebase=“base_url”
code=“AppletName.class”
width=“###”
height=“###”>
</applet>
// if different from current directory
// name of class file
// width of applet area
// height of applet area
// do not forget this
Simple Applets
TickerTape
codebase: http://pirate.shu.edu/~wachsmut/Ticker
code: TickerApplet.class
width: 300, height=30
3D Surface
codebase: http://pirate.shu.edu/~wachsmut/Surface
code: surface.class
width: 500, height=550
Missile Defense
codebase: http://www.csd.uu.se/~d95vil/missile/classes
code: MissileCommand.class
width: 320, height: 200
Configurable
Applets
Many applets can be configured using
“param” tags:
1.
2.
Inquire about name and meaning of
“param” tags for the applet
Add “param” tags to HTML as needed
<applet codebase=“base_url”
code=“AppletName.class”
width=“###”
height=“###”>
<param name=“Name” value=“Value”>
<param name=“Name2” value=“Value2”>
...
</applet>
Configurable
Applet Examples
TickerTape
codebase: http://pirate.shu.edu/~wachsmut/Ticker
code: TickerApplet.class
width: 300, height=30
param: msg, delay, foreground, background
Chatterbox
codebase: http://sciris.shu.edu/Java/Chat
code: ChatterBoxlet.class
width: 200, height=50
param: host (use sciris.shu.edu), port (use 1234)
Note: Applets can be embedded easily with
FrontPage or Notes (LearningSpace)
Writing
Java Applets
To create Java Applets, you need:
Knowledge and a good Java book
Good ideas, patience, and persistence
and
A text editor & Java compiler
or
An “Integrated Developing Environment” (IDE)
Tools for Writing
Java Applets
Java API -> essential resource
http://mathcs.shu.edu/Resources/Java/API/
Editor ->Programmer’s File Editor
http://www.lancs.ac.uk/people/cpaap/pfe/
Compiler -> SUN’s Java compiler
http://www.javasoft.com/
IDE -> BlueJ version 1.1
http://bluej.monash.edu/
IDE -> Microsoft Visual J++ 6.0
Available from SHU but not recommended
Writing Applets - Intro
Java Applets are saved as “Name.java”
They must be compiled into “Name.class”
They require HTML page to run
They have “fields” and “methods”
fields are used to store data
methods are used to perform action
They inherit *lots* of fields and methods from
“parent” classes
Writing Applets - Framework
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Name extends Applet implement ActionListener
{ private type aField = new type(“foo”);
public void init()
{ /* code here when applet is initialized */ }
public void start()
{ /* code here to execute when page is visited */ }
public void stop()
{ /* code here to execute when page is left */ }
public void paint(Graphics g)
{ /* drawing code goes here */}
public void actionPerformed(ActionEvent ae)
{ /* code to react to action events here */ }
}
Writing Applets – Example 1
Steps
1.
2.
3.
4.
5.
6.
7.
Open editor
Type this source code
Save as “Junk.java”
Compile into “Junk.class”
Create HTML document
Add proper <applet> tag
View in web browser
Writing Applets – Example 2
Steps
1.
2.
3.
4.
5.
6.
7.
8.
Need two pictures named
“picture1.jpg” and picture2.jpg”
Open editor
Type this source code
Save as “Slide.java”
Compile into “SSlide.class”
Create HTML document
Add proper <applet> tag
View in web browser
Enhancing Applet 2
Steps
1.
2.
3.
4.
Questions:
•
•
•
What parameter tag(s) to use?
What must image names be?
How many images can be loaded?
Modify previous source
code
Save and compile
Add proper <param>
tag(s) to previous HTML
document
View new applet in web
browser (quit and restart)
Self-running
Applet
Steps
1.
2.
3.
Modify previous source
code
Save and compile
View new applet in web
browser (quit and restart)
Questions
•
•
•
•
What’s happening?
What’s the delay?
Pick a new <param> tag!
Does paint method change?
A Professional
Applet
To
create a professional Applet:
small, fast loading, preloading resources
completely fool-proof
completely configurable
utilizes reusable components (objects)
cool and new
Too
much for this course
What’s the Object of ObjectOriented Programming?
OOP revolves around 6 principles:
Classes: Fundamental structure of every Java
program (fields and methods)
Objects: Are constructed from class blueprints
Encapsulation: Regulate access to class
members
Overloading: Multiple definitions for methods
Inheritance: Relationships between classes
Polymorphism: To deal with related classes
based on common features
OOP Example
Our Task:
Create a unit conversion applet that converts
between feet and meter
The program should execute in a separate window
The program should have one menu item to exit
Steps:
Think about the objects to be used
Download the files from the web site
Compile, execute, test, and IMPROVE
Some “less-good” News
Java has “version problems” with applets:
1.0: Netscape 3, IE 3
1.1: Netscape < 4.5, IE 4
1.2: Netscape > 4.5, IE 4
1.3: Netscape 6 or “Java plugin”
Platform independent questionable (<1.3)
C++ “native” code is faster
No access to system-level programming
Large Programs need long download times
Applets have Security Restrictions (no saving…)
More Information
Sun’s Website & Online Tutorials
JARS Resources
CSAS1111, CSAS1112, etc
Java by Definition
http://www.jars.com/
Seton Hall Classes
http://www.javasoft.com/
http://pirate.shu.edu/~wachsmut/jbd/
Books
See Barnes & Nobles or Amazon.com
The End …
What is
JavaScript
JavaScript is a programming language that:
Is object-oriented but “loosely typed”
Has no GUI support but supports most web page
elements
Is somewhat standardized and runs only inside a
JavaScript-aware web browser
Code is embedded directly in a web page
Is a lot easier than Java
JavaScript Example:
<script language=“JavaScript”>
document.writeln(“Today is ” + (new Date()));
</script>