Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

Similar documents
Chapter 3 - Introduction to Java Applets

CSC System Development with Java Introduction to Java Applets Budditha Hettige

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

9. APPLETS AND APPLICATIONS

Java Applet & its life Cycle. By Iqtidar Ali

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

Java Applet Basics. Life cycle of an applet:

Java TM Applets. Rex Jaeschke

PROGRAMMING LANGUAGE 2

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 9: Writing Java Applets. Introduction

Unit 1- Java Applets. Applet Programming. Local Applet and Remote Applet ** Applet and Application

Chapter 7 Applets. Answers

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Contents 8-1. Copyright (c) N. Afshartous

Part 1: Introduction. Course Contents. Goals. Books. Difference between conventional and objectoriented

CS335 Graphics and Multimedia

Summary. 962 Chapter 23 Applets and Java Web Start

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Graphics Applets. By Mr. Dave Clausen

SIMPLE APPLET PROGRAM

Chapter 12 Advanced GUIs and Graphics

Programming In Java Prof. Debasis Samanta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

G51PRG: Introduction to Programming Second semester Applets and graphics

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

Chapter 27. HTTP and WWW

Module 5 Applets About Applets Hierarchy of Applet Life Cycle of an Applet

Overview. Applets. A Java GUI inside your browser! Important methods Drawing images Playing audio Getting input parameters Double buffering


Outline. Introduction to Java. What Is Java? History. Java 2 Platform. Java 2 Platform Standard Edition. Introduction Java 2 Platform

Introduction to the Java T M Language

GETTING STARTED. The longest journey begins with a single step. In this chapter, you will learn about: Compiling and Running a Java Program Page 2

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Sun ONE Integrated Development Environment

UNIT -1 JAVA APPLETS

MULTIMEDIA PROGRAMMING IN JAVA. Prof.Asoc. Alda Kika Department of Informatics Faculty of Natural Sciences University of Tirana

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved.

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

CT 229 Arrays in Java

An applet is called from within an HTML script with the APPLET tag, such as: <applet code="test.class" width=200 height=300></applet>

Graphics Applets. By Mr. Dave Clausen

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

Advanced Internet Programming CSY3020

TWO-DIMENSIONAL FIGURES

Java - Applets. public class Buttons extends Applet implements ActionListener

Understanding How Java Programs Work

Introduction to Java Applets 12

Graphics -- To be discussed

CSC 551: Web Programming. Fall 2001

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

(Refer Slide Time: 02:01)

Prashanth Kumar K(Head-Dept of Computers)

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

GUI, Events and Applets from Applications, Part III

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

Contents. iii Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1998, Revision B

Core JAVA Training Syllabus FEE: RS. 8000/-

Lecture Static Methods and Variables. Static Methods

CS1004: Intro to CS in Java, Spring 2005

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

PESIT Bangalore South Campus

UNIT-2: CLASSES, INHERITANCE, EXCEPTIONS, APPLETS. To define a class, use the class keyword and the name of the class:

SNS COLLEGE OF ENGINEERING, Coimbatore

CISC 1600 Lecture 3.1 Introduction to Processing

15CS45 : OBJECT ORIENTED CONCEPTS

MODULE 8p - Applets - Trials B

Chapter 3: Inheritance and Polymorphism

John Cowell. Essential Java Fast. How to write object oriented software for the Internet. with 64 figures. Jp Springer

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

User interfaces and Swing

In order to teach you about computer programming, I am going to make several assumptions from the start:

Eclipse Tutorial. For Introduction to Java Programming By Y. Daniel Liang

Java Programming Lecture 6

(2½ hours) Total Marks: 75

Introduction to Computer Science I

Lecture Static Methods and Variables. Static Methods

SELF-STUDY. Glossary

CS 11 java track: lecture 3

Supplement H.1: JBuilder X Tutorial. For Introduction to Java Programming, 5E By Y. Daniel Liang

Supplement II.B(1): JBuilder X Tutorial. For Introduction to Java Programming By Y. Daniel Liang

IT101. Graphical User Interface

Chapter 14: Applets and More

Introduction p. 1 Java Features p. 2 Java Expansion p. 4 Getting, Setting Up, and Using Java p. 5 The Java Language p. 5 Java Swing Components p.

Chapter 1 GUI Applications

UCLA PIC 20A Java Programming

CSD Univ. of Crete Fall Java Applets

CS2 Advanced Programming in Java note 8

Inheritance and Interfaces

Introduction to Java

SCHEME OF COURSE WORK

Building Java Programs

Chapter 14: Applets and More

Applets as front-ends to server-side programming

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

Transcription:

