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

Slides:



Advertisements
Similar presentations
1 Applets Programming Enabling Application Delivery Via the Web.
Advertisements

Made with love, by Zachary Langley Applets The Graphics Presentation.
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
1 L44 Introduction to Java Applets. 2 OBJECTIVES  To differentiate between applets and applications.  To observe some of Java's exciting capabilities.
 2002 Prentice Hall. All rights reserved. 1 Week 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
Chapter 2 - Introduction to Java Applications
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
A Simple Applet.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 24 - Introduction to Java Applications and Applets Outline 24.1Introduction 24.2Basics of a Typical.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software Development Kit The TicTacToe Applet.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
 Pearson Education, Inc. All rights reserved Introduction to Java Applets.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 3 Introduction to Java Applets 1 3 There is no main method in a Java Applet. A Java Applet can only run in a browser. An Applet is run.
Java I--Copyright © Tom Hunter. Chapter 3 Introduction to Java Applets.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance Java Applets –what is an applet vs. an application? –where do applets.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.3Sample.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Creating a Java Application and Applet
Please open JCreator and follow these steps: 1)Configure  Options 2)JDK Tools 3)Choose Run Applet from drop-down 4)Click Default, then Edit 5)Click the.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Lecture 09 Applets.
Introduction to Java Applets
Chapter 3 - Introduction to Java Applets
Java I.
Chapter 24 - Introduction to Java Applications and Applets
Java Applets.
Chapter 3 - Introduction to Java Applets
Applet 2019/4/23.
Handout-14 Applets and Graphics
Outline Character Strings Variables and Assignment
Presentation transcript:

Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth

Reading assignment Chapter 3 (classes and objects)

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

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.

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.

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.

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.

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

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.

The HTML page

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.

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.

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.

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

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.

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.

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 );

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

Its HTML

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

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