Download presentation
Presentation is loading. Please wait.
Published byErica Hill Modified over 9 years ago
1
9/21/99www.cs.vt.edu/wwtut/1 User Interface Programming In Java Part 1 – Introduction Marc Abrams Virginia Tech CS Dept courses.cs.vt.edu/wwwtut/
2
9/21/99www.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.
3
9/21/99www.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
4
9/21/99www.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
5
9/21/99www.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
6
9/21/99www.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.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,
7
9/21/99www.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.java.sun.com/products/jdk/1.2/docs/- relnotes/features.html
8
9/21/99www.cs.vt.edu/wwtut/8 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
9/21/99www.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.)here
10
9/21/99www.cs.vt.edu/wwtut/10 Solution (Ex. 6-1)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.
11
9/21/99www.cs.vt.edu/wwtut/11 Analysis of Code Invoked by HTML page: My First Applet This is the world's simplest applet.
12
9/21/99www.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); }
13
9/21/99www.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); }
14
9/21/99www.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); }
15
9/21/99www.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); }
16
9/21/99www.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); }
17
9/21/99www.cs.vt.edu/wwtut/17 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 } import java.applet.*; import java.awt.*; public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString( "Hello World", 25, 50); }
18
9/21/99www.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); }
19
9/21/99www.cs.vt.edu/wwtut/19 What does entire java.awt look like? Show diagram on transparency
20
9/21/99www.cs.vt.edu/wwtut/20 Example 2… Variation on Hello Word
21
9/21/99www.cs.vt.edu/wwtut/21 Code 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); 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.
22
9/21/99www.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 );
23
9/21/99www.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 );
24
9/21/99www.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);
25
9/21/99www.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);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.