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.

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth.
TCU CoSc Programming with Java Handling Events.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet.
Java Applets A First Program. Applet Example /* The world’s simplest program, repeated once more in another format in an attempt to turn all of you into.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
 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.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
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.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
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.
28-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.
A Simple Applet.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
 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.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
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.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Java Applet Presented by Fitsum Okubu. Introduction Introduction Graphics Graphics Methods and Variables Methods and Variables Events Events Decision.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
Graphics in Java Starring: NetBeans Co-Starring: Java.awt.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
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,
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
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.
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.
Creating a Java Application and Applet
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
CSC 205 Programming II Lecture 5 AWT - I.
Applets.
Handling User Events with Swing
A First Look at GUI Applications
Lecture 09 Applets.
Object Oriented Programming
Chapter 13: Advanced GUIs and Graphics
UNIT-5.
Java Applets.
Java Programming COMP-417 Applet
Presentation transcript:

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 getting a.class file as before Create an HTML file (say HelloWorld.HTML in same directory as.class file) and include in this in simplest case with no parameters where applet will run in window of given width and height (in pixels)

More on Applets Given the following HTML <APPLET CODE="StockGraph" CODEBASE=" " WIDTH=200 HEIGHT=200> Runs the "StockGraph.class" executable as an applet

Java applets Java application executes from a command window an applet is a Java program that runs in the appletviewer or in a Web browser The appletviewer (or browser) executes an applet when a HTML document containing the applet is opened in the appletviewer (or browser).

Applets in Code Warrior Start a new project in CW choose java stationary and applet (in a window jdk1.3) Project with three source files: TrivialApplet.java, TrivialApplet.html and TrivialAppletDebug.html Open the java file. – import java.awt.*; – import java.applet.Applet; We import objects from two packages which supports graphical user interface: AWT (abstract windowing tool). There is a newest version of class Applet called JApplet, from the new package java.swing (SWING is built on awt and exdends its capabilities).

Applets in Code Warrior We import objects from two packages which supports graphical user interface: – AWT (abstract windowing tool). – the new package java.swing (SWING is built on awt and exdends its capabilities) public class TrivialApplet extends Applet The extends keyword followed by a class name indicates the class (in this case Applet) from which our new class inherits existing pieces. In this inheritance relationship, Applet is called the superclass or base class and TrivialApplet is called the subclass or derived class. We will discuss inheritance in detail later. Using inheritance a new class TrivialApplet has the data and methods of the Applet class as well as the new features, like a method paint(), which overrides Applet's method paint(). three methods init, start and paint that are guaranteed to be called automatically for you when any applet begins execution. – called exactly in that order

init, start methods init public void init() Called by the browser or applet viewer to inform this applet that it has been loaded into the system. It is always called before the first time that the start method is called. A subclass of Applet should override this method if it has initialization to perform. start public void start() Called by the browser or applet viewer to inform this applet that it should start its execution. It is called after the init method and each time the applet is revisited in a Web page. A subclass of Applet should override this method if it has any operation that it wants to perform each time the Web page containing it is visited. For example, an applet with animation might want to use the start method to resume animation, and the stop method to suspend the animation.

init, start and paint methods The appletviewer or browser expects each of these methods to be defined so it can provide a consistent start-up sequence for an applet. Some sample code import java.awt.*; import java.applet.Applet; public class test extends Applet { // Initialize the applet public void init() { } // Paint graphics on the screen public void paint(Graphics g) { g.drawString( "Hello World!!!", 30, 30 ); }

drawString method The first argument to drawString is the string to draw The last two arguments in the list, 30 and 30, are the coordinates at which the bottom-left corner of the string should be drawn in the applet's area on the screen Coordinates are measured from the upper-left corner of the applet in pixels: first 30 (x-coordinate) is a number of pixel to the right, and the second parameter (y-coordinate) is number of pixel down.

Executing a Java Applet provide an HTML text file that indicates which applet the appletviewer (or browser) should load and execute

Java Resources There is a large number of Java applet resources available to you. The best place to start is

Event Listeners If a class want to respond to a user event, it must implement the interface (we will discuss interfaces in a few lectures) that deals with the event These interfaces are called event listeners. Each listener handles a specific kind of event A sample of event listeners: – ActionListener - action event that generated by a user taking an action such as a click on a button. – AdjustmentListener - an event that generated when the componenet such as a scrollbar is moved. – ItemListener - item event that generated when an item such as a check box has been changed.

Event Listeners more sample event listeners: – KeyListener - keyboard event that occurs when a user types on a keyboard. – MouseListener - mouse event that generated by a mouse click. – MouseMotionListener - mouse event that generated by a mouse movement. – WindowListener - window event that generated by window move or adjustment.

an example of a proper class declaration: public class Foo extends Applet implements ActionListener, MouseListener {... } By making a class event listener you have to set up a specific type of event You have to add a matching listener. Once a component (a button, for example) is created you can add one of following methods associated with it:

Listener Methods addActionListener() - Button, CheckBox, ComboBox, TextField, RadioButton. addAdjustmentListener() - ScrollBar. addItemListener() - Button, CheckBox, ComboBox, RadioButton. addKeyListener() - all components. addMouseListener() - all components. addMouseMotionListener() - all components.

Names names of the component may be different depending on which package you use All above components are in AWT (import java.applet.Applet; ) In the new package SWING (import javax.swing.JApplet; ), all above components have the capital letter J in front: JButton, JCheckBox and so on.

An Example create a button and associate an action event listener JButton b = new JButton("give him a name"); b.addActionListener(this); The ActionListener interface contains only one method actionPerformed() public void actionPerformed(ActionEvent e) { //what to do when a user //clicks the button? }

Example public void actionPerformed(ActionEvent e) { if (e.getSource() == buttonOne)... else... } If you have more than one componenet has an event listener, you have to figure out which one does what. The class ActionEvent (a parameter in actionPerformed()) has a method getSource() which can help to determine which component generated the event.