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

Similar documents
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

Chapter 7 Applets. Answers

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

CSC System Development with Java Introduction to Java Applets Budditha Hettige

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

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


SIMPLE APPLET PROGRAM

Using the API: Introductory Graphics Java Programming 1 Lesson 8

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

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

G51PRG: Introduction to Programming Second semester Applets and graphics

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,

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

Introduction to Computer Science I

Chapter 3 - Introduction to Java Applets

Java - Applets. public class Buttons extends Applet implements ActionListener

PROGRAMMING LANGUAGE 2

CS335 Graphics and Multimedia

Java TM Applets. Rex Jaeschke

Java Applet Basics. Life cycle of an applet:

9. APPLETS AND APPLICATIONS

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

CSC 551: Web Programming. Fall 2001

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

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

Prashanth Kumar K(Head-Dept of Computers)

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

CSC 551: Web Programming. Fall 2001

Using ImageZoom 2. Zoom In. Quick Zoom In: To double the size of the image, click on the point that you want to zoom in.

Graphics Applets. By Mr. Dave Clausen

Introduction to Java Applets 12

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

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

Lecture Static Methods and Variables. Static Methods

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

CHAPTER 7 WEB SERVERS AND WEB BROWSERS

Chapter 27. HTTP and WWW

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

UNIT -1 JAVA APPLETS

Java Applets / Flash

Web Development & Design Foundations with HTML5, 8 th Edition Instructor Materials Chapter 11 Test Bank

CSD Univ. of Crete Fall Java Applets

Netscape Introduction to the JavaScript Language

Component Based Software Engineering

CS2 Advanced Programming in Java note 8

Lecture Static Methods and Variables. Static Methods

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

Higher National Diploma in Information Technology First Year, Second Semester Examination 2015

Applets as front-ends to server-side programming

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

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

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

Advanced Internet Programming CSY3020

GUI, Events and Applets from Applications, Part III

SNS COLLEGE OF ENGINEERING, Coimbatore

SEMESTER ONE EXAMINATIONS SOLUTIONS 2003/2004

CSC 1214: Object-Oriented Programming

CST272 Getting Started Page 1

Object-Oriented Programming EE219 Repeat 2004/2005 Page 1 of 8

CHAPTER 2. Java Overview

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

Inheritance 2. Inheritance 2. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

Lesson 5 Introduction to Cascading Style Sheets

UCLA PIC 20A Java Programming

Garfield AP CS. Graphics

The figure below shows the Dreamweaver Interface.

For live Java EE training, please see training courses at

IQTIDAR ALI Lecturer IBMS Agriculture University Peshawar

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

Creating a Graphical LED cluster bean IBM Visual Age for Java - Creating Custom Beans

Graphics Applets. By Mr. Dave Clausen

Outline. Announcements. Feedback. CS1007: Object Oriented Design and Programming in Java. Java beans Applets

Building Java Programs

Brief Summary of Java

WebVisit User course

Programmierpraktikum

Understanding How Java Programs Work

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

Introduction to the Java T M Language

Chapter 2 Exercise Solutions

CT 229 Arrays in Java

Chapter 12 Advanced GUIs and Graphics

