Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.

Slides:



Advertisements
Similar presentations
Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets Running Java applets. Security.
Advertisements

Dept. of Computer Science - SSBN Java Applets Vishnuvardhan.M.
Slides prepared by Rose Williams, Binghamton University Mouse Events + Chapter 17: Applets.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
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.
 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
Java Applets. Lecture Objectives Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
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.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 20 Applets Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
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.
JAPPLET.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Chapter 13. Applets and HTML HTML Applets Computer Programming with JAVA.
Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 13 l HTML l Applets Applets and HTML.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
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.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
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,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Applets Yong Choi School of Business CSU, Bakersfield.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document.
Chapter 131 Applets and HTML Chapter Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
 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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 17 : Applets King Fahd University of Petroleum & Minerals College of Computer Science.
Chapter 14 Applets. Applets: Introduction Java programs are divided into two main categories, applets and applications An application is an ordinary Java.
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Java Applets.
Chapter 8 Graphics.
Java Applets.
Lecture 09 Applets.
Object Oriented Programming
Java Applets.
Chapter 13: Advanced GUIs and Graphics
Chapter 10 Graphics.
Distributed Computing, M. L. Liu
UNIT-5.
Java Applets.
Java Applets.
Chapter 8 Graphics.
Applet in Java.
Advanced GUIs and Graphics
Presentation transcript:

Java Applets

Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java applets  Running Java applets.  Security issues with Java applets.

Introduction  Java programs are divided into two main categories, applets and applications.  An application is an ordinary Java program.  An applet is a kind of Java program that can be run across the Internet.

Programming Applets  The word applet is meant to suggest a small application.  Applets were intended to be small programs run over the Internet:  However, there are no size constraints on applets.  Applets can be viewed over the Internet, or without any connection to the internet.  An applet is similar to a Swing GUI:  In fact, almost all of the Swing techniques can be used in applets.

Defining an Applet  An applet class is normally defined as a derived class of the class Japplet.  The class JApplet is in the package javax.swing.  There is also an older class, Applet, which has been superseded by the JApplet class.

Designing an Applet  An applet class can be designed as a derived class of JApplet in much the same way that regular Swing GUIs are defined as derived classes of Jframe.  However, an applet normally defines no constructors.  The method init performs the initializations that would be performed in a constructor for a regular Swing GUI

Designing an Applet (Cont’d)  Components can be added to an applet in the same way that a component is added to a JFrame  The method add is used to add components to an applet in the same way that components are added to a JFrame

Java Applets: An Example 16 } Output using an applet viewer

How Applets Differ from Swing GUIs?  Some of the items included in a Swing GUI are not included in an applet  Applets do not contain a main or setVisible method  Applets are displayed automatically by a Web page or an applet viewer  Applets do not have titles  Therefore, they do not use the setTitle method  They are normally embedded in an HTML document, and the HTML document can add any desired title

How Applets Differ from Swing GUIs? (Cont’d)  Applets do not use the setSize method  The HTML document takes care of sizing the applet  Applets do not have a close-window button  Therefore, they do not have a setDefaultCloseOperation method  When the HTML document containing the applet is closed, then the applet is automatically closed

Running an Applet  An applet class is compiled in the same way as any other Java class  However, an applet is run differently from other Java programs  The normal way to run an applet is to embed it in an HTML document  The applet is then run and viewed through a Web browser

 An applet can also be viewed using an applet viewer  An applet viewer is a program designed to run an applet as a stand-alone program  The Java appletviewer can be used to run an applet: appletviewer FirstApplet.html  It may be necessary, however, to create the HTML document, and place the applet in it Running an Applet (Cont’d)

Menus in a JApplet  Menus are constructed and added to a JApplet as they are for a Jframe  JApplet has a method named setJMenuBar that behaves the same as the setJMenuBar method of a JFrame  JApplet can also have menu bars added to a JApplet or to a panel that is part of the JApplet using the add method

Tip: Converting a Swing Application to an Applet  The fastest and easiest way to explain how to define an applet, is to explain how to modify a Swing GUI to transform it into an applet 1. Derive the class from the class JApplet instead of from the class Jframe 2. Remove the main method 3. Replace the constructor with a no-parameter method named init – The body of the init method can be the same as the body of the deleted constructor, but with some items removed

Tip: Converting a Swing Application to an Applet (2) 4. Delete any invocation of super 5. Delete any method invocations that program the close-window button of a windowing GUI 6. Delete any invocation of setTitle 7. Delete any invocation of setSize  The following applet was generated in this way.

The Applet Calculator

An Applet with an Icon Output using an applet viewer

Inserting an Applet in an HTML Document  An applet can be placed in an HTML document with an applet tag: <applet code="PathToApplet" width=Number1 height=Number2>  If given a.class file name only, then the HTML file and the applet file must be in the same directory  The PathToApplet can be a full or relative path name

Inserting an Applet in an HTML Document (Cont’d)  Note that the name of the.class file, not the.java file, is given  Note also that the width and height of the applet is given in this command, and not within the applet class definition  The width and height are in pixels  The following code, when placed in an HTML document, will display the calculator applet in a browser as shown <applet code="AppletCalculator.class" width=400 height=300>

An Applet in an HTML Document Vampire Control......

Pitfall: Using an Old Web Browser  An old browser may not be able to run applets from an HTML document:  Even if a java application runs correctly on the same system.  Web browsers do not use the same Java Virtual Machine used to run regular Java applications.  An old browser will have an old Java Virtual Machine, or perhaps, no Java Virtual Machine.  However, an applet viewer will work, as long as a recent version of Java is installed.

Getting started…  Create a new java class file  After that build the file … the you will get tow files

