Java Applets. Introduction to Java and Java Applets Java applications Run in stand-alone mode No additional software required (such as a Web browser)

Slides:



Advertisements
Similar presentations
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Advertisements

LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
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.
Object Oriented Programming with Java (150704).   Applet  AWT (Abstract Window Toolkit)  Japplet  Swing Types of Applet.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Graphics Programming With Applets Feb 23, Applets There are three different types of executable java code. –Standalone application, which has main()
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
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.
A Simple Applet --- Digital Clock import java.awt.*; import java.util.Calendar; public class DigitalColok extends java.applet.Applet implements Runnable.
Enahnced Digital Clock Applet Setting applet parameters in the web page. The applet tag in HTML:
Applets, Graphical User Interfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
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.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
1 Features of Java CS 3331 Fall Outline  Abstract class  Interface  Application --- animation applets.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets Java API.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
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.
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.
Internet Software Development Applets Paul J Krause.
Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.
Applets.
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.
POS 406 Java Technology And Beginning Java Code
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.
1 CSC Computer Education (P) Ltd. DESIGNED BY K PRAKASH,
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
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.
Java Applets 1. What is an applet? An applet is a small Java program that is typically embedded in a Web page and can be run using the applet viewer or.
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.
Java applets. Outline What is Java? Java Applications Java Applets Java Applets Securities Summary.
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.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD.
1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document.
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.
Copyright © Curt Hill Applets A different type of program.
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.
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.
1 Java Applet Basics Chapter Eight. 2 Applets vs. Applications l Applications: Stand alone Java programs run by interpreter l Applets WWW browser embedded.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Applet: An applet is a java program that is transmitted over the network from the server to client & executed within clients browser. Applets are used.
Applications Active Web Documents Active Web Documents.
Object Oriented Programming
Java Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Java Applets.
Java Applets.
Java applets 1/3/2019.
Java Programming COMP-417 Applet
Applet Fundamentals Applet are small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run.
Presentation transcript:

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 Compiled Java class files Run within a Web browser (or an appletviewer) Loaded from anywhere on the Internet security restrictions!

Java Basic Concepts Source Code converted to Byte code Byte code -machine code of JVM (Java Virtual Machine) Each real machine must have own JVM Interpretation JIT compilation Direct Execution Java Byte Code consists of 1 Byte opcode 1 or more operands

Capabilities and Limitations of Applets Build full-featured graphical user interfaces (suitable for the Web) Communicate over the Internet to a host server (support Client-Server architecture) Communicate with other applets on a form Environment-neutral (any platform) Limitations on Java applets to ensure client security

Capabilities and Limitations of Applets Bytecode verification Forces loaded Java applets to undergo a rigorous set of checks in order to run on the local system The verifier checks each bytecode before it is executed to make sure that it is not going to perform an illegal operation Client-side precautions Most Web browsers preclude Java applets from doing file access or communicating with any computer on the Internet other than the computer that the applet was loaded from Enforced by the client Web browser (or other applet loader) but done by a part of the Java runtime engine known as the class loader

First Java Applet import java.awt.*; //Contains all of the classes for creating user interfaces //and for painting graphics and images import java.applet.Applet; public class HelloFromVenus extends Applet { public void paint(Graphics g) { Dimension d = getSize(); g.setColor(Color.orange); g.fillRect(0,0,d.width,d.height); g.setFont(new Font("Sans-serif",Font.BOLD,24)); g.setColor(new Color(255, 10, 0)); g.drawString("Hello From Venus, a Mars Colony!", 40, 25); g.drawImage(getImage(getCodeBase(),"venus.jpg"), 20, 60, this); }

HTML Source Hello From Venus Applet Here is the Hello From Venus Applet The source.

Elements of Java Applets Superclass: java.applet.Applet extend javax.swing.JApplet if you have swing components Swing: Sun's set of GUI components that give much fancier screen displays than the raw AWT No main() method paint() method paints the picture Applet tags: code width height

Compile and Run an Applet To compile: javac HelloFromVenus.java  Generates HelloFromVenus.class To run: a) Use the appletviewer from JDK appletviewer Venus.html b) Open page from browser: Venus.html

Applet’s Life Each applet has four major events in its lifetime: Initialization --- init() Starting --- start() Painting --- paint(Graphics) Stopping --- stop() Destroying --- destroy() The methods defined Applet class Except for paint()  in class java.awt.Container do nothing--they are stubs You make the applet do something by overriding these methods

Applet’s Life When an applet begins the following sequence of methods is called init() informs applet that it has been loaded into the system Called only once an ideal place to initialize variables and create UI objects start() informs applet that it should start its execution Right after init() Each time the page is loaded and restarted paint(Graphics) When an applet dies (or is terminated), the following sequence of method calls takes place: stop() informs applet that it should stop its execution When a web browser leaves the HTML document

Applet’s Life destroy() informs applet that it is being reclaimed and that it should destroy any resources that it has allocated Use destroy() to explicitly release system resources (like threads) Usually released automatically (Auto garbage collection) Called only once when the environment determines that your applet needs to be removed completely from memory The stop() method is always called before destroy() no guarantee that this method will be completely executed The Java Virtual Machine might exit before a long destroy method has completed

Methods are called in this order init and destroy are only called once each start and stop are called whenever the browser enters and leaves the page do some work is code called by your listeners paint is called again when the applet needs to be repaint ed init() start() stop() destroy() paint do other work

public void paint(Graphics g) Needed if you do any drawing or painting other than just using standard GUI Components Any painting you want to do should be done here, or in a method you call from here For painting done in other methods Never call paint(Graphics), always call repaint( ) Life Cycle Applet via AppletViewer Automatically called when when the applet begins execution the window in which the applet is running may be overwritten by another window and then uncovered the applet window is resized

