21
Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Embed Size (px)

Citation preview

Page 1: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Lecture 2

Internet Computing Using Java

Theophano Mitsa

UMASS-Dartmouth

Page 2: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Reading assignment

• Chapter 3 (classes and objects)

Page 3: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

HW (due next Wednesday)

• Write an applet that inputs two numbers and finds out whether the second number is a factor of the first. If it is, the applet prints “You found a factor”. If not, it prints, “No, you did not find a factor”.

Page 4: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Introduction to Java Applets

• You can run them either using appletviewer or your Browser.

• NOTE: In order to use the appletviewer command remember to run the script that sets the environment variables.

Page 5: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Web browsers and applets

• Web browsers, such as Microsoft IE or Netscape, are used to display HTML documents on the WWW. HTML stands for Hypertext Markup Language.

• An HTML document can contain an applet. In this case, the local browser launches the Java class loader to load the applet from the remote computer where it is stored. Each browser that supports Java has a built-in JVM, and therefore it has a built-in class loader, bytecodes verifier, and interpreter.

Page 6: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Web browsers and applets

• Web browsers, such as Microsoft IE or Netscape, are used to display HTML documents on the WWW. HTML stands for Hypertext Markup Language.

• An HTML document can contain an applet. In this case, the local browser launches the Java class loader to load the applet from the remote computer where it is stored. Each browser that supports Java has a built-in JVM, and therefore it has a built-in class loader, bytecodes verifier, and interpreter.

Page 7: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Applet execution

• Once the applet is loaded, it is executed by the browser’s interpreter.

• Applets can also execute from the command line, using the appletviewer command. Note: appletviewer also requires an HTML document as input:

appletviewer Mypage.html

where mypage contains an applet.

Page 8: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Our first applet

import java.awt.Graphics;

import javax.swing.JApplet;

public class FirstApplet extends JApplet {

// draw text on applet’s background

public void paint( Graphics g )

{

// call superclass version of method paint

super.paint( g );

// draw a String at x-coordinate 30 and y-coordinate 30

g.drawString( "Welcome to my class!", 30, 30 );

} // end method paint

} // end class FirstApplet

Page 9: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Compilation and execution

• Compile it the usual way:

javac FirstApplet.java

• To execute it though, you have to create an HTML page to load the applet into the applet container.

Page 10: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

The HTML page

<html>

<applet code = "FirstApplet.class" width = "300" height = "45">

</applet>

</html>

Page 11: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Inside the Java applet

• The first import statement is: import java.awt.Graphics;We need this one, because the applet uses class

Graphics (g is a Graphics object).It is through the use of a Graphics object that an

applet is able to draw lines, rectangles, strings,etc. This is a good example of how many useful predefined classes exist in the Java API.

Exercise: Find the Graphics class in the Java API and learn about its methods.

Page 12: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

• The second import statement is: import javax.swing.JApplet; You always need this class when you create an applet.• JApplet is another example of a very useful

predefined Java class. It encapsulates what an applet should be. All you have to do is inherit from it.

This is how you inherit in Java: public class FirstApplet extends JApplet {• So JApplet is the superclass and FirstApplet is the

subclass. We will talk more about inheritance in later chapters.

Page 13: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

A very important note about import statements

• The * character does NOT import subpackages.

Example: When you say import javax.swing.*; this means that you need to import multiple classes from the javax.swing package. However, the * does not import the subpackages of javax.swing. You have to import those with another statement if you need them.

NOTE: We will learn more about packages in later lectures. For now, our programs exist in the default package.

Page 14: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Applet lifecycle

• An applet container, such as appletviewer, or your browser calls methods in the applet that have to do with managing the lifecycle of the applet:

• Applet initialization (done only once)• Applet starting (done whenever the user visits the

page)• Applet stopping (done whenever the user exits the

page)• Applet destruction (done only once)• There are methods in JApplet for all these!!!

Page 15: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Applet lifecycle

• Applet initialization: Done with the method void init(). This method is called by the applet container to notify the applet that it has been loaded.

• The init() call is followed by a call to start() and a call to paint().

• NOTE: All these methods get called by the applet container, not by you!!!

• NOTE: Just because we haven’t overriden init() and start() in our FirstApplet, it doesn’t mean they don’t get called. Their original version (from JApplet) gets called by the applet container.

Page 16: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

The paint() method

• This is where we tell the applet container what we want drawn on the screen.

• The applet container calls paint() and it passes an argument to it, which is a Graphics object, that paint() needs in order to perform its job.

• super.paint(g) calls the version of paint() from the superclass JApplet. For now, just include it as the first line of the paint() method, in all your applets.

• g.drawString( "Welcome to my class!", 30, 30 ); drawString is a method of the Graphics class. The

first argument is the string to be drawn, the other two are the coordinates for the bottom left side of the string.

Page 17: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

An applet that adds numbersimport java.awt.Graphics; // import class Graphicsimport javax.swing.*; // import package javax.swing

public class AdditionApplet extends JApplet { double sum; // sum of values entered by user

// initialize applet public void init() { String firstNumber; // first string entered by user String secondNumber; // second string entered by user double number1; // first number to add double number2; // second number to add

// obtain first number from user firstNumber = JOptionPane.showInputDialog( "Enter first floating-point value" ); // obtain second number from user secondNumber = JOptionPane.showInputDialog( "Enter second floating-point value" ); // convert numbers from type String to type double number1 = Double.parseDouble( firstNumber ); number2 = Double.parseDouble( secondNumber );

Page 18: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

// add numbers sum = number1 + number2;

} // end method init

// draw results in a rectangle on applet’s background public void paint( Graphics g ) { // call superclass version of method paint super.paint( g );

// draw rectangle starting from (15, 10) that is 270 // pixels wide and 20 pixels tall g.drawRect( 15, 10, 270, 20 );

// draw results as a String at (25, 25) g.drawString( "The sum is " + sum, 25, 25 );

} // end method paint

}

Page 19: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Its HTML

<html>

<applet code = "AdditionApplet.class" width = "300" height = "50">

</applet>

</html>

Page 20: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Exercise

• Write an applet that computes the square of

a number.

Page 21: Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

References

• Your textbook

• K.A. Mughal and R.W.Rasmussen, A Programmer’s Guide to Java Certification,

Addison-Wesley.