Getting started…  Now use the previous code in to a regular HTML document Welcome Applets

Developing Applets  Java applets do not need a ‘main’ method. they can run in a web browser environment  The applet have  Init()  Start()  Stop()  Paint( Graphics g)

init() called by the browser or applet viewer to inform this applet that it has been reloaded to the system We use init() to:  Initialize variables  Get data from user  Place various GUI component.

start() & stop()  Start() //called by the browser or applet viewer to inform this applet that it should start its execution  Stop() //called by the browser or applet viewer to inform this applet that it should stop its execution

paint( Graphics g) is used to create the output. whenever you override this method the first java statement is super.paint(g);  To draw a string we use drawString method  Public abstract void drawstring(String str, int x, int y)

paint( Graphics g)  To change the text font  setFont (new Font(“font name”, ”font style”, font_size)  To chang the text color setColor(Color.red)  The available constant colors  White, Black, blue, cyan, darkGray,gray,lightGray,red, yellow,pink, orange, magenta,green Using JDK guarantees the following fonts:Font style Serif Font.PLAIN Sanserif Font.BOLD Monospaced Font.ITALIC Dialog DialogInput

Example

JAVA application VS. applets  Java applications and Applets share some common programming features although they differ in some aspects GUIApplets Is derived from class JframeIs derived from class JApplet Have “MAIN” methodDo not have main method instead It have init(), start(), stop(), paint(..) Uses the class constructor to initialize the GUI component and data members Uses the init() method to initialize the GUI component and data members Uses setSize(),setTitle(), setVisible() methode don not use the because the HTML document do the job.

converting JAVA application to applets 1. Change the extends from JFrame to JApplets. 2. Change the constructor to method init(). 3. Remove method calls like setSize(),setTitle(), setVisible(). 4. Remove the method main

Simple application Example

Convert to applet

39 The java.awt package  “awt” stands for “Abstract Window Toolkit”  The java.awt package includes classes for:  Drawing lines and shapes  Drawing letters  Setting colors  Choosing fonts  If it’s drawn on the screen, then java.awt is probably involved!

40 Importing the java.awt package  Since you may want to use many classes from the java.awt package, simply import them all: import java.awt.*;  The asterisk, or star (*), means “all classes”  The import directives can go in any order, but must be the first lines in your program

41 The applet so far import java.applet.Applet; import java.awt.*;

42 Your applet class public class Drawing extends Applet { … }  Drawing is the name of your class  Class names should always be capitalized  extends Applet says that our Drawing is a kind of Applet, but with added capabilities  Java’s Applet just makes an empty window  We are going to draw in that window  The only way to make an applet is to extend Applet

43 The applet so far import java.applet.Applet; import java.awt.*; public class Drawing extends Applet { …we still need to put some code in here... }

44 The paint method  Our applet is going to have a method to paint some colored rectangles on the screen  This method must be named paint  paint needs to be told where on the screen it can draw  This will be the only parameter it needs  paint doesn’t return any result

45 The paint method, part 2  public void paint(Graphics g) { … }  public says that anyone can use this method  void says that it does not return a result  A Graphics (short for “Graphics context”) is an object that holds information about a painting  It remembers what color you are using  It remembers what font you are using  You can “paint” on it (but it doesn’t remember what you have painted)

46 The applet so far import java.applet.Applet; import java.awt.*; public class Drawing extends Applet { public void paint(Graphics g) { …we still need to put some code in here… } }

47 Colors  The java.awt package defines a class named Color  There are 13 predefined colors—here are their fully-qualified names:  For compatibility with older programs (before the naming conventions were established), Java also allows color names in lowercase: Color.black, Color.darkGray, etc. Color.BLACKColor.PINK Color.GREEN Color.DARK_GRAYColor.REDColor.CYAN Color.GRAYColor.ORANGEColor.BLUE Color.LIGHT_GRAYColor.YELLOW Color.WHITE Color.MAGENTA

48 Setting a color  To use a color, we tell our Graphics g what color we want: g.setColor(Color.RED);  g will remember this color and use it for everything until we tell it some different color

49 The paint method so far public void paint(Graphics g) { g.setColor(Color.BLUE); …draw a rectangle… g.setColor(Color.RED); …draw another rectangle… } }

50 Pixels  A pixel is a picture (pix) element  one pixel is one dot on your screen  there are typically 72 to 90 pixels per inch  java.awt measures everything in pixels

51 Java’s coordinate system  Java uses an (x, y) coordinate system  (0, 0) is the top left corner  (50, 0) is 50 pixels to the right of (0, 0)  (0, 20) is 20 pixels down from (0, 0)  (w - 1, h - 1) is just inside the bottom right corner, where w is the width of the window and h is its height (0, 0) (0, 20) (50, 0) (50, 20) (w-1, h-1)

52 Drawing rectangles  There are two ways to draw rectangles:  g.drawRect( left, top, width, height );  g.fillRect( left, top, width, height );

53 The complete applet import java.applet.Applet; import java.awt.*; public class Drawing extends Applet { public void paint(Graphics g) { g.setColor(Color.BLUE); g.fillRect(20, 20, 50, 30); g.setColor(Color.RED); g.fillRect(50, 30, 50, 30); } }

54 Some more java.awt methods  g.drawLine( x1, y1, x2, y2 );  g.drawOval( left, top, width, height );  g.fillOval( left, top, width, height );  g.drawRoundRect( left, top, width, height );  g.fillRoundRect( left, top, width, height );  g.drawArc( left, top, width, height, startAngle, arcAngle );  g.drawString( string, x, y );

55 The HTML page  You can only run an applet in an HTML page  The HTML looks something like this:  DrawingApplet Applet