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

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

PROGRAMMING LANGUAGE 2

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

SIMPLE APPLET PROGRAM

Java Applet Basics. Life cycle of an applet:

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

9. APPLETS AND APPLICATIONS

Java Applet & its life Cycle. By Iqtidar Ali

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

Object Oriented Programming Concepts-15CS45

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

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

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

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

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

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Java TM Applets. Rex Jaeschke

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

Graphical User Interface (GUI)

Chapter 7 Applets. Answers

G51PRG: Introduction to Programming Second semester Applets and graphics

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

User interfaces and Swing

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. 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

ASSIGNMENT NO 14. Objectives: To learn and demonstrated use of applet and swing components

Graphical User Interface (GUI)

CSC System Development with Java Introduction to Java Applets Budditha Hettige

UNIT -1 JAVA APPLETS

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

IT101. Graphical User Interface

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

Introduction to Computer Science I

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

CT 229 Arrays in Java

Advanced Internet Programming CSY3020

Graphics Applets. By Mr. Dave Clausen

Mathematics for Computer Graphics - Lecture 13

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

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

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

SNS COLLEGE OF ENGINEERING, Coimbatore

(Refer Slide Time: 02:01)

CSC 1214: Object-Oriented Programming

Java Applets / Flash

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

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Graphics Applets. By Mr. Dave Clausen


Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Introduction to Java Applets 12

Chapter 3 - Introduction to Java Applets

Chapter 14: Applets and More

Introduction to the Java T M Language

Sri Vidya College of Engineering & Technology

Windows and Events. created originally by Brian Bailey

Methods (Deitel chapter 6)

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,

Methods (Deitel chapter 6)

CSC 551: Web Programming. Fall 2001

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Chapter 14: Applets and More

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

PESIT Bangalore South Campus

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

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

CSD Univ. of Crete Fall Java Applets

CS2 Advanced Programming in Java note 8

Friend Functions. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CSC207H: Software Design Lecture 11

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

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

PESIT Bangalore South Campus

Java - Applets. public class Buttons extends Applet implements ActionListener

Programmierpraktikum

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

Part 3: Graphical User Interface (GUI) & Java Applets

Programming Language Concepts: Lecture 8

Programming: You will have 6 files all need to be located in the dir. named PA4:

MODULE 8p - Applets - Trials B

Here is a list of a few of the components located in the AWT and Swing packages:

Chapter 12 Advanced GUIs and Graphics

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

UCLA PIC 20A Java Programming

Packages: Putting Classes Together

Java. GUI building with the AWT

Painting your window

CS/120 Final Exam. Name

Namespaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Core JAVA Training Syllabus FEE: RS. 8000/-

GUI Output. Adapted from slides by Michelle Strout with some slides from Rick Mercer. CSc 210

Graphics -- To be discussed

CS1004: Intro to CS in Java, Spring 2005

The AWT Package, Graphics- Introduction to Images

Chapter 1 GUI Applications

Inheritance Systems. Merchandise. Television Camcorder Shirt Shoe Dress 9.1.1

CHAPTER 2. Java Overview

Lecture 3: Java Graphics & Events

Transcription:

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

The Applet Class

Types of Applets (Abstract Window Toolkit) Offers richer and easy to use interface than AWT.

An Applet is a Java program that runs in a web browser. An Applet is a Java class that extends Applet class. Applet class is present in java.applet.*; java.applet.*; and java.awt.*; packages must be imported to write applet programs. No main() method. Applets are designed to be embedded within an HTML(Hyper Text Markup Language) page. JVM is required to view an applet. Applet have strict security rules that are enforced by web browser. Java enabled web browser is required to run an Applet. (or appletviewer is required)

Applet Architecture An applet is a window-based program. As such, its architecture is different from the console-based programs. First, applets are event driven. An applet waits until an event occurs. The run-time system notifies the applet about an event by calling an event handler that has been provided by the applet. Once this happens, the applet must take appropriate action and then quickly return.

Second, the user initiates interaction with an applet not the other way around. In a nonwindowed program, when the program needs input, it will prompt the user and then call some input method, such as readline( ). This is not the way it works in an applet. Instead, the user interacts with the applet as he or she wants, when he or she wants. These interactions are sent to the applet as events to which the applet must respond. For example, when the user clicks the mouse inside the applet s window, a mouse-clicked event is generated. If the user presses a key while the applet s window has input focus, a keypress event is generated.

An Applet Skeleton Consists of 4 methods: init( ) start( ) stop( ) destroy( ) These are defined in Applet. Applets do not need to override those methods they do not use. AWT-based applets will also override the paint( ) method, which is defined by the AWT Component class. This method is called when the applet s output must be redisplayed.

Applet Initialization and Termination (Life cycle of an applet) It is important to understand the order in which the various methods shown in the skeleton are called. When an applet begins, the following methods are called, in this sequence: 1. init( ) 2. start( ) 3. paint( ) When an applet is terminated, the following sequence of method calls takes place: 1. stop( ) 2. destroy( ) When minimized: 1. stop() When Maximized: 1. start() 2. paint()

init() The init( ) method is the first method to be called. This is where you should initialize variables. This method is called only once during the run time of your applet. start() The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. start( ) is called each time an applet s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ).

paint(graphics g) The paint( ) method is called each time your applet s output must be redrawn. For example, the window in which the applet is running may be overwritten by another window and then uncovered. Or the applet window may be minimized and then restored. paint( ) is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called. The paint( ) method has one parameter of type Graphics.

stop() The stop( ) method is called when a web browser leaves the HTML document containing the applet. For example : when it goes to another page. You should use stop( ) to suspend threads that don t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.

destroy() The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources the applet may be using. The stop( ) method is always called before destroy( ).

The Applet Class Applet provides all necessary support for applet execution. It also provides methods that load and display images, and methods that load and play audio clips. Applet extends the AWT class Panel. In turn, Panel extends Container, which extends Component. These classes provide support for Java s window-based, graphical interface. Thus, Applet provides all of the necessary support for window-based activities. The Applet class defines the methods shown in Table:

Simple Applet Program AppletEx.java demo.html

Simple Applet Display Methods To output a string to an applet, use drawstring( ), which is a member of the Graphics class. Typically, it is called from within paint() method. It has the following general form: void drawstring(string message, int x, int y) Here, message is the string to be output beginning at x, y. In a Java window, the upper-left corner is location 0,0. The drawstring( ) method will not recognize newline characters. If you want to start a line of text on another line, you must do so manually, specifying the precise X,Y location where you want the line to begin.

To set the background color of an applet s window, use setbackground( ). To set the foreground color, use setforeground( ). These methods are defined by Component, and they have the following general forms: void setbackground(color newcolor) void setforeground(color newcolor) Here, newcolor specifies the new color. The class Color defines the constants shown here that can be used to specify colors:

The following example sets the background color to green and the text color to red: setbackground(color.green); setforeground(color.red); A good place to set the foreground and background colors is in the init( ) method.

You can obtain the current settings for the background and foreground colors by calling getbackground( ) and getforeground( ), respectively. They are also defined by Component and are shown here: Color getbackground( ) Color getforeground( )

Using the Status Window In addition to displaying information in its window, an applet can also output a message to the status window of the browser or applet viewer on which it is running. To do so, call showstatus( ) with the string that you want displayed. The status window is a good place to give the user feedback about what is occurring in the applet, suggest options, or possibly report some types of errors. The status window also makes an excellent debugging aid, because it gives you an easy way to output information about your applet. Ex: StatusWindow.java

Requesting Repainting The repaint( ) method is defined by the AWT. repaint() calls paint(). Thus, for another part of your applet to output to its window, simply store the output and then call repaint( ). The AWT will then execute a call to paint( ), which can display the stored information. For example, if part of your applet needs to output a string, it can store this string in String variable and then call repaint( ). Inside paint( ), you will output the string using drawstring( ).

The repaint( ) method has four forms. 1. void repaint( ) This version causes the entire window to be repainted. The following version specifies a region that will be repainted: 2. void repaint(int left, int top, int width, int height) Here, the coordinates of the upper-left corner of the region are specified by left and top, and the width and height of the region are passed in width and height. These dimensions are specified in pixels. You save time by specifying a region to repaint. If you need to update only a small portion of the window, it is more efficient to repaint only that region.

The other 2 forms are as follows: void repaint(long maxdelay) void repaint(long maxdelay, int x, int y, int width, int height) Ex: SimpleBanner.java

References Herbert Schildt, The Complete Reference, JAVA, 7 th ed