Java I--Copyright © 2000-2007 Tom Hunter. Chapter 3 Introduction to Java Applets.

Slides:



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

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.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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.
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when 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.
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 Recitation 11. Applet Applets. An applet is a Java program that is started by a browser (e.g. netscape or internet explorer) when an html file has a.
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.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
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.
A Simple Applet.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 24 - Introduction to Java Applications and Applets Outline 24.1Introduction 24.2Basics of a Typical.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) 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.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
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.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
JAPPLET.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
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.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
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,
Review of CIS 120 Concepts: What you said you want….
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and.
 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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
Creating a Java Application and Applet
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 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
Chapter 3: Using Methods, Classes, and Objects
Programming Language Concepts (CIS 635)
Java I.
Chapter 24 - Introduction to Java Applications and Applets
Java Applets.
Chapter 3 - Introduction to Java Applets
Applet 2019/4/23.
Defining Classes and Methods
Presentation transcript:

Java I--Copyright © Tom Hunter

Chapter 3 Introduction to Java Applets

Java I--Copyright © Tom Hunter There is no main method in a Java Applet. A Java Applet can only run in a browser. An Applet is run only when an HTML page requests that it be executed. In place of a browser, we use a utility called the appletviewer This is a “minimal browser”—it ignores all other HTML commands except the one used to run an Applet. Applets Execute in a Browser

Java I--Copyright © Tom Hunter appletviewer syntax. To execute an Applet, do the following: Compile your Java Applet using javac, as usual. C:\ javac Hello.java Create an HTML file and name it: Example.html Applets Execute in a Browser To run the Applet, you type the following: C:\ appletviewer Example.html w

Java I--Copyright © Tom Hunter appletviewer syntax. To execute an Applet, do the following: Compile your Java Applet using javac, as usual. C:\ javac Hello.java Create an HTML file and name it: Example.html Applets Execute in a Browser To run the Applet, you type the following: C:\ appletviewer Example.html These parameters refer to the width and height (in pixels) of the box your Applet will get when it is executed on a web page. w

Java I--Copyright © Tom Hunter Writing A “String” (A Sentence) using an Applet Because an Applet gets help from a browser, it contains much less code. The most obvious omission is the main method. A Simple Applet

Java I--Copyright © Tom Hunter Every Java Applet must commence by importing the class JApplet. This class does all the heavy lifting for us. A Simple Applet // A First Applet import javax.swing.JApplet; import java.awt.Graphics;

Java I--Copyright © Tom Hunter A Simple Applet // A First Applet import javax.swing.JApplet; import java.awt.Graphics; We are also importing class Graphics from the awt (Abstract Windowing Tools) package so that we can draw the String on the Applet.

Java I--Copyright © Tom Hunter All code in an Applet sits in the wrapper of a class. Likewise, the class name starts with a Capital The public keyword enables the browser to create an instance of this class—what we have to do by ourselves in an Application. A Simple Applet // A First Applet import javax.swing.JApplet; import java.awt.Graphics; public class WelcomeApplet extends JApplet { }

Java I--Copyright © Tom Hunter Take special note of the “ extends ” keyword. extends means that our WelcomeApplet is building on top of another class, JApplet ! We’re taking everything it has, and adding to it ! A Simple Applet // A First Applet import javax.swing.JApplet; import java.awt.Graphics; public class WelcomeApplet extends JApplet { } This is Inheritance —a very special principle—and there are special terms to describe the relationship. Superclass (base class)JApplet Subclass (derived class)WelcomeApplet w

Java I--Copyright © Tom Hunter Now we have added a method called paint. Notice its Access Modifier is public, so this method can be called by any object outside of this class. The return value is void. A Simple Applet // A First Applet import javax.swing.JApplet; import java.awt.Graphics; public class WelcomeApplet extends JApplet { public void paint( Graphics g ) { }

Java I--Copyright © Tom Hunter Apparently, the paint method of JApplet expects to be passed an object of type Graphics. We are instantiating (creating) a Graphics class object called g right there, inside the parenthesis of paint. A Simple Applet // A First Applet import javax.swing.JApplet; import java.awt.Graphics; public class WelcomeApplet extends JApplet { public void paint( Graphics g ) { } Don’t get nervous. When you created an integer variable ( int x; char m; ) you were doing the same thing, “Creating a instance of a type.” “ Graphics g ” is creating an example or instance of Graphics class and naming that instance g. w