Other Applet Methods public void repaint() public void update (Graphics) public void showStatus(String) public String getParameter(String) ava/applet/Applet.html

repaint( ) Call repaint() when you have changed something and want your changes to show up on the screen after drawing commands ( drawRect(...), fillRect(...), drawString(...), etc.) Outside paint repaint( ) is a request it might not happen! When you call repaint( ), Java schedules a call to update(Graphics g) public void update(Graphics g) { // Fills applet with background // color, then paint(g); }

Sample Graphics methods A Graphics is something you can paint on g.drawRect(x, y, width, height); g.fillRect(x, y, width, height); g.drawOval(x, y, width, height); g.fillOval(x, y, width, height); g.setColor(Color.red); g.drawString(“Hello”, 20, 20); Hello

Drawing Strings g.drawString("A Sample String", x, y)

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 Range of 0 to 255 Set of constants defined in java.awt.Color

The java.awt.Font Class Fonts are specified with three attributes: font name: Serif Sans-serif Monospaced Dialog DialogInput TimesRoman Helvetica Courier Dialog 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 )

The java.awt.Graphics Class Represent Graphics Context A graphics context is an abstraction of various drawing surfaces: -screen -printer -off-screen image (an image stored in memory) Provide a rich set of graphics methods drawString() drawLine() drawArc() fillArc() drawOval() fillOval() drawPolygon() fillPolygon() drawRect() fillRect() drawRoundRect() fillRoundRect()

The java.awt.Graphics Class (cont'd) setColor(color) set the current color setFont(font) set the current font setPaintMode() set the paint, or overwrite mode setXORMode(color) set the XOR mode getColor() get the current color getFont() get the current font getFontMetrics() get the font metrics of the current font getFontMetrics(font) get the font metrics for the specified font

showStatus(String s) showStatus(String s) displays the String in the applet’s status line Each call overwrites the previous call You have to allow time to read the line!

Example Applet import java.awt.*; import java.applet.Applet; import javax.swing.JOptionPane; //try it in eclipse using AppletViewer public class LifeCycleApplet extends Applet { Font theFont = new Font("Helvetica", Font.BOLD, 20); String Status; public void init(){ Status = "Initializing!"; showStatus("The applet is initializing!"); JOptionPane.showMessageDialog(this,Status); repaint();} public void start(){ Status += "--Starting!"; showStatus("The applet is starting!"); JOptionPane.showMessageDialog(this,Status); repaint();} }

Example Applet public void stop(){ Status += "--Stopping!"; showStatus("The applet is stopping!"); JOptionPane.showMessageDialog(this,Status); repaint();} public void destroy(){ Status += "--Destroyed!"; showStatus("The applet is being destroyed!"); JOptionPane.showMessageDialog(this,Status); //might cause freezing problems due to //unpredictability of when VM calls this method repaint(); }

Example Applet public void paint(Graphics g){ Status += "--Painting!"; Dimension d = getSize(); g.setColor(Color.orange); g.fillRect(0,0,d.width,d.height); g.setFont(theFont); g.setColor(Color.blue); g.drawString("Author:"+getParameter("FName")+" "+getParameter("LName"),50,50); g.drawString("URL of the applet : " + getCodeBase(), 50, 100); g.drawString("URL of document : " + getDocumentBase(), 50, 150); g.drawString(Status, 50, 200); showStatus("The applet is painting!"); // JOptionPane.showMessageDialog(this,Status); }

HTML Tags Hi World Applet

HTML Source Clock The Digital Clock Applet <applet code= DigitalClock.class width=400 height=100> The source

The Tag The syntax for using the tag is the following: alternate_content The APPLET attributes are standard values that all applets accept and are a standard part of HTML The applet_parameter_tags contain applet-specific parameters that are read by the applet at runtime This is a handy way of passing arguments to an applet to allow the applet to be more generic

The Tag Tag Attributes ALT -Alternate text that can be displayed by text-only browsers ALIGN -The ALIGN attribute designates the alignment of the applet within the browser page CODE -(Required) The CODE attribute is used to indicate the.class file that loads the applet CODEBASE -The CODEBASE attribute is used to indicate the location of the.class file that loads the applet HEIGHT -(Required) The HEIGHT attribute is used to set the applet's bounding rectangle height HSPACE -The HSPACE attribute sets the amount of horizontal space to set off around the applet NAME -The NAME attribute sets the symbolic name of the applet VSPACE -The VSPACE attribute sets the amount of vertical space to set off around the applet WIDTH -(Required) The WIDTH attribute is used to set the applet's box width

The Tag Passing Parameters to Java Applets Parameters are an easy way to configure Java applets without actually changing the source file Background color based on preference (different HTML files) In the previous applet example, the text drawn on the screen was drawn using the blue color This was "hardwired" into the applet's code However, just as easily, we could have passed a parameter to the applet specifying that it use the blue tag See next example

The Tag // Passing parameters to the applet using HTML parameters. This is the LifeCycle applet! Prepare to be amazed! If you can see this, your browser does not support Java applets The only question left to be answered is this: how does the Java applet determine the value of the parameters?

The Tag The answer is that the applet has to call the getParameter() method supplied by the java.applet.Applet parent class Calling getParameter("color") using the previous Java applet example would return a String value containing the text "blue“ It is then left up to the applet to take advantage of this information and actually paint the text blue on the screen Here are three methods commonly used by applets: String getParameter(String name) : Returns the value for the specified parameter string URL getCodeBase() : Returns the URL of the applet URL getDocumentBase(): Returns the URL of the document containing the applet