Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].

Slides:



Advertisements
Similar presentations
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.
Advertisements

Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
OOP & JAVA. HelloWorld.java /** * The HelloWorld class is an application that * displays "Hello World!" to the standard output. */ public class HelloWorld.
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, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
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.
A Simple Applet --- Digital Clock import java.awt.*; import java.util.Calendar; public class DigitalColok extends java.applet.Applet implements Runnable.
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.
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.
APPLETS CSC 171 FALL 2004 LECTURE 6. APPLETS Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
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.
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.
Applets. What is an Applet?  According to Sun “An applet is a small program that is intended not to be run on its own, but rather to be embedded inside.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
1 Features of Java CS 3331 Fall Outline  Abstract class  Interface  Application --- animation applets.
Java Applets. Applets The term Applet refers to a little application. In JAVA the applet is a java program that is embedded within a HTML document and.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets Java API.
Agenda For Feb Finish up Unit 3 Exercises on page Unit 4 Exercises on page 33. Question #2 3. Create a Happy Face Java Applet (due next class).
Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to.
JAVA APPLETS By: Ms. Humaira Siddiqui. Java and the Internet Java is considered to be a language well suited to be used in the internet. In contrast with.
Internet Software Development Applets Paul J Krause.
Applets.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
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.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
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,
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.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
Java applets. Outline What is Java? Java Applications Java Applets Java Applets Securities Summary.
APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.
Lecture 15.1 Static Methods and Variables Static Methods In Java it is possible to declare methods and variables to belong to a class rather than.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Features of Java CS 3331 Sections and 5.5.
Your name CSCI/CMPE 3326 Object-Oriented Programming in Java Dongchul Kim Department of Computer Science University of Texas – Pan American 1.Applet.
1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document.
Chapter 8 Frameworks. Frameworks Framework is a set of cooperating classes and interface types that structures the essential mechanisms of a particular.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
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.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
Java Applets Getting Started. Import Files In order to run the applet properly, we have to import some files into our class. These files are: –import.
Java Applets. Introduction to Java and Java Applets Java applications Run in stand-alone mode No additional software required (such as a Web browser)
Java Applets.
Topic: Applets Course : JAVA PROGRAMMING Paper Code: ETCS-307
User Interface Programming In Java
Object Oriented Programming
Java Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Java Applets.
Introduction to Computing Using Java
Java Applets.
Java applets 1/3/2019.
Applet in Java.
Java Programming COMP-417 Applet
11.1 Applets & graphics.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
A Brief History January 1996: first official release JDK 1.0
Presentation transcript:

Applets CS 3331 Sections 3.3 & 4.7 of [Jia03]

Outline Basics of applets First applet – HelloWorldApplet.java More on applets Animation applet – digital clock

What Are Applets? An applet is a special Java program that can be embedded in HTML documents. It is automatically executed by (applet-enabled) web browsers. In Java, non-applet programs are called applications.

Application vs. Applet Application Applet Trusted (i.e., has full access to system resources) Invoked by Java Virtual Machine (JVM, java), e.g., java HelloWorld Should contain a main method, i.e., public static void main(String[]) Applet Not trusted (i.e., has limited access to system resource to prevent security breaches) Invoked automatically by the web browser Should be a subclass of class java.applet.Applet