Java I--Copyright © Tom Hunter Finally, with this statement, we use the Graphics class object g, and call the method drawString that all Graphics class objects have. drawString expects a String, plus two coordinates that say where to place the bottom-left corner of the text. A Simple Applet // A First Applet import javax.swing.JApplet; import java.awt.Graphics; public class WelcomeApplet extends JApplet { public void paint( Graphics g ) { g.drawString( “Welcome to Java!”, 25, 25 ); }

Java I--Copyright © Tom Hunter Class JApplet already has a method called paint. Class JApplet’s paint method is empty. It does nothing. Although we did inherit the do-nothing method paint from JApplet, ours will do something. We are overriding the method paint that we inherited. A Simple Applet // A First Applet import javax.swing.JApplet; import java.awt.Graphics; public class WelcomeApplet extends JApplet { public void paint( Graphics g ) { g.drawString( “Welcome to Java!”, 25, 25 ); }

Java I--Copyright © Tom Hunter Overriding a method This process is central to inheritance. Although we received a perfectly good method from the Superclass, we decided to create our own version of it—with the exact same name. Since we used the same name, our version of the method takes over or overrides the method. We haven’t affected the original copy of the method—it still exists—we just improved upon it in our own special Subclass. Applets Execute in a Browser

Java I--Copyright © Tom Hunter You might have to resize the Applet. You set the dimensions of this box in your Example.html file. Note: When you resize the Applet, you trigger the paint method to fire. Applets Execute in a Browser

Java I--Copyright © Tom Hunter In an Application, the method main is guaranteed to be called by the operating system. In an Applet, which has no main method, three other methods are guaranteed to be called by the operating system: init() start() paint() ( In that order ) More About Applets

Java I--Copyright © Tom Hunter Since we did not override init() and start(), the default versions of these methods were executed. What did they do? Nothing! Only paint() did something because we overrode it and made it do something useful. Finally, anytime you resize an Applet (meaning drag the bottom right corner to make it bigger or smaller), then the method paint() is automatically called again. More About Applets init() start() paint()

Java I--Copyright © Tom Hunter Attributes and Behaviors: All Objects have them. An Object in the real world has: Attributes--its qualities, and its Behaviors--what it does. Thinking About Objects Object: A balloon Attributes: Color: red Diameter: 2 inches Behaviors: Rises Inflates Deflates Pops

Java I--Copyright © Tom Hunter Java is based on the unit of a class. A class is an object, it encapsulates the attributes and behaviors into one self-contained unit. The attributes (internal data variables) and behaviors (methods that have an effect on those internal data variables) are combined into a unit called an object. Java encapsulates data (attributes) and methods (behavior) into a unit called an Object. Thinking About Objects

Java I--Copyright © Tom Hunter Thinking About Objects Some objects are Similar—Java Takes Advantage of the Similarities A bicycle inner tube is a specific type of balloon. It has a color and a width. It inflates, deflates and pops. I cannot change the diameter of the balloon without using the method of inflating it. Alone, I can’t change the diameter attribute of the inner tube. I have to use the method of inflating or deflating the inner tube to change its diameter attribute. We say the width attribute is hidden. I can’t change it without using the method.

Java I--Copyright © Tom Hunter Thinking About Objects An Employee is an Object A Generic Employee has attributes: name address phoneNumber socialSecurityNumber A Generic Employee has a method: calculatePay()

Java I--Copyright © Tom Hunter Thinking About Objects An Employee is an Object An Hourly Employee is a specific kind of Employee. It has the same Attributes as the Generic Employee, plus other Attributes: hourlyPayRate overtimeHours An Hourly Employee has the same method: calculatePay() but the calculation is different. It uses hourlyPayRate and overtimeHours.

Java I--Copyright © Tom Hunter Thinking About Objects An Employee is an Object A Salaried Employee is specific kind of Employee. It has all the same Attributes as the Generic Employee, plus another Attribute: salary A Salaried Employee has the same method: calculatePay() but the calculation is different. It uses salary.

Java I--Copyright © Tom Hunter Thinking About Objects An Employee is an Object A Commission Employee is specific kind of Employee. It has all the same Attributes as the Generic Employee, plus other Attributes: basePay commissionRate A Commission Employee has the same method: calculatePay() but the calculation is different. It uses basePay and commissionRate.

Java I--Copyright © Tom Hunter Thinking About Objects An Employee is an Object A Piecework Employee is specific kind of Employee. It has all the same Attributes as the Generic Employee, plus another Attribute: payPerPiece A Piecework Employee has the same method: calculatePay() but the calculation is different. It uses payPerPiece.

Java I--Copyright © Tom Hunter Thinking About Objects Inheritance The Hourly Employee, Salaried Employee, Commission Employee and Piecework Employee all... Inherit the Attributes and Behaviors of the Generic Employee. They elaborate on the stuff they inherited. Any Superclass methods that are appropriate are NOT overridden. Although they inherit from Generic Employee, the base is not changed.

Java I--Copyright © Tom Hunter The Graphics class that drew the String on our previous Applet has many methods at our disposal. To the list of API classes we must know, we must now add Graphics and JApplet. Draw A Line

Java I--Copyright © Tom Hunter This will put lines above and below the sentence. The 4 arguments are the beginning and end points of the line. Draw A Line // Display Text and Lines import javax.swing.JApplet; import java.awt.Graphics; public class WelcomeLines extends JApplet { public void paint( Graphics g ) { g.drawLine( 15, 10, 210, 10 ); g.drawLine( 15, 30, 210, 30 ); g.drawString( “Welcome to Java!”, 25,25); }

Java I--Copyright © Tom Hunter This is the output. Draw A Line

Java I--Copyright © Tom Hunter This will produce the same result as we achieved with the Addition Application, only this time as an Applet. The goal is to add two floating-point numbers. Applet Example: Addition

Java I--Copyright © Tom Hunter // Add two floating point numbers import javax.swing.*; import java.awt.Graphics; public class AdditionApplet extends JApplet { double sum; // instance variable public void init() { } public void paint( Graphics g ) { } w Notice, this variable sum is outside of any method. sum is an instance variable. Numeric instance variables are automatically initialized to zero, char instance variables are automatically initialized to spaces and boolean are automatically initialized to false. This asterisk allows you to import all the classes in a package. ( But only the classes at this directory, not any sub-directories. ) Also, from that wildcard package, the compiler will only bring in those classes that you actually used in the program. Two Kinds of Java Variables: Instance variables: Declared outside of any method Automatically initialized Visible in all methods of the class Local variables: Declared inside a method Not automatically initialized—a syntax error if you try to use them with out first putting a value in. Vanish after the method returns to whatever called it.

Java I--Copyright © Tom Hunter // Add two floating point numbers import javax.swing.*; import java.awt.Graphics; public class AdditionApplet extends JApplet { double sum; // instance variable public void init() { String firstNumber, secondNumber; // local variables double num1, num2; // local variables } public void paint( Graphics g ) { }

Java I--Copyright © Tom Hunter // Add two floating point numbers import javax.swing.*; import java.awt.Graphics; public class AdditionApplet extends JApplet { double sum; // instance variable public void init() { String firstNumber, secondNumber; double num1, num2; firstNumber = JOptionPane.showInputDialog(“First Num” ); secondNumber = JOptionPane.showInputDialog(“Second Num” ); num1 = Double.parseDouble( firstNumber ); num2 = Double.parseDouble( secondNumber ); sum = num1 + num2; } public void paint( Graphics g ) { } Since num1 and num2 are doubles, we use the “Type Wrapper” class that can convert a String into a double. ( There are Type Wrapper classes for all the primitive data types. ) w

Java I--Copyright © Tom Hunter // Add two floating point numbers import javax.swing.*; import java.awt.Graphics; public class AdditionApplet extends JApplet { double sum; // instance variable public void init() { String firstNumber, secondNumber; double num1, num2; firstNumber = JOptionPane.showInputDialog(“First Num” ); secondNumber = JOptionPane.showInputDialog(“Second Num” ); num1 = Double.parseDouble( firstNumber ); num2 = Double.parseDouble( secondNumber ); sum = num1 + num2; } public void paint( Graphics g ) { g.drawRect( 15, 10, 270, 20 ); g.drawString( “The Sum is ” + sum, 25, 25 ); } In drawRect, the parameters are the coordinates for the top left-hand corner, the width and the height.

Java I--Copyright © Tom Hunter Applet Example: AdditionOutput

Java I--Copyright © Tom Hunter Another Word on Variables Earlier, we discussed the so-called primitive variables. The are so named, because they are not objects. For short, these are called just variables. int num1; double sum;

Java I--Copyright © Tom Hunter Another Word on Variables There is another group— reference variables. These are objects. String firstNumber; String secondNumber; The identifiers for these reference variables only contain references—that is, the address where this reference variable can be found in memory.

Java I--Copyright © Tom Hunter num int num1 = 132; [Integers always take 4 bytes.] A primitive can only store one piece of data {This is the actual place in memory where the String Object stores everything it needs to accomplish its task as a String Object. Inevitably, it would be a lot more than a simple primitive... firstNumber A Reference is a pointer ! String firstNumber = JOptionPane.showMessageDialog firstNumber is a reference (pointer) to the real Object. An object can store many kinds of data.