9/21/99 user interface programming in java part 1 – introduction marc abrams virginia tech cs dept...
TRANSCRIPT
9/21/99 www.cs.vt.edu/wwtut/ 1
User Interface Programming In Java
Part 1 – IntroductionMarc Abrams
Virginia Tech CS Deptcourses.cs.vt.edu/wwwtut/
9/21/99 www.cs.vt.edu/wwtut/ 2
History Evolved from Sun project to write code
for consumer electronics Gosling and colleagues began
adding/subtracting C++ features In 1993, developers realized Java’s
potential for Web. Wrote HotJava browser, with HTML support
for applets.
9/21/99 www.cs.vt.edu/wwtut/ 3
Java 1 Features (1) Simplicity
Garbage collection, single inheritance, less C/C++.But… you must learn a boatload of classes!
Object-oriented Distributed
network communication classes Architecture-neutral Interpreted
portable
9/21/99 www.cs.vt.edu/wwtut/ 4
Java 1 Features (2) Robust (?)
eliminates common bugs: pointers, arrays Secure
more so with Java 2 Multithreaded No type system violations Dynamic
dynamically load classes over network
9/21/99 www.cs.vt.edu/wwtut/ 5
What’s New in Java 2 Enhanced Security
Fine-grained security manager(e.g., grant write access only to a particular file)
Digitally signed apps and applets Swing UI components Java 2D API Accessibility API Drag & drop to/from non-Java apps, between
Java apps, w/in one Java app Custom cursors
9/21/99 www.cs.vt.edu/wwtut/ 6
What’s New in Java 2 Navigation with keyboard only (w/o mouse) “Collections” – container classes that
generalize Vector, Hashtable, Array. Like C++’s Standard Template Library.
Input Methods for Japanese, Chinese, or Korean text
Package-level version numbers Enhancements to serialization, RMI,
JavaBeans, garbage collection, Java Native Interface, JDBC,
9/21/99 www.cs.vt.edu/wwtut/ 7
What’s New in Java 2 Java Sound API Engine to play back
WAV, AIFF, … Java IDL allows CORBA objects to be
written in Java Performance improvements
See java.sun.com/products/jdk/1.2/docs/-relnotes/features.html for more info.
9/21/99 www.cs.vt.edu/wwtut/ 8
Where examples come from…
Java in a Nutshell, 2nd Editionwww.ora.com/catalog/javanut2/
Examples: www.ora.com/catalog/javanut2/examples/
However, this book only covers Java 1.1.
9/21/99 www.cs.vt.edu/wwtut/ 9
Try this now… Construct an HTML page. Page contains a Java applet. Java applet prints “hello world”.
(Click here to run example.)
9/21/99 www.cs.vt.edu/wwtut/ 10
Solution (Ex. 6-1)
import java.applet.*; // Don't forget this import statement!
import java.awt.*; // Or this one for the graphics!
public class FirstApplet extends Applet {
// This method displays the applet.
// The Graphics class is how you do all drawing in Java.
public void paint(Graphics g) {
g.drawString("Hello World", 25, 50);
}
}
// This example is from the book "Java in a Nutshell, Second Edition".
// Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
// You may distribute this source code for non-commercial purposes only.
// You may study, modify, and use this example for any purpose, as long as
// this notice is retained. Note that this example is provided "as is",
// WITHOUT WARRANTY of any kind either expressed or implied.
9/21/99 www.cs.vt.edu/wwtut/ 11
Analysis of Code Invoked by HTML page:
<HTML><HEAD><TITLE>My First Applet</TITLE></HEAD><BODY>This is the world's simplest applet.<P><APPLET code="FirstApplet.class" width=150 height=100></APPLET></BODY></HTML>
9/21/99 www.cs.vt.edu/wwtut/ 12
Analysis of Code No
main() function Function declarations
Only class declarations Inherits from pre-defined
class in package Package names are
(theoretically) globally unique
Default: names visible only within package
import java.applet.*;
import java.awt.*;
public class FirstApplet extends Applet {
public void paint(Graphics g) {
g.drawString(
"Hello World", 25, 50);
}
}
9/21/99 www.cs.vt.edu/wwtut/ 13
Analysis of Code To compile:
javac FirstApplet.java Compile creates
bytecode:FirstApplet.class
import java.applet.*;
import java.awt.*;
public class FirstApplet extends Applet {
public void paint(Graphics g) {
g.drawString(
"Hello World", 25, 50);
}
}
9/21/99 www.cs.vt.edu/wwtut/ 14
What is java.applet?Object Java.lang Component Java.awt
Container Panel
Applet Java.applet
import java.applet.*;
import java.awt.*;
public class FirstApplet extends Applet {
public void paint(Graphics g) {
g.drawString(
"Hello World", 25, 50);
}
}
9/21/99 www.cs.vt.edu/wwtut/ 15
What is java.applet?public class Applet extends Panel
{
…
public String getAppletInfo();
public String getParameter(
String name);
public void init(); //empty
public void start(); //empty
public void stop(); //empty
}
import java.applet.*;
import java.awt.*;
public class FirstApplet extends Applet {
public void paint(Graphics g) {
g.drawString(
"Hello World", 25, 50);
}
}
9/21/99 www.cs.vt.edu/wwtut/ 16
Where is paint() defined?(It’s not in applet!)
Object Java.lang Component Java.awt
Container Panel
Applet Java.applet
import java.applet.*;
import java.awt.*;
public class FirstApplet extends Applet {
public void paint(Graphics g) {
g.drawString(
"Hello World", 25, 50);
}
}
9/21/99 www.cs.vt.edu/wwtut/ 17
paint() is defined injava.awt.Component
Object Java.lang Component Java.awt
Container Panel
Applet Java.applet
public abstract class Component extends Object
{ … public void paint(Graphics g);
//empty}
import java.applet.*;
import java.awt.*;
public class FirstApplet extends Applet {
public void paint(Graphics g) {
g.drawString(
"Hello World", 25, 50);
}
}
9/21/99 www.cs.vt.edu/wwtut/ 18
What is java.awt.graphics?Object Java.lang Graphics Java.awt
public abstract class Graphics extends Object{ … public abstract void drawLine(int x1, int y1, int x2,int y2); public abstract void drawOval(int x, int y, int w, int h); public void drawString(String, int x, int y); public void fillOval(int x, int y, int w, int h); public void setColor(Color c); public void setFont(Font c);}
public class FirstApplet extends Applet {
public void paint(Graphics g) {
g.drawString(
"Hello World", 25, 50);
} }
9/21/99 www.cs.vt.edu/wwtut/ 19
What does entire java.awt look like?
Show diagram on transparency
9/21/99 www.cs.vt.edu/wwtut/ 20
Example 2… Variation on Hello Word
9/21/99 www.cs.vt.edu/wwtut/ 21
Codeimport java.applet.*;
import java.awt.*;
public class SecondApplet extends
Applet {
static final String message =
"Hello World";
private Font font;
public void init() {
font = new Font("Helvetica",
Font.BOLD, 48);
}
public void paint(Graphics g) { // The pink oval g.setColor(Color.pink); g.fillOval(10, 10, 330, 100);
// Red outline. Simulate // 4-pixel wide line
g.setColor(Color.red); g.drawOval(10,10, 330, 100); g.drawOval(9, 9, 332, 102); g.drawOval(8, 8, 334, 104); g.drawOval(7, 7, 336, 106);
// The text g.setColor(Color.black); g.setFont(font); g.drawString(message, 40, 75); }}
// This example is from the book "Java in a Nutshell, Second Edition".
// Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
// You may distribute this source code for non-commercial purposes only.
// You may study, modify, and use this example for any purpose, as long as
// this notice is retained. Note that this example is provided "as is",
// WITHOUT WARRANTY of any kind either expressed or implied.
9/21/99 www.cs.vt.edu/wwtut/ 22
Analysis of Code String
is a class, not char array each char is 16-bit
unicode
Font, Color are classes
import java.applet.*;
import java.awt.*;
public class SecondApplet extends
Applet {
static final String message =
"Hello World";
private Font font;
public void init() {
font = new Font("Helvetica",
Font.BOLD, 48);
}public void paint(Graphics g) { // The pink oval g.setColor( Color.pink );
9/21/99 www.cs.vt.edu/wwtut/ 23
What is java.awt.Color?Object Java.lang Color Java.awt
public final class Color extends Object {
public Color(int r, int g, int b);
public Color(int rgb);
public Color(float r, float g, float b);
..
public final static Color pink;
} g.setColor( Color.pink );
9/21/99 www.cs.vt.edu/wwtut/ 24
What is java.awt.Font?Object Java.lang Font Java.awt
public class Font extends Object {
public Font(String name, int style, int size);
public final static int BOLD; // or ITALIC, PLAIN
...
}
font = new Font("Helvetica", Font.BOLD, 48);
9/21/99 www.cs.vt.edu/wwtut/ 25
Recall java.awt.graphics…Object Java.lang Graphics Java.awt
public abstract class Graphics extends Object{ … public abstract void drawLine(int x1, int y1, int x2,int y2); public abstract void drawOval(int x, int y, int w, int h); public void drawString(String, int x, int y); public void fillOval(int x, int y, int w, int h); public void setColor(Color c); public void setFont(Font c);}
g.setColor(Color.pink);g.fillOval(10, 10, 330, 100);...g.setColor(Color.red);g.drawOval(10,10, 330, 100);...g.setFont(font);