Examples HelloWorld.java HelloWorldApplet.java public class HelloWord { public static void main(String[] args) { System.out.println(“Hello, World!”); } HelloWorldApplet.java

First Java Applet Java source in HelloWorldApplet.java import java.awt.*; import java.applet.Applet; public class HelloWorldApplet extends Applet { public void paint(Graphics g) { Dimension d = getSize(); g.setColor(Color.BLACK); g.fillRect(0, 0, d.width, d.height); // paint background g.setFont(new Font("San-serif", Font.BOLD, 24)); g.setColor(new Color(255, 215,0)); g.drawString("Hello, world!", 60, 40); g.drawImage(getImage(getCodeBase(), “Rabbit.jpg"), 20, 60, this); } Represents graphics context, an abstraction of various drawing surfaces, e.g., screen, printer, off-screen image (an image stored in memory). Provide a rich set of graphics methods

Graphics Coordinate (0,0) x height y width

Embedding Applet in HTML HTML source in HelloWorld.html <!--HelloWorld.html--> <html> <head> <title>HelloWord</title> </head> <body> <center> <applet code="HelloWorldApplet.class" width=300 height=350></applet> </center> <hr/> <a href="HelloWorldApplet.java">The source.</a> </body> </html>

Compiling and Running To compile javac HelloWorldApplet.java Produces HelloWorldApplet.class To run Open page HelloWorld.htmlfrom web browser or Use appletviewer of JDK appletviewer HelloWorld.html

Elements of Applets Superclass: java.applet.Applet No main method paint method to paint the picture Applet tag: <applet> </applet> code width and height

Outline Basics of applets First applet – HelloWorldApplet.java More on applets Animation applet – digital clock

Framework-Based Programming (OO) Frameworks Semi-complete applications. Provide the structure (backbone) and utilities for applications. Often domain specific. Inversion of control. Examples: applets, GUI frameworks, etc.

The Class Applet

The Life-Cycle of Applet init() Called exactly once in an applet’s life. Called when applet is first loaded, which is after object creation, e.g., when the browser visits the web page for the first time. Used to read applet parameters, start downloading any other images or media files, etc.

Applet Life-Cycle (Cont.) start() Called at least once. Called when an applet is started or restarted, i.e., whenever the browser visits the web page. stop() Called when the browser leaves the web page.

Applet Life-Cycle (Cont.) destroy() Called exactly once. Called when the browser unloads the applet. Used to perform any final clean-up. init start destroy stop start

Outline Basics of applets First applet – HelloWorldApplet.java More on applets Animation applet – digital clock

Animation Applet --- Digital Clock import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Calendar; public class DigitalClock extends java.applet.Applet { <Fields> <Methods> } An applet must be a subclass of java.applet.Applet.

Program Structure java.applet.Applet DigitalClock + start(): void + stop(): void + paint(g: Graphics) : void 1 javax.swing.Timer <<use>> java.util.Calendar <<use>> <<use>> java.awt java.awt.event

Methods for Digital Clock public void start(){...} invoked when entering the web page that contains the applet public void stop(){...} invoked when leaving the web page that contains the applet public void paint(Graphics g){...} paint the picture

Field Declarations protected Timer timer; protected Font font = new Font("Monospaced", Font.BOLD, 48); protected Color color = Color.GREEN;

Object Initialization public DigitalClock() { timer = new Timer(1000, createTimerTickHandler()); } protected ActionListener createTimerTickHandler() { return new ActionListener() { public void actionPerformed(ActionEvent event) { repaint(); }; //--- Or in Java 8 public DigitalColoc() { timer = new Timer(1000, e -> repaint());

The start() and stop() Methods public void start() { timer.start(); } public void stop() { timer.stop(); Start and stop the timer Stopped timer will not consume CPU time.

The paint() Method public void paint(Graphics g) { Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); g.setFont(font); g.setColor(color); g.drawString(String.format(“%02d:%02d:%02d”, hour, minute, second), 10, 60); }

Who Calls the paint()method? Timer ticks and calls ActionListener.actionPerformed() ActionListener.actionPerformed()calls DigitalClock.repaint() DigitalClock.repaint() calls DigitalClock.paint() The paint() method is usually not called directly.

Drawing Strings g.drawString("A sample string", x, y)

HTML Source <!-- DigitalClock.html --> <html> <head> <title>Digital Clock Applet</title> </head> <body bgcolor=black> <h1>The Digital Clock Applet</h1><p> <applet code=DigitalClock.class width=250 height=80> </applet> <p><hr> <a href=DigitalClock.java>The source</a> </body> </html>

The java.awt.Color Class Instances of the Color class represent colors. new Color(r, g, b) where r, g, b are the values of the red, green, and blue components, respectively. They are in the in the range of 0 to 255. Predefined constants BLACK ORANGE YELLOW BLUE GREEN PINK CYAN LIGHTGRAY RED ARKGRAY MAGENTA WHITE

The java.awt.Font Class Fonts are specified with three attributes: font name: Serif Sans-serif Monospaced Dialog DialogInput TimesRoman Helvetica Courier font style: PLAIN BOLD ITALIC Styles can be combined: Font.BOLD|Font.ITALIC font size: a positive integer A font can be created as follows: new Font(name, style, size)

Exercise Write an applet class named CuckooDigitalClock that behaves like DigitalClock except that it cuckoos at every hour by playing an audio file “cuckoo.au”. Hints To play an audio file, use play(getCodeBase(), “cuckoo.au”). To get the current time, use the class java.util.Calendar, e.g., Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND);