Presentation is loading. Please wait.

Presentation is loading. Please wait.

User Interface Programming In Java

Similar presentations


Presentation on theme: "User Interface Programming In Java"— Presentation transcript:

1 User Interface Programming In Java
Part 1 – Introduction Created by Marc Abrams Modified by Mir Farooq Ali Virginia Tech CS Dept 9/21/99

2 Where examples come from…
Java in a Nutshell, 2nd Edition Examples: However, this book only covers Java 1.1.

3 Try this now… Construct an HTML page. Page contains a Java applet.
Java applet prints “hello world”. (Click here to run example.)

4 // 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.*; // 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); }

5 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>

6 Analysis of Code No Only class declarations
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); } Unique package names like com.sun.java.x.y.z

7 Analysis of Code To compile: Compile creates bytecode:
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); } Unique package names like com.sun.java.x.y.z

8 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); } Unique package names like com.sun.java.x.y.z

9 What is java.applet? You define! 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); } Unique package names like com.sun.java.x.y.z You define!

10 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); } Unique package names like com.sun.java.x.y.z

11 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); } Unique package names like com.sun.java.x.y.z

12 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); } } Unique package names like com.sun.java.x.y.z

13 Example 2… Variation on Hello Word

14 Code public void paint(Graphics g) { // The pink oval
// 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); // 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); } 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); }

15 Analysis of Code String 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 ); String is a class, not char array each char is 16-bit unicode Font, Color are classes “static” means it belongs to class, and not to instance

16 g.setColor( Color.pink );
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; } Unique package names like com.sun.java.x.y.z g.setColor( Color.pink );

17 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 ... } Unique package names like com.sun.java.x.y.z font = new Font("Helvetica", Font.BOLD, 48);

18 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); Unique package names like com.sun.java.x.y.z


Download ppt "User Interface Programming In Java"

Similar presentations


Ads by Google