15
Java Lecture 16: Dolores Zage

Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Embed Size (px)

Citation preview

Page 1: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Java

Lecture 16:

Dolores Zage

Page 2: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

WWW

Was a method for distributing passive information

added forms and image maps interaction was only a new way to get at the

same information enter Java, and the capability for Web pages

of containing Java applets

Page 3: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Java applet is a dynamic and interactive program that can run

inside a Web page displayed by a Java-capable browser

Small programs that create animations multimedia presentations real-time video games multi-user networked games ream interactivity most anything a small program can do

Page 4: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

What is Java?

OO programming language developed by Sun Microsystems

modeled after C++ small, simple, portable across platforms and

operating systems complete full-fledge programming language HotJava (Sun’s browser) was completely

written in Java

Page 5: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Applets and Applications

Applets- Java programs that are downloaded over the WWW and executed by a Web browser on the reader’s machine

applications - more general programs, do not require a browser to run.

A single Java program can be both, depending on how you write that program and the capabilities that program uses.

Page 6: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Platform-Independent

Most significant advantages that Java has over other programming languages

at both the source and the binary level Java’s foundation class libraries make it easy

to write code that can be moved from platform to platform without the need to rewrite it to work with that platform

Page 7: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Bytecodes

Binary files are also platform-independent use a form called bytecodes bytecodes are a set of instructions that looks

like some machine codes, but that is not specific to any one processor

Page 8: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Traditional versus Java compilation

-------

-------

-------

-----

------

----

Traditional CodeCompiler(Pentium)

Compiler(Power PC)

Java Compiler(SPARC)

Java Compiler(Pentium)

JavaCompiler

(PowerPC)

Compiler(SPARC)

Binary File

(Pentium)

Binary File

(PowerPC)

Binary File

(SPARC)

Java Bytecode(Platform

Independent)

JavaInterpreter(Pentium)

JavaInterpreter(PowerPC)

JavaInterpreter(SPARC)

Java Code

-----

-----

--

-----

---

------

Page 9: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Bytecodes

Programs can run on any platform and any operating or window system as long as the Java interpreter is available

Disadvantage - execution speed.

Page 10: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Creating the Hello World Java Application

Class HelloWorld

{

Public static void main (String args[])

{

System.out.println(“Hello World!”);

}

}

All of the program is

enclosed in a class

definition!

Main is the first routine that is run when the program is executed

Page 11: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Creating and Executing “Hello World” Use your favorite editor filename Helloworld.java compile the source file using the Java

compiler (Suns -- javac Helloworld.java) if no errors - end up with a file called

Helloworld.class - this is the bytecode file then use the Java interpreter to execute java Helloworld

Page 12: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

To create an applet

Creating applets may be more complex than creating an application because of the special rules for how they behave in a browser

for example, Hello World as an applet need to make space for your message use graphics operations to paint the message to

the screen

Page 13: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Creating the Hello World Java Applet

import java.awt.Graphics;

Class HelloWorldApplet extends java.applet.Applet

{

Public void paint (Graphics g)

{

g.drawString(“Hello World!”, 5, 25);

}

}

Enables this applet to interact with the JDK classes for creating applets and drawing graphics on the screen

Applets use several standard methods to take the place of main, which include init(), start() and paint()

Page 14: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Creating and Executing “Hello World” Use your favorite editor filename HelloworldApplet.java compile the source file using the Java

compiler (Suns -- javac HelloworldApplet.java) if no errors - end up with a file called

HelloworldApplet.class - this is the bytecode file

now use inside of a web page

Page 15: Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to

Using the Hello World Java Applet

<HTML>

<HEAD><TITLE> Hello to Everyone! </TITLE>

</HEAD> <BODY>

<P>My Java applet says:

<APPLET CODE=“HelloworldApplet.class” WIDTH=150 HEIGHT=25>

</BODY>

</HTML>