Java History. Java History (cont'd)

1. Setup a root folder for the website 2. Create a wireframe 3. Add content 4. Create hyperlinks between pages and to external websites

Summary. 962 Chapter 23 Applets and Java Web Start

Using Graphics. Building Java Programs Supplement 3G

Building Java Programs

Java. GUI building with the AWT

ADF Code Corner. 71. How-to integrate Java Applets with Oracle ADF Faces. Abstract: twitter.com/adfcodecorner

Object-oriented programming in Java (2)

Java applets & the RealJ IDE

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

1. What is Jav a? simple

Last Class: Network Overview. Today: Distributed Systems

Lab 3: Work with data (IV)

A socket is a software endpoint that establishes bidirectional communication between a server program and one or more client programs.

Developer Manual. Guide to Library Implementation 1 CONTENT TERMINOLOGY LIBRARY IMPLEMENTATION COMPONENT IMPLEMENTATION...

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Transcription:

INTRODUCTION TO COMPUTER PROGRAMMING Richard Pierse Class 9: Writing Java Applets Introduction Applets are Java programs that execute within HTML pages. There are three stages to creating a working Java Applet. Firstly, the applet itself must be written in the Java language and compiled into a class. Secondly, a web page must be written in HTML that includes a link to the Java applet class, using one of the tags <APPLET> or <OBJECT>. Finally, both the HTML page and the applet need to be uploaded to a web server. Writing a Java Applet The structure of a Java applet differs in some respects from that of a stand-alone Java application. Firstly, the main class of a Java applet must be derived as an extension of the standard Java class java.applet.applet. This class does not include a main() method as with stand-alone applications. Secondly, the method init() should always be included in the main class to initialise the applet. In a stand-alone application, initialisation is done in the class constructor. However, the class constructor of an applet may be called before the hosting program (web browser) is ready to initialise the applet, so the init() method is provided instead. Other methods of the class java.applet.applet that may be overridden by your subclass are: public void start() which is called to tell the applet to start doing its task, and public void paint(graphics g) which is called to draw the applet on the screen. This latter method is passed a Graphics object, which can be used to display text or draw shapes in the applet window. 1

A Simple Applet Here is the skeleton for a simple Java applet. import java.applet.*; // required for applet import java.awt.*; // required for graphics public class MyApplet extends java.applet.applet public void init() public void paint(graphics g) g.setcolor(color.red); g.drawstring( Welcome to Java!!,50,60); This applet uses the setcolor and drawstring methods of the Graphics class to set the pen colour to red and then write a line to the applet window. The first two import lines load the necessary libraries: java.applet, which is required for any applet and java.awt, which is required in order to use Graphics objects. Creating Java Applets in JCreator JCreator provides a user-friendly environment in which to build new applets. Select New... from the main menu and choose the Basic Java applet template. A new project is created having two files: a template for the Java applet itself and a simple HTML file to display the applet. These files should be edited to include content for your applet. Then go to the Build menu and compile the project to create the Java class and then execute the project to test the applet. Finally, the.class and.html files can be uploaded to a web server using WS_FTP to make them available on the WWW. 2

The HTML <APPLET> and <OBJECT> Tags A Java applet can be included in an HTML page using either the <APPLET> or the <OBJECT> tag. Both tags are equivalent although the <OBJECT> tag is preferred and can be used to refer to compiled objects other than Java classes. The format is: or <APPLET CODE= my.class HEIGHT=30 WIDTH=40> </APPLET> <OBJECT CODE= my.class HEIGHT=30 WIDTH=40> </OBJECT> The CODE parameter indicates the name of the Java applet. The parameters HEIGHT and WIDTH are required and specify the initial height and width of the object on the page. (This initial size can subsequently be changed by the applet itself). Two other parameters that may be needed are CODEBASE and ARCHIVE. The browser expects to look for the applet code in the current directory. If the applet (and any files associated with it), are located elsewhere, then the location should be specified in the CODEBASE parameter. For example, the value CODEBASE=../myapplets causes the browser to look for the file my.class in the directory myapplets located one level higher up the tree than the current directory. Applets may comprise more than a single class. In this case, all the classes comprising the applet can be compiled together into a Java archive. Archives are stored in files with the suffix.jar. These files can be compressed so that they will load faster in the web browser. The location of the Java archive containing the applet is specified with the ARCHIVE parameter as in: ARCHIVE= myfiles.jar 3

Passing Parameters to Applets Many applets use parameters to control the behaviour and screen appearance of the applet. Parameters are passed to the applet from the browser using <PARAM> tags between the <APPLET> and </APPLET> tags. The format is: <PARAM NAME= speed VALUE= 20 > This passes to the applet a parameter called speed which is set to the value 20. If Java has been disabled in the web browser, then the <PARAM> tags are simply ignored, but any text included between the <APPLET> and </APPLET> will be displayed. (This text is ignored if Java is enabled). This provides a mechanism for notifying users with disabled browsers that they are missing out on an applet. Reading Parameters in Applets Within the applet itself, parameters can be read using the getparameter method. In the above example, the value of the parameter speed could be read with the following code: String temp; int speed; temp = getparameter( speed ); if(temp == null) speed = 1; else speed = Integer.parseInt(temp); The parameter value is first read into the String variable temp. If the speed parameter had not been defined by a <PARAM> tag in the HTML code calling the applet, then this string would contain a null value. In this case, the integer variable speed is set to the default value of 1. Otherwise, the string variable temp is converted to an integer using the Integer.parseInt method and the result stored in speed. 4

Modifying Our Simple Applet To modify our simple applet to read a parameter message to set the message to display and a parameter colour to select the text colour of the message, we could use the following code: int colour; String message; public void init() String temp; temp = getparameter( message ); if(temp == null) message = Welcome to Java!! ; else message = temp; temp = getparameter( colour ); if(temp == null) colour = 0; else colour = Integer.parseInt(temp); public void paint(graphics g) if(colour == 1) g.setcolor(color.blue); else if(colour == 2) g.setcolor(color.green); else g.setcolor(color.red); g.drawstring(message,50,100); The variables colour and message are defined as data members of the applet class. This makes them "global" so that they are available to all methods in the class. They are defined in the init() method called when the applet is initialised and then used in the paint() method when the applet is displayed. Note that only three values are recognised for the parameter colour : 1 = blue, 2 = green, any other value = red. To call this applet from a web page, we could use the HTML code: <APPLET CODE= MyApplet.class HEIGHT=500 WIDTH=300> <PARAM NAME= message VALUE= Hi there! > <PARAM NAME= colour VALUE= 1 > </APPLET> 5

A Note on JavaScript One feature you will certainly come across if you take a look at many actual HTML pages is the use of JavaScript. JavaScript is a language that looks rather similar to Java (both being based on C and C++) but is much less powerful. Invented by Netscape for their browser, it is mainly used for achieving small dynamic effects such as making hyperlinks change appearance when a mouse hovers over them, or pre-checking HTML forms before they are submitted. One fundamental difference between Java and JavaScript is that JavaScript source code appears directly in HTML documents within <SCRIPT> and </SCRIPT> tags. This code is interpreted at run time by the web browser. In contrast, Java applets are compiled before being uploaded to the web and the browser merely executes the compiled bytecode. This means that Java applets will generally run much faster than their equivalent JavaScript counterparts. Also, the source code for a Java applet is hidden from the end-user in a way not possible in JavaScript. Some Example Applets I have created a web page which illustrates the use of our simple applet and also includes some other (more interesting) applets taken from the web site: www.javaboutique.internet.com Some of these applets contain Java source code, which you can study. They illustrate the wide range of uses of the Java applet. Look at the source code of the HTML file index.html in the java directory. This will show you the parameters that are needed to run the various applets. You can try to incorporate these (or other applets from the Java boutique site) into your own HTML pages. 6