Java Applets

Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets Introduce inheritance Introduce the applet environment html needed for applets Reading: The applet coverage in the book is different from the coverage for today. The best review for this material is this PowerPoint presentation.

Applet Viewer and browsers An applet is a program that suns inside an applet viewer. The applet viewer takes care of providing the environment and calling several of the applet s methods. Modern browsers come with applet capabilities. The JDK also comes with an applet viewer. The JDK applet viewer is really just a minimum browser. It only understands the applet tag.

3.3 Simple Java Applet: Drawing a String Now, create applets of our own Take a while before we can write applets like in the demos Cover many of same techniques Upcoming program Create an applet to display "Welcome to Java!!" Show applet and HTML file, then discuss them line by line

import java.awt.*; import java.applet.*; public class WelcomeApplet extends Applet { public void init() { } extends allows us to inherit the capabilities of class Applet. public void paint(graphics g) { g.drawstring("welcome to Java Programming!", 25, 25 ); Method paint is guaranteed to } be called in all applets. Its first } line must be defined as above. Modified by Evan Korth

3.3 Simple Java Applet: Drawing a String import java.awt.*; // import package with class Graphics import javax.applet.*; // import class Applet Import predefined classes grouped into packages When you create applets, import Applet class (in the package java.applet) import the Graphics class (package java.awt) to draw graphics Can draw lines, rectangles ovals, strings of characters import specifies directory structure Modified by Evan Korth

3.3 Simple Java Applet: Drawing a String Applets have at least one class declaration (like applications) Rarely create classes from scratch Use pieces of existing classes Inheritance - create new classes from old ones (this will be the next major topic) public class WelcomeApplet extends Applet { Begins class declaration for class WelcomeApplet Keyword class then class name extends followed by class name Indicates class to extend (Applet) Applet : superclass (base class) WelcomeApplet : subclass (derived class) WelcomeApplet now has methods and data of Applet Modified by Evan Korth

3.3 Simple Java Applet: Drawing a String public class WelcomeApplet extends Applet { Class Applet defined for us Someone else defined "what it means to be an applet" Applets require over 200 methods! extends Applet Inherit methods, do not have to declare them all Do not need to know every detail of class Applet Modified by Evan Korth

3.3 Simple Java Applet: Drawing a String public class WelcomeApplet extends Applet { Class WelcomeApplet is a blueprint appletviewer or browser creates an object of class WelcomeApplet Keyword public required File can only have one public class public class name must be file name

3.3 Simple Java Applet: Drawing a String public void paint( Graphics g ) Our class inherits method paint from Applet By default, paint has empty body Override (redefine) paint in our class Methods init, start and paint. Guaranteed to be called automatically Our applet gets "free" version of these by inheriting from Applet Free versions have empty body (do nothing) Every applet does not need all three methods Override the ones you need Applet container draws itself by calling method paint Modified by Evan Korth

3.3 Simple Java Applet: Drawing a String public void paint( Graphics g ) Method paint Draws graphics on screen void indicates paint returns nothing when finishes task Parenthesis define parameter list - where methods receive data to perform tasks Normally, data passed by programmer, as in JOptionPane.showMessageDialog paint gets parameters automatically Graphics object used by paint Mimic paint's first line

3.3 Simple Java Applet: Drawing a String g.drawstring( "Welcome to Java Programming!", 25, 25 ); Body of paint Method drawstring (of class Graphics) Called using Graphics object g and dot (.) Method name, then parenthesis with arguments First argument: String to draw Second: x coordinate (in pixels) location Third: y coordinate (in pixels) location Java coordinate system Measured in pixels (picture elements) Upper left is (0,0)

3.3 Simple Java Applet: Drawing a String Running the applet Compile As you would a regular file in JCreator or javac WelcomeApplet.java (from the command line) If no errors, bytecodes stored in WelcomeApplet.class Create an HTML file Loads the applet into appletviewer or a browser Ends in.htm or.html To execute an applet Create an HTML file indicating which applet the browser (or appletviewer) should load and execute Modified by Evan Korth

3.3 Simple Java Applet: Drawing a String <html> <applet code = "WelcomeApplet.class" width = "300" height = "45"> </applet> </html> Simple HTML file (WelcomeApplet.html) Usually in same directory as.class file Remember,.class file created after compilation HTML codes (tags) Usually come in pairs Begin with < and end with > Lines 1 and 4 - begin and end the HTML tags Line 2 - begins <applet> tag Specifies code to use for applet Specifies width and height of display area in pixels Line 3 - ends <applet> tag

3.3 Simple Java Applet: Drawing a String <html> <applet code = "WelcomeApplet.class" width = "300" height = "45"> </applet> </html> appletviewer only understands <applet> tags Ignores everything else Minimal browser Executing the applet appletviewer WelcomeApplet.html Perform in directory containing.class file

3.3 Simple Java Applet: Drawing a String Running the applet in a Web browser

More applets 3.4 Drawing Strings and Lines First example Display two lines of text Use drawstring to simulate a new line with two drawstring statements Second example Method g.drawline(x1, y1, x2, y2 ) Draws a line from (x1, y1) to (x2, y2) Remember that (0, 0) is upper left Use drawline to draw a line beneath and above a string

import java.awt.*; import java.applet.*; public class WelcomeApplet2 extends Applet { public void init() { } } public void paint(graphics g) { } g.drawstring( "Welcome to", 25, 25 ); g.drawstring( "Java Programming!", 25, 40 ); The two drawstring statements simulate a newline. In fact, the concept of lines of text does not exist when drawing strings. Modified by Evan Korth

<html> <applet code = "WelcomeApplet2.class" width = "300" height = "60"> </applet> </html> HTML file Program Output

import java.awt.*; import java.applet.*; public class WelcomeApplet3 extends Applet { WelcomeLines.ja va } public void init() { } public void paint(graphics g) { g.drawstring("welcome to Java Programming!", 25, 25 ); g.drawline (15, 10, 210, 10); g.drawline (15, 30, 210, 30); } 2. Class WelcomeLines (extends Applet) 3. paint 3.1 drawline 3.2 drawline Draw horizontal 3.3 lines drawstring with drawline (endpoints have same y coordinate). Program Output Modified by Evan Korth

<html> <applet code = "WelcomeApplet3.class" width = "300" height = "40"> </applet> </html> HTML file

3.4 Drawing Strings and Lines Method drawline of class Graphics Takes as arguments Graphics object and line s end points X and y coordinate of first endpoint X and y coordinate of second endpoint

Applet Code for House

Applet Methods public void init() public void start() public void paint( Graphics g ) public void stop() public void destroy()

Method public void init() When the method is called and its purpose This method is called once by the applet container when an applet is loaded for execution. It performs initialization of an applet. Typical actions performed here are initializing fields, creating GUI components, loading sounds to play, loading images to display (see Chapter 19, Multimedia) and creating threads (see Chapter 16, Multithreading). public void start() This method is called after the init method completes execution. In addition, if the browser user visits another Web site and later returns to the HTML page on which the applet resides, method start is called again. The method performs any tasks that must be completed when the applet is loaded for the first time and that must be performed every time the HTML page on which the applet resides is revisited. Typical actions performed here include starting an animation (see Chapter 19) and starting other threads of execution (see Chapter 16). public void paint( G raphi cs g ) This drawing method is called after the init method completes execution and the start method has started. It is also called every time the applet needs to be repainted. For example, if the user covers the applet with another open window on the screen and later uncovers the applet, the paint method is called. Typical actions performed here involve drawing with the G raphics object g that is passed to the paint method by the applet container. public void stop() public void destroy() Fig. 6.11 This method is called when the applet should stop executing normally, when the user of the browser leaves the HTML page on which the applet resides. The method performs any tasks that are required to suspend the applet s execution. Typical actions performed here are to stop execution of animations and threads. This method is called when the applet is being removed from memory normally, when the user of the browser exits the browsing session (i.e., closes all browser windows). The method performs any tasks that are required to destroy resources allocated to the applet. A pplet methods that the applet container calls during an applet s execution.

Life Cycle of Applets

Passing Parameter to Applets <PARAM..> tag is used to pass the parameter value from HTML file to Applet code. In the example shown below parameter which is used is named as name and value of the parameter is initialized as Achin Jain. Now in the Applet code, you should use same parameter name as name to fetch the value

Applet Code

HTML Code

Getting Input from User As you know that Applets works in Graphics environment where inputs are treated as Strings. So in order to get input from user, we need to create TextField just we used to do in HTML Coding. Below is the example that takes input from user as Number and second TextField displays the Square of that number.

Applet Code

HTML Code

Example to Display Image in Applet An applet can display images of the format GIF, JPEG, BMP, and others. To display an image within the applet, you use the drawimage() method found in the java.awt. Graphics class.

Applet Code

HTML Code

Example to Play Audio in Applet An applet can play an audio file represented by the AudioClip interface in the java.applet package. The AudioClip interface has three methods, including: public void play(): Plays the audio clip one time, from the beginning. public void loop(): Causes the audio clip to replay continually. public void stop(): Stops playing the audio clip.

Applet Code

HTML Code