Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java (150704).   Applet  AWT (Abstract Window Toolkit)  Japplet  Swing Types of Applet.
Advertisements

1 Chapter 17 Applets and Multimedia. 2 Motivations F When browsing the Web – you frequently see the graphical user interface –animation developed using.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 18 Applets and Multimedia.
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.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
1 Applets. 2 Motivations When browsing the Web, you frequently see the graphical user interface and animation developed using Java. These programs are.
Event-Driven Programming
Applets, Graphical User Interfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads.
Applet class The Applet class provides the essential framework that enables applets to be run by a web browser Applet do not have main method Applet depend.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets Java API.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java 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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
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.
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
1 Chapter 14 Applets, Images, and Audio. 2 Objectives F To explain how the Web browser controls and executes applets (§ 14.2). F To describe the init,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved GUI: Applets and Multimedia.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Applets and Multimedia.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Object Oriented Programming.  Interface  Event Handling.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Copyright © Curt Hill Applets A different type of program.
Creating User Interfaces Event-Driven Programming.
Creating a Java Application and Applet
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Advanced Programming Rabie A. Ramadan Lecture 5.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
Chapter 14 Event-Driven Programming
Chapter 17 Applets and Multimedia
Applets.
Chapter 12 Event-Driven Programming
Chapter 12 Applets and Advanced Graphics
UNIT-5.
Java Programming COMP-417 Applet
11.1 Applets & graphics.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Presentation transcript:

Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters to Applets Conversions Between Applications and Applets Conversions Between Applications and Applets Running a Program as an Applet and as an Application Running a Program as an Applet and as an Application Handling the Mouse Event Handling the Mouse Event Handling the Keyboard Event Handling the Keyboard Event Model dynamic behavior using sequence diagrams and statecharts diagrams Model dynamic behavior using sequence diagrams and statecharts diagrams Advanced Layout (CardLayout and GridBagLayout and using No Layout Manager) (Optional) Advanced Layout (CardLayout and GridBagLayout and using No Layout Manager) (Optional)

The Applet Class public class MyApplet extends Applet { public void init() public void init() {... } {... } public void start() public void start() {... } {... } public void stop() public void stop() {... } {... } public void destroy() public void destroy() {... } {... } //your other methods //your other methods}

Browser Calling Applet Methods

The init() Method Invoked when the applet is first loaded and again if the applet is reloaded. Invoked when the applet is first loaded and again if the applet is reloaded. Common functions implemented in this method include Common functions implemented in this method include  creating threads,  loading images,  setting up user-interface components,  getting parameters from the tag in the HTML page.

The start() Method Invoked after the init() method is executed; also called whenever the applet becomes active again after a period of inactivity Invoked after the init() method is executed; also called whenever the applet becomes active again after a period of inactivity For example, when the user returns to the page containing the applet after surfing other Web pages. For example, when the user returns to the page containing the applet after surfing other Web pages. Functionality might include restarting threads (for example, to resume an animation) or simply telling the applet to run again. Functionality might include restarting threads (for example, to resume an animation) or simply telling the applet to run again.

The stop() Method The opposite of the start() method, which is called when the user moves back to the page containing the applet; the stop() method is invoked when the user moves off the page. The opposite of the start() method, which is called when the user moves back to the page containing the applet; the stop() method is invoked when the user moves off the page. When the user leaves the page, any threads the applet has started—but not completed—will continue to run. When the user leaves the page, any threads the applet has started—but not completed—will continue to run.

The destroy() Method Invoked when the browser exits normally to inform the applet that it is no longer needed and that it should release any resources it has allocated. Invoked when the browser exits normally to inform the applet that it is no longer needed and that it should release any resources it has allocated. Usually, you will not need to override this method unless you need to release specific resources, such as threads that the applet created. Usually, you will not need to override this method unless you need to release specific resources, such as threads that the applet created.

Example 10.1 Using Applets Objective: Compute mortgages. The applet enables the user to enter the annual interest rate, the number of years, and the loan amount. Click the Compute Mortgage button, and the applet displays the monthly payment and the total payment. Objective: Compute mortgages. The applet enables the user to enter the annual interest rate, the number of years, and the loan amount. Click the Compute Mortgage button, and the applet displays the monthly payment and the total payment. MortgageApplet Run Applet Viewer

Writing Applets Always extends the JApplet class, which is a subclass of Applet for Swing components. Always extends the JApplet class, which is a subclass of Applet for Swing components. Override init(), start(), stop(), and destroy() if necessary. By default, these methods are empty. Override init(), start(), stop(), and destroy() if necessary. By default, these methods are empty. Add your own methods and data if necessary. Add your own methods and data if necessary. Applets are always embedded in an HTML page. Applets are always embedded in an HTML page.

The HTML Tag <applet code=classfilename.class code=classfilename.class width=applet_viewing_width_in_pixels width=applet_viewing_width_in_pixels height=applet_viewing_height_in_pixels height=applet_viewing_height_in_pixels [archive=archivefile] [archive=archivefile] [codebase=applet_url] [codebase=applet_url] [vspace=vertical_margin] [vspace=vertical_margin] [hspace=horizontal_margin] [hspace=horizontal_margin] [align=applet_alignment] [align=applet_alignment] [alt=alternative_text] [alt=alternative_text]> </applet>

Running Applets in Java Plug-In Why to Use Java Plug-In? Java Plug-in enables Web browsers to run Java applets consistently on all the platforms.

How to Use Java Plug-In Convert the HTML file to a new HTML file using the HTMLConverter Utility. The new HTML file contains the tags for invoking the Java Plug-In. Convert the HTML file to a new HTML file using the HTMLConverter Utility. The new HTML file contains the tags for invoking the Java Plug-In. If the Plug-In is not installed, the new HTML file automatically downloads it from the Sun JavaSoft Web site. If the Plug-In is not installed, the new HTML file automatically downloads it from the Sun JavaSoft Web site.

Passing Parameters to Applets <applet code = "DisplayMessage.class" code = "DisplayMessage.class" width = 200 width = 200 height = 50 height = 50 alt="You must have a Java-enabled browser to view the applet" alt="You must have a Java-enabled browser to view the applet"> </applet>

Example 10.2 Passing Parameters to Java Applets Objective: Display a message at a specified location. The message and the location (x, y) are obtained from the HTML source. Objective: Display a message at a specified location. The message and the location (x, y) are obtained from the HTML source. Run Applet Viewer DisplayMessage

Conversions Between Applications and Applets Conversions between applications and applets are simple and easy. Conversions between applications and applets are simple and easy. You can always convert an applet into an application. You can always convert an applet into an application. You can convert an application to an applet as long as security restrictions are not violated. You can convert an application to an applet as long as security restrictions are not violated.

Example 10.3 Converting Applets into Applications Objective: Convert MortgageApplet (from Example 10.1) to a Java application. Objective: Convert MortgageApplet (from Example 10.1) to a Java application. Run MortgageApplication

Example 10.4 Converting Applications into Applets Objective: Convert a Java application that performs simple numeric calculations ( MenuDemo, from Example 9.11) into a Java applet. Objective: Convert a Java application that performs simple numeric calculations ( MenuDemo, from Example 9.11) into a Java applet. Run Applet Viewer AppletMenuDemo

Example 10.5 Running a Program as an Applet and as an Application Objective: Modify MortgageApplet to enable it to run both as an applet and as an application. Objective: Modify MortgageApplet to enable it to run both as an applet and as an application. Run as ApplicationRun as Applet DisplayMessageApp

Handling Mouse Events Java provides two listener interfaces, MouseListener and MouseMotionListener, to handle mouse events. Java provides two listener interfaces, MouseListener and MouseMotionListener, to handle mouse events. The MouseListener listens for actions such as when the mouse is pressed, released, entered, exited, or clicked. The MouseListener listens for actions such as when the mouse is pressed, released, entered, exited, or clicked. The MouseMotionListener listens for actions such as dragging or moving the mouse. The MouseMotionListener listens for actions such as dragging or moving the mouse.

Example 10.6 Moving Message Using Mouse Objective: Create a program to display a message in a panel. You can use the mouse to move the message. The message moves as the mouse drags and is always displayed at the mouse point. Objective: Create a program to display a message in a panel. You can use the mouse to move the message. The message moves as the mouse drags and is always displayed at the mouse point. Run as Application MoveMessageDemo Run as Applet

Example 10.7 Handling Complex Mouse Events Objective: Create a program for drawing using a mouse. Draw by dragging with the left mouse button pressed; erase by dragging with the right button pressed. Objective: Create a program for drawing using a mouse. Draw by dragging with the left mouse button pressed; erase by dragging with the right button pressed. Run as Application Scribble Demo Run as Applet

Handling Keyboard Events keyPressed(KeyEvent e) keyPressed(KeyEvent e) Called when a key is pressed. keyReleased(KeyEvent e) keyReleased(KeyEvent e) Called when a key is released. keyTyped(KeyEvent e) keyTyped(KeyEvent e) Called when a key is pressed and then released. To process a keyboard event, use the following handlers in the KeyListener interface:

The KeyEvent Class Methods: Methods: getKeyChar() method getKeyCode() method Keys: Keys: HomeVK_HOME EndVK_End Page UpVK_PGUP Page DownVK_PGDN etc...

Example 10.8 Keyboard Events Demo Objective: Display a user-input character. The user can also move the character up, down, left, and right using the arrow keys. Objective: Display a user-input character. The user can also move the character up, down, left, and right using the arrow keys. Run as Applet KeyboardEventDemo Run as Application

Sequence diagrams Sequence diagrams describe interactions among objects by depicting the time ordering of method invocations. Sequence diagrams describe interactions among objects by depicting the time ordering of method invocations.

Dynamic Behavior Modeling Sequence Diagrams Sequence Diagrams  class role: the roles that object plays  Life line: the existence of an object over a period of time  activation: the time that an object is performing an operation.  method invocation: communication between objects. Statecharts: states and transiton Statecharts: states and transiton

Sequence diagrams, cont.

Statechart diagrams Statechart diagrams describe flow of control of the object. Statechart diagrams describe flow of control of the object.

Statechart diagrams, cont.

Example 10.9 The TicTacToe Game Run as Applet TicTacToeRun as Application

Advanced Layout CardLayout CardLayout GridBagLayout GridBagLayout Using No Layout Manager Using No Layout Manager

CardLayout The CardLayout manager arranges components in a queue of cards. The CardLayout manager arranges components in a queue of cards. You can only see one card at a time. You can only see one card at a time. To construct a CardLayout, simply use the constructor CardLayout(). To construct a CardLayout, simply use the constructor CardLayout(). To add a component in the CardLayout container, use the following method: To add a component in the CardLayout container, use the following method: void add(Component com, String name)

CardLayout View Components void first(container) void first(container) void last(container) void last(container) void next(container) void next(container) void previous(container) void previous(container) void show(Container, String name) void show(Container, String name)

Example Testing CardLayout Manager ShowCardLayout Run Objective: Create two panels in a frame. The first panel holds named components. The second panel uses buttons and a choice box to control which component is shown. Objective: Create two panels in a frame. The first panel holds named components. The second panel uses buttons and a choice box to control which component is shown.

GridBagLayout The GridBagLayout manager is the most flexible and the most complex. It is similar to the GridLayout manager in the sense that both layout managers arrange components in a grid. The components can vary in size, however, and can be added in any order in GridBagLayout. Example 10.11: Using GridBagLayout Manager RunShowGridBagLayout

Using No Layout Manager You can place components in a container without using any layout manager. In this case, the component must be placed using the component’s instance method setBounds(). Example 10.12: Using No Layout Manager RunShowNoLayout