290 likes | 297 Views
Java Tutorial Ethan Cerami @1998 New York University. Road Map. What is Java? Why is Java Important? Strengths, Weaknesses Java Tutorial: Creating an Applet Cool Examples/Demos Future of Java. I. What is Java?. Java is a new Internet programming language.
E N D
Java Tutorial Ethan Cerami @1998 New York University
Road Map • What is Java? • Why is Java Important? • Strengths, Weaknesses • Java Tutorial: Creating an Applet • Cool Examples/Demos • Future of Java
I. What is Java? • Java is a new Internet programming language. • Developed by Sun Microsystems, Inc. • Originally developed for Interactive Television Set-top boxes. • Rapidly becoming the standard for Internet programming.
Evolution of the Web • Phase 1: Everything was in text. Used mainly by academics and government. • Phase 2: Simple, graphical user interface. • Mosaic and Netscape. • Used by millions of people. • Phase 3: Interactive Applications and Web Pages. • Where we are today.
Applets • Applets: mini Java applications that can be embedded inside a web page. Big Text Smaller Text Clock Applet
Some Basic Examples • Scrolling Ticker • “Bouncing Heads” • Simple Video Games
II. Why is Java Important? • Enables the distribution of applications across the Internet. • As a language, Java also has many important strengths: • Object Oriented • Built-in Security • Built-in Networking • Platform Independent • Built-in Multithreading
Feature #1: Object Oriented • Object Oriented languages enable the creation of large applications that are easy to maintain. Scrolling Ticker Object Clock Object Weather Object
Feature #2: Built-In Security • If you download an application across the Internet, the program might do malicious things: deleting files, reading sensitive data, etc. . Operating System: Read, write, delete files Java Security Sandbox Restricted Access
Feature #3: Networking • It is extremely easy to create network applications in Java. • Java was built from the ground up to work with the Internet.
Feature #4: Portable • Java is Platform independent: Runs on any platform - Windows, Macintosh, UNIX. • “Write once, run anywhere.” • Create a single application and it can run anywhere on the Internet.
Feature #5: Multithreading • You can create applications that do more than one thing at a time. • For example, you can have an animation and a sound at the same time. • Important for animations and network applications.
Java Virtual Machine Java Applet C/C++ Program Java Virtual Machine Operating System Operating System
Java Performance • The Achilles Heal of Java • When Java was first released, it was about 20 times slower than native applications written in C/C++. • Now, it’s about half the speed of C/C++. But, it really depends on the Browser and the Platform.
III. Java Tutorial • Once you understand the basics of Java, creating an Applet is straightforward. • In order to create an applet, you first need to download a Java Compiler. • Download the Java Development Kit (JDK) 1.1 from java.sun.com
Object Oriented Basics • Every object has two things: • Some Data • Some Methods (or functions) • For example, a scrolling ticker object might have: • The Text to be displayed. • A method called animate() that tells the object to start scrolling.
The Applet Object • The Applet objects contains: • Data regarding the Web Browser. • Methods for creating applications. • Some methods: • getImage (): Retrieves an image • paint (): Paint on a canvas • getAudioClip(): Retrieves a sound file
Hello World! import java.applet.*; import java.awt.*; // This applet prints Hello public class FirstApplet extends Applet { // The method paints the applet public void paint (Graphics g) { g.drawString ("Hello, World!", 25, 50); } }
Import/Comments • Import statement tells the Compiler to load certain libraries: • similar to the #include statement in C. • import java.applet.*; • import java.awt.*; (Abrstract Windowing Toolkit. Useful for using widgets and graphics) • Comments are denoted with // • // This is a comment.
Extending Applet • public class FirstApplet extends Applet { • This tells the compiler that you want to create an Applet object. Called FirstApplet. • This really relates to object oriented inheritance (which we don’t have time to discuss.)
Drawing Graphics • paint method: • Responsible for all graphics painting inside the applet. • g.drawString (“Hello World”, 25, 50); X Coordinate Y Coordinate
To View your Applet • First you need to compile your applet: • javac FirstApplet.java • Then, you need to create a simple HTML page: <APPLET code="FirstApplet.class" width=150 height=100></APPLET>
Hello World, Version 2.0 import java.applet.*; import java.awt.*; // This applet prints Hello, Version 2.0 public class SecondApplet extends Applet { // The method paints the applet public void paint (Graphics g) { g.setColor (Color.pink); g.fillOval (10,10,330,100); g.setColor (Color.black); g.drawOval (10,10,328,100); g.setFont (new Font("Helvetica", Font.BOLD, 48)); g.drawString ("Hello, World", 40, 75); } }
To View the Second Applet • HTML Page: <APPLET code="SecondApplet.class" width=350 height=125></APPLET>
IV. Cool Examples • The Molecule Viewer: • http://java.sun.com/nav/whatis/cherwell.html • Wired Stock Graphing Tool: • http://stocks.wired.com • 3D Ray: • http://www.2nu.com/Wayne/SplattJava/SplattJava480x300.html • Games: • http://www.comedycentral.com/southpark/cartman/cartman.htm
The Future of Java • To answer this question, you have to look at two elements: • What can you do with Java? What kind of added functionality will we see? • Where can you actually run Java? What comes after the Applet?
Java 1.2 • Current Release: Java 1.2 (Beta) Most browsers only support Version 1.0 • What’s new in Java 1.2: • Java Foundation Classes (Swing) • Java 2D, 3D • Java Sound • Java Speech
Java Everywhere • Java can run on any platform. • Embedded Java: • Java Rings • Java Smart Cards • Java Telephones • Java Televisions • Java light switches
One Java or Many? • Sun wants Java to be an open standard that can run on any platform. • Microsoft hates Java. Has created a slightly different version that is optimized for Windows.