Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth."— Presentation transcript:

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

2 Reading assignment Chapter 3 (classes and objects)

3 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”.

4 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.

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

6 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.

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

8 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

9 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.

10 The HTML page

11 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.

12 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.

13 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.

14 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!!!

15 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.

16 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.

17 An applet that adds numbers import java.awt.Graphics; // import class Graphics import 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 );

18 // 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 }

19 Its HTML

20 Exercise Write an applet that computes the square of a number.

21 References Your textbook K.A. Mughal and R.W.Rasmussen, A Programmer’s Guide to Java Certification, Addison-Wesley.


Download ppt "Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth."

Similar presentations


Ads by Google