Download Programming in Java Java Applets

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

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

Document related concepts
no text concepts found
Transcript
China Jiliang University
Java
Programming in Java
Java Applets
Java Web Applications,  Helmut Dispert
China Jiliang University
Java Applets
applet = app =
application snippet =
(German: Anwendungsschnipsel)
An applet is a small program that is intended
not to be run on its own, but rather to be
embedded inside another application.
Ref.: Sun Microsystems
Java Web Applications,  Helmut Dispert
China Jiliang University
CGI - Common Gateway Interface
Internet
CGI
HTML
Applications
Application Program
Browser
Operating System
Client
Java Web Applications,  Helmut Dispert
Application
WebServer
China Jiliang University
Java Applications and Applets
Internet
Java
Applications
Java
Applet
HTML
Application
Server
CGI
Application Program
Browser
Operating System
Client
Java Web Applications,  Helmut Dispert
Application
WebServer
China Jiliang University
Java Applications and Applets
Applet:
Ø Java application that runs within a WWW-browser.
Java AWT: Abstract Windowing Toolkit
Advantages:
Ø Defined surface exists (window, graphic environment,
event handling)
Disadvantages (security):
Ø no file access; no communication with other
computers; programs cannot be executed; native code
cannot be loaded.
Java Web Applications,  Helmut Dispert
China Jiliang University
AWT vs. Swing APIs
Java AWT (Abstract Windowing Toolkit)
java.awt
Ø Package:
Ø GUI functionality (graphical user interface)
Ø Component libraries:
labels, buttons, textfields, etc. (platform dependent)
Ø Helper classes:
event handling, layout managers (window layouts), etc.
The Swing APIs
javax.swing
Ø Package:
Ø Greater platform independence
- portable "look and feel" (buttons, etc.)
AWT and Swing are part of the JFC (Java Foundation Classes)
Easy upgrading using "J":
Java Web Applications,  Helmut Dispert
Applet
Button
→
→
JApplet
JButton
China Jiliang University
Viewing Applets
Viewing Applets:
a. using a (Java enabled) Browser:
load HTML-file (*.html) that will call the applet-file (*.class)
from local directory.
b. Using the appletviewer (part of the java development kit):
appletviewer name.html
batch file:
appletviewer %1.html
Java Web Applications,  Helmut Dispert
China Jiliang University
Classes, Packages
Classes for Applet Programming
Ø
java.awt.Graphics
Ø
java.awt.Image
Ø
java.awt.Font
Ø
java.awt.Color
Ø
java.awt.Event
Ø
java.net.URL
Importing Packages / Subpackages
import
import
import
import
java.awt.Color;
java.awt.*;
java.awt.Graphics;
java.awt.Font;
Java Web Applications,  Helmut Dispert
China Jiliang University
Classes, Packages
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
Component
Container
Panel
Applet
Java Web Applications,  Helmut Dispert
China Jiliang University
Class JApplet
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
javax.swing.JApplet
Component
Container
Class JApplet:
An extended version of java.applet.Applet
that adds support for the JFC/Swing
component architecture.
Panel
Applet
JApplet
Java Web Applications,  Helmut Dispert
China Jiliang University
Classes, Packages
Syntax:
public class Name extends java.applet.Applet
{
...
}
class
imported package
Java Web Applications,  Helmut Dispert
China Jiliang University
Methods
Java Applets
Methods that are automatically called
(implicit call)
void init()
Initialization:
Set up colors, fonts, etc. when first loaded
void start()
start the applet when loading HTML page
(e.g. start animation)
void paint(Graphics g) paint screen: display text, graphics
void repaint()
repaint screen: call paint() for update
void stop()
stop the applet
void destroy()
cleanup (before exiting)
Java Web Applications,  Helmut Dispert
China Jiliang University
Hello World Applet
Example Source Program "Hello World"
import java.awt.Graphics;
public class HelloWorldApp extends java.applet.Applet
{
public void paint (Graphics g)
{
g.drawString("Hello World!", 40, 20);
}
}
stored in:
HelloWorldApp.java
compiled with:
javac HelloWorldApp.java
Java Web Applications,  Helmut Dispert
China Jiliang University
AWT
The java.awt.Graphics class
Coordinate System:
origin
x
y
Unit: pixel
Java Web Applications,  Helmut Dispert
China Jiliang University
Hello World Applet
Example Source Program "Hello World"
<HTML>
<HEAD>
<TITLE>Hallo World Applet</TITLE>
</HEAD>
<BODY>
<APPLET CODE= "HelloWorldApp.class"
WIDTH = "210" HEIGHT = "50">
</APPLET>
</BODY>
</HTML>
Java Web Applications,  Helmut Dispert
China Jiliang University
Hello World Applet
Object Tag:
HTML 4 introduces the OBJECT element, which offers an all-purpose
solution to generic object inclusion.
The new OBJECT element thus subsumes some of the tasks carried out by
existing elements:
Type of inclusion
Specific element
Generic element
Image
IMG
OBJECT
Applet
APPLET (deprecated)
OBJECT
Another HTML document
IFRAME
OBJECT
The chart indicates that each type of inclusion has a specific and a general
solution. The generic OBJECT element will serve as the solution for
implementing future media types.
<object codetype="application/java" classid="java:Applet.class"
width="200" height="250">
</object>
http://www.w3.org/TR/REC-html40/struct/objects.html
Java Web Applications,  Helmut Dispert
China Jiliang University
Hello World Applet Example
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
public class HelloWorldApp2 extends java.applet.Applet
{
String str = "Hello World 2";
int w = 300;
int h = 80;
Font f = new Font ("Arial", Font.BOLD + Font.ITALIC, 48);
public void init()
{
resize(w,h);
}
public void paint (Graphics g)
{
g.setFont(f);
g.drawRect(0,0,w-1,h-1);
g.setColor(Color.red);
g.drawString(str, 10, 50);
}
}
continued
Java Web Applications,  Helmut Dispert
China Jiliang University
Hello World Applet Example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hallo World Applet</title>
</head>
<body>
<applet code= "HelloWorldApp2.class"
width="350" height = "100">
</applet>
</body>
</html>
Object
Java Web Applications,  Helmut Dispert
China Jiliang University
Hello World Applet Example using Swing
import javax.swing.*;
import java.awt.*;
public class JHWApplet extends JApplet
{
String msg;
public void init()
{
msg = "Hello J-World";
}
public void paint(Graphics g)
{
g.drawString(msg, 20, 30);
}
}
Java Web Applications,  Helmut Dispert
China Jiliang University
Methods
Further Methods
boolean
isActive()
Determines if this applet is active.
String
getAppletInfo()
Returns information about this applet.
void
showStatus(String msg)
Requests that the argument string be
displayed in the "status window (bar)".
public String
Returns the value of the named parameter
getParameter(String name) in the HTML tag.
URL
getCodeBase()
Gets the base URL.
Image
getImage(URL url)
Returns an Image object that can then be
painted on the screen.
AudioClip
getAudioClip(URL url)
Returns the AudioClip object specified by
the URL argument.
void
play(URL url)
Plays the audio clip at the specified
absolute URL.
Ref.: Sun Microsystems
Java Web Applications,  Helmut Dispert
China Jiliang University
Hello World Applet using Parameters
Transfering Parameters from HTML to Applet
<applet code
<param name
...
</applet>
= filename or URL>
= "name" value = "value">
Check for Null Reference:
...
if (name == null)
...
Java Web Applications,  Helmut Dispert
China Jiliang University
Applet using Parameters
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
public class HelloWorldApp3 extends java.applet.Applet
{
String text, fontSize;
Font f;
int w = 300;
int h = 80;
int theFontSize;
public void init()
{
resize(w,h);
this.text = getParameter("text");
if(this.text == null) this.text = "Hello World - Error!";
this.fontSize = getParameter("fontSize");
if (this.fontSize == null) this.theFontSize = 18;
else this.theFontSize = Integer.parseInt(fontSize);
}
continued
Java Web Applications,  Helmut Dispert
China Jiliang University
Applet using Parameters
public void paint (Graphics g)
{
Font f = new Font("Arial",
Font.BOLD + Font.ITALIC, this.theFontSize);
g.setFont(f);
g.drawRect(0,0,w-1,h-1);
g.setColor(Color.red);
g.drawString(text, 10, 50);
}
}
continued
Java Web Applications,  Helmut Dispert
China Jiliang University
Applet using Parameters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hallo World Applet</title>
</head>
<body>
<applet code= "HelloWorldApp3.class" width="600" height =
"100">
<param name="text" value="Hello World, Version 3" />
<param name="fontSize" value="48" />
</applet>
</body>
</html>
With parameters
Without parameters
Java Web Applications,  Helmut Dispert
continued
China Jiliang University
Applet using Parameters
with Parameter
without Parameter
Java Web Applications,  Helmut Dispert
China Jiliang University
AWT
Documentation
Method
Description
drawLine(int,int,int,int)
x/y-start to x/y-end
drawRect(int,int,int,int)
x/y-upper left to lower right
fillRect(int,int,int,int)
filled rectangle
drawRoundRect(6*int)
last 2 int for rounding
draw3DRect(4*int, boolean)
3D Rect. with shadow (y/n)
drawPolygon(int[],int[],int)
x,y-coordinates, number
drawOval(int,int,int,int)
Oval (including circle)
drawArc(6*int)
Arc
fillArc(6*int)
filled Arc
clearRect(int,int,int,int)
rect. in background color
copyArea(6*int)
x/y-translation
Java Web Applications,  Helmut Dispert
China Jiliang University
AWT - Example: Curve Plotting
import java.awt.*;
public class CurveApp extends java.applet.Applet
{
int f(double x)
{
return (int) ((Math.cos(x/5) + Math.sin(x/7) + 2) * 50);
}
public void paint (Graphics g)
{
for (int x = 0; x < 400; x++)
g.drawLine(x,f(x),x+1,f(x+1));
}
}
continued
Java Web Applications,  Helmut Dispert
China Jiliang University
AWT - Example: Curve Plotting
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hallo World Applet</title>
</head>
<body>
<applet code= "CurveApp.class"
width="600" height = "200">
</applet>
</body>
</html>
Java Web Applications,  Helmut Dispert
China Jiliang University
Color
Class:
java.awt.Color
Standard colors are class variables
RGB-Color: RED, GREEN, BLUE (16.7 million colors)
Create New Colors:
(Integer)
Color name = new Color (int red, int green, int blue)
(Float)
Color name = new Color (float red, float green, float blue)
Java Web Applications,  Helmut Dispert
China Jiliang University
Color
The class Color provides a series of static Color-objects that
can be used directly:
public
public
public
public
public
public
public
static
static
static
static
static
static
static
Color
Color
Color
Color
Color
Color
Color
white
lightGray
gray
darkGray
black
red
blue
public
public
public
public
public
public
static
static
static
static
static
static
Color
Color
Color
Color
Color
Color
green
yellow
magenta
cyan
orange
pink
Methods to determine the RGB-values of a color-object:
public int getRed()
public int getGreen()
public int getBlue()
Java Web Applications,  Helmut Dispert
China Jiliang University
Color Methods
Methods
Description
setcolor(Color)
Set the current color
setBackground (Color)
Set the background color
setForeground (Color)
Set the foreground color
setColor (new Color(1.0f, 0.0f, 0.0f));
Color c = new Color (0,255,0);
setBackground (c);
// RGB green
setBackground(Color.green); // Standard color green
Java Web Applications,  Helmut Dispert
China Jiliang University
Graphics g (lamp demo)
import java.awt.*;
public class Lamp extends java.applet.Applet
{
public void init()
{
resize(300,300);
}
public void paint(Graphics g)
{
// the moon
g.drawArc (20,20,100,100,90,180);
g.drawArc (40,20,60,100,90,180);
// the table
g.setColor (Color.orange);
g.fillOval (0,220,300,100);
g.setColor (Color.black);
Java Web Applications,  Helmut Dispert
continued
China Jiliang University
Graphics g (lamp demo)
// lamp cable
g.drawRect (148,0,5,89);
// upper arc of lamp
g.drawArc (85,87,130,50,62,58);
// two lines of the lamp
g.drawLine (215,177,181,90);
g.drawLine (85,177,119,90);
// the lower oval
g.setColor (Color.yellow);
g.fillOval (85,157,130,50);
// lamp pattern
g.setColor (Color.red);
g.fillOval (120,100,20,20);
g.copyArea (120,100,20,20,40,-7);
g.copyArea (120,100,20,20,30,30);
g.copyArea (120,100,20,20,-15,35);
g.copyArea (120,100,20,20,60,40);
}
}
Java Web Applications,  Helmut Dispert
China Jiliang University
System Colors
public final class SystemColor:
A class to encapsulate symbolic colors representing the color of native
GUI objects on a system.
For systems which support the dynamic update of the system colors
(when the user changes the colors) the actual RGB values of these
symbolic colors will also change dynamically.
In order to compare the "current" RGB value of a SystemColor object with
a non-symbolic Color object, getRGB should be used rather than equals.
Examples:
SystemColor.desktop
The color rendered for the background of the desktop.
SystemColor.window
The color rendered for the background of interior regions
inside windows.
SystemColor.control
The color rendered for the background of control panels
and control objects, such as pushbuttons.
SystemColor.controlText
The color rendered for the text of control panels and
control objects, such as pushbuttons.
SystemColor.scrollbar
The color rendered for the background of scrollbars.
Java Web Applications,  Helmut Dispert
China Jiliang University
AWT Structure
Types of classes and interfaces in package AWT
Ø Graphics
Ø Components (Windows, Menus)
Ø Layout Manager
Ø Event Handler
Ø Image Manipulation
Java Web Applications,  Helmut Dispert
China Jiliang University
AWT Structure
partial listings
java.awt.Component
Component
Component
Container
Container
Window
Window
Button
Button
Panel
Panel
Canvas
Canvas
Checkbox
Checkbox
Label
Label
ScrollPane
ScrollPane
java.awt
Graphics
Graphics
CLASS
CLASS
Color
Color
Font
Font
ABSTRACT
ABSTRACTCLASS
CLASS
extends
Java Web Applications,  Helmut Dispert
implements
Image
Image
FontMetrics
FontMetrics
INTERFACE
INTERFACE
China Jiliang University
Fonts
Class:
java.awt.Font
Font types:
Font Styles:
Size:
Courier;
TimesRoman;
Helvetica,
Arial, etc.
Font.PLAIN
Font.BOLD
Font.ITALIC
in pixel
Java Web Applications,  Helmut Dispert
// = 0
// = 1
// = 2
China Jiliang University
Fonts
Font styles are constants that can be added:
Example:
Font.BOLD + Font.ITALIC
// bold italic
Creating Fonts, Examples:
Font f = new Font("TimesRoman", Font.BOLD, 48)
Method f.*
Description
getName()
Return name of font as string
getSize()
Return current font size (int)
getStyle()
Return current styles (0-3)
isPlain()
Returns “true” if plain
isBold()
Returns “true” if bold
isItalic()
Returns “true” if italic
Java Web Applications,  Helmut Dispert
China Jiliang University
Fonts
Examples:
Font f = new Font("TimesRoman", Font.PLAIN, 72);
g.setFont(f);
g.drawString("This is size 72",10 ,100);
Method g.*
Description
drawString()
Draw a string
setColor()
Set color to be used
setFont()
Set font to be used
getFont()
Return current font object
Java Web Applications,  Helmut Dispert
China Jiliang University
FontMetrics
FontMetrics (abstract class)
Quality of text and font
Method fm.*
Description
stringWidth(str)
Return width of string str
charWidth(c)
Width of char c
getAscent()
Return the ascent of the font
getDescent()
Return the descent of the font
getLeading()
Returns the leading of the font
getHeight()
Returns the total height of font
2D Text Tutorial
Java Web Applications,  Helmut Dispert
China Jiliang University
FontMetrics
import java.awt.*;
public class Center extends java.applet.Applet
{
public void paint (Graphics g)
{
Font f = new Font ("TimesRoman", Font.PLAIN, 24);
FontMetrics fm;
g.setFont(f);
fm = getFontMetrics(f);
String str = "This text will be centered";
int x = (this.size().width - fm.stringWidth(str)) / 2;
int y = (this.size().height - fm.getHeight())
/ 2;
g.drawString(str,x,y);
}
}
Java Web Applications,  Helmut Dispert
China Jiliang University
FontMetrics Demo
import java.awt.*;
import java.applet.*;
public class DrawText extends Applet
{
public void paint (Graphics g)
{
// output strings
byte
byte_text[] = {'H','E','L','L','O'};
char
char_text[] = {'h','e','l','l','o'};
String String_text = "\u00c4g";
// fonts
Font Arial24 = new Font ("Arial", Font.PLAIN,24);
Font Tmsrm72 = new Font ("TimesRoman", Font.PLAIN,72);
// font metrics
FontMetrics fm;
int ascent, descent;
int string_width;
int char_width;
int leading;
Java Web Applications,  Helmut Dispert
continued
China Jiliang University
FontMetrics Demo
// simple output methods
g.drawBytes (byte_text,0,5,20,20);
g.drawChars (char_text,0,5,20,40);
g.drawString ("A complete string",20,60);
g.drawString (Arial24.toString(),20,80);
// selecting a font
g.setFont (Arial24);
g.drawString ("Now using Arial 24",20,120);
// translate origin
g.translate (100,250);
// select a large font
g.setFont(Tmsrm72);
continued
Java Web Applications,  Helmut Dispert
China Jiliang University
FontMetrics Demo
// font metrics
fm = getFontMetrics (Tmsrm72);
string_width
char_width
ascent
descent
leading
=
=
=
=
=
fm.stringWidth (String_text);
fm.charWidth ('\u00c4');
fm.getAscent ();
fm.getDescent ();
fm.getLeading ();
// draw vertical lines
g.drawLine (0, - ascent -10, 0, descent + 10);
g.drawLine (char_width, -ascent, char_width, descent + 10);
g.drawLine (string_width, - ascent -10, string_width, descent +10);
// draw horzontal lines
g.drawLine (-10, 0, string_width + 10, 0);
g.drawLine (-10, -ascent, string_width +10, -ascent);
g.drawLine (-10, descent, string_width + 10, descent);
g.drawLine (-10, descent + leading,
string_width + 10, descent + leading);
}
}
Java Web Applications,  Helmut Dispert
continued
China Jiliang University
FontMetrics Demo
Java Web Applications,  Helmut Dispert
China Jiliang University
Applet Demo
Karnaugh-Veitch Diagramm
Java Web Applications,  Helmut Dispert
China Jiliang University
Model-View-Controller
MVC:
Model-View-Controller-Architecture:
Ø Methodology / design pattern widely used in objectoriented programming.
Ø Relates the the user interface (UI) to the underlying data
models.
Ø Used in program development with Java, Smalltalk, C,
and C++.
Java Web Applications,  Helmut Dispert
China Jiliang University
Model-View-Controller
The MVC pattern proposes three main components or
objects to be used in software development:
Ø A Model:
represents the underlying, logical structure of data in a
software application and the high-level class associated
with it.
Does not contain information about the user interface.
Ø A View:
collection of classes representing the elements in the user
interface (visual display, possible user responses - buttons,
display boxes, etc.)
Ø A Controller:
represents the classes connecting the model and the view.
Java Web Applications,  Helmut Dispert
China Jiliang University
Model-View-Controller
View
Model
Controller
Java Web Applications,  Helmut Dispert
Related documents