Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
User Interface Programming In Java Part 1 – Introduction Marc Abrams Virginia Tech CS Dept courses.cs.vt.edu/wwwtut/ 9/21/99 www.cs.vt.edu/wwtut/ 1 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. 9/21/99 Wrote HotJava browser, with HTML support for applets. www.cs.vt.edu/wwtut/ 2 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 9/21/99 portable www.cs.vt.edu/wwtut/ 3 Java 1 Features (2) Robust (?) Secure eliminates common bugs: pointers, arrays more so with Java 2 Multithreaded No type system violations Dynamic 9/21/99 dynamically load classes over network www.cs.vt.edu/wwtut/ 4 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/ 5 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/ 6 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/ 7 Where examples come from… Java in a Nutshell, 2nd Edition www.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/ 8 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/ 9 // // // // // // 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. Solution (Ex. 6-1) import java.applet.*; import java.awt.*; // Don't forget this import statement! // 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); } } 9/21/99 www.cs.vt.edu/wwtut/ 10 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/ 11 Analysis of Code No main() function Function declarations import java.applet.*; import java.awt.*; Only class declarations Inherits from pre-defined class in package Package names are (theoretically) globally unique Default: names visible only within package 9/21/99 public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString( "Hello World", 25, 50); } } www.cs.vt.edu/wwtut/ 12 Analysis of Code To compile: javac FirstApplet.java Compile creates bytecode: FirstApplet.class 9/21/99 import java.applet.*; import java.awt.*; public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString( "Hello World", 25, 50); } } www.cs.vt.edu/wwtut/ 13 What is java.applet? Object Java.lang Component Java.awt Container Panel Applet Java.applet 9/21/99 import java.applet.*; import java.awt.*; public class FirstApplet extends Applet { public void paint(Graphics g) g.drawString( "Hello World", 25, 50); } } www.cs.vt.edu/wwtut/ 14 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 } 9/21/99 import java.applet.*; import java.awt.*; public class FirstApplet extends Applet { public void paint(Graphics g) g.drawString( "Hello World", 25, 50); } } www.cs.vt.edu/wwtut/ 15 Where is paint() defined? (It’s not in applet!) Object Java.lang Component Java.awt Container Panel Applet Java.applet 9/21/99 import java.applet.*; import java.awt.*; public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString( "Hello World", 25, 50); } } www.cs.vt.edu/wwtut/ 16 paint() is defined in java.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 } 9/21/99 import java.applet.*; import java.awt.*; public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString( "Hello World", 25, 50); } } www.cs.vt.edu/wwtut/ 17 What is java.awt.graphics? Object Graphics Java.lang Java.awt public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString( "Hello World", 25, 50); } } 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); } 9/21/99 www.cs.vt.edu/wwtut/ 18 What does entire java.awt look like? Show diagram on transparency 9/21/99 www.cs.vt.edu/wwtut/ 19 Example 2… Variation on Hello Word 9/21/99 www.cs.vt.edu/wwtut/ 20 // // // // // // 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. Code public void paint(Graphics g) { // The pink oval g.setColor(Color.pink); g.fillOval(10, 10, 330, 100); import java.applet.*; import java.awt.*; // 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); public class SecondApplet extends Applet { static final String message = "Hello World"; private Font font; // The text g.setColor(Color.black); g.setFont(font); g.drawString(message, 40, 75); } public void init() { font = new Font("Helvetica", Font.BOLD, 48); } } 9/21/99 www.cs.vt.edu/wwtut/ 21 Analysis of Code String import java.applet.*; import java.awt.*; is a class, not char array each char is 16-bit unicode Font, Color are classes 9/21/99 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 ); www.cs.vt.edu/wwtut/ 22 What is java.awt.Color? Object Color Java.lang 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; } 9/21/99 g.setColor( Color.pink ); www.cs.vt.edu/wwtut/ 23 What is java.awt.Font? Object Font Java.lang 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/ 24 Recall java.awt.graphics… Object Graphics Java.lang Java.awt g.setColor(Color.pink); g.fillOval(10, 10, 330, 100); ... g.setColor(Color.red); g.drawOval(10,10, 330, 100); ... g.setFont(font); 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); } 9/21/99 www.cs.vt.edu/wwtut/ 25