1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.

Slides:



Advertisements
Similar presentations
Internet Services and Web Authoring (CSET 226) Lecture # 5 HyperText Markup Language (HTML) 1.
Advertisements

Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
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.
Java Applets A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia.
 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.
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.
Applets, Graphical User Interfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads.
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.
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.
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.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
Applets, HTML and GUI’s Recitation – 04/11/2008 CS 180 Department of Computer Science, Purdue University.
PHY281Introduction to JavaSlide 1 Introduction to Java In this section we will learn how how to use Java and write our first Java Applet:  The Java Development.
Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.
Java Unit 5: Applets and Graphics Web Pages and Viewing Applets.
Basics of HTML.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
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.
CP2022 Multimedia Internet Communication1 HTML and Hypertext The workings of the web Lecture 7.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 5 - Graphics.
JAPPLET.
Web Programming Basics of HTML. HTML stands for Hyper Text Mark-up Language A mark-up language is different than those that you have learned before in.
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.
HTML INTRODUCTION. What is HTML?  HTML stands for Hypertext Markup Language  Developed in 1990  Hidden code that helps us communicate with others on.
1 What is HTML? Standardized codes Web pages SGML Descriptive markup Tags.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Chapter 8 Introduction to HTML and Applets Fundamentals of Java.
1 Week 1 l HTML l Applets Applets and HTML. 2 Overview l Applets: Java programs designed to run from a document on the Internet l HTML: Hypertext Markup.
Chapter 13. Applets and HTML HTML Applets Computer Programming with JAVA.
1 Programming Fundamentals AITI-GP. 2 Introduction to Programming.
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
Chapter 4: Applets and Graphics 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 4 Applets and Graphics.
HTML Structure & syntax. Introduction This presentation introduces the following: Doctype declaration HTML Tags, Elements and Attributes Sections of a.
Chapter 4 Fundamental Data Types. Chapter Goals To be able to write simple applets To display graphical shapes such as lines and ellipses To use colors.
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.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. In order to display a drawing in a frame, define a class that extends.
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.
Lec.10 + (Chapter 8 & 9) GUI Java Applet Jiang (Jen) ZHENG July 6 th, 2005.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance Java Applets –what is an applet vs. an application? –where do applets.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.
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 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
HTML Basics. HTML Coding HTML Hypertext markup language The code used to create web pages.
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.
HTML-I Basic HTML Elements. HTML (Hyper Text Markup Language) HTML is a document layout and hyperlink- specification language. i.e. a language used to.
HTML. INDEX Introduction to HTML Creating Web Pages Commands And Tags Web Page.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
Object Oriented Programming
Chapter Two Applet vs. Application
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Java Applets.
Handout-14 Applets and Graphics
AN INTRODUCTION BY FAITH BRENNER
APPLET PROGRAMMING.
Presentation transcript:

1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes »Rectangle »Ellipse »Lines l Preview: More on Applets

2 What is an Applet? l An applet is a program that runs in a World Wide Web browser when an HTML document containing the applet is loaded into the browser. l The code for an applet is enclosed in a class, which is declared as public and as an extension of a standard class called Applet. l You run applets within a page on internet(browser) or within another program called appletviewer. l Unlike java applications, applets do not have main method. l The standard Applet class is contained in the package called java.applet and must be imported at the beginning of each applet program l Applets can do most of the things that applications can do but limited by some security restrictions.

3 Steps for creating and executing an applet l When you create an applet, you do the following: 1. Write the applet in the Java programming language, and save it with a.java file extension, just as when you write an application 2. Compile the applet into bytecode using the javac command, just as when you do with an application 3. Write an HTML document that includes a statement to call your compiled java class 4. Load the HTML document into a Web browser(such as Internet Explorer or, Netscape Navigator)

4 What is HTML? l HTML or Hypertext Markup Language is a simple language used to create Web pages for the Internet. l It contains many commands that allow you to format text on a Web page, import graphic images, and link your page to other Web pages. l These commands are case insensitive and are usually called HTML Tags. l You can always identify a tag by the brackets( ) that enclose the tag name. Example: The sentence: Java is an Object-oriented language. is displayed by the browser as: Java is an Object-oriented language. Obviously, the pair and are used to indicate italics.

5 Basics HTML tags l The following are some of the basic HTML tags that we need load our applet: l … : These are used to indicate the beginning and the end of a HTML document. The entire content of a HTML file should be enclosed in these pair of codes. l … : A HTML file consists of a head and a body and these tags are used to indicate the beginning and end of the head part. l … : This should appear inside the head part of a HTML file and is used to specify the information to be displayed on the browser’s title bar. For our case, this is the only tag that we need to include in the head part. l … : These are used to indicate the beginning and end of the body part of a HTML file. Almost all the contents of a html document appear within these pair of tags.

6 Basics HTML tags (Cont’d)... : This is used to instruct the browser to load an applet. l Notice the additional specifications the applet tag: CODE, WIDTH and HEIGHT. These are called attributes of the tag. There are many other attributes for the Applet tag but these are the required ones. The CODE is used to specify the applet file (.class file), while WIDTH and HEIGHT are used to specify the size of the window in which the applet is displayed. The following is a simple applet to display the message “Hello World” import java.applet.Applet; import java.awt.Graphics; public class HelloApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); }

7 Basics HTML tags (Cont’d) The following is a sample HTML file, which may be used to load the HelloApplet example. A Simple Program l The following are sample output when the applet is loaded in Windows explorer and Applet viewer:

8 Drawing simple graphical shapes l Java provides a package (java.awt) called the Abstract Window Toolkit(AWT) to facilitate graphics and GUI programming. l When you wish to draw graphics, you must import the AWT this package. l An applet has a paint method, which is executed by the browser each time the applet is displayed. There are few other methods that an applet may have which will be discussed later. l The paint method receives an object of class Graphics and is used to draw lines, ellipses etc. on a web page. It is also use to set font sizes and colors. Graphics is one of the classes in the java.awt package. l However, recent versions of Java come with another graphics class called Graphics2D, which is better than the previous one. We shall be using this new class instead.

9 Drawing simple graphical shapes (cont’d) l To use the Graphics2D, we only need to convert the object of class Graphics, which is passed to the paint method into an object of class Graphics2D using casting. public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g... l In drawing graphics, the browser’s page is considered as a drawing board with the top right-hand corner as the origin (0, 0). The size of the graphic shape that we wish to draw should be specified relative to this origin

10 Drawing a rectangle l To draw a rectangle, we need to create an object of class Rectangle of the java.awt package and then draw it using the draw method of the Graphics2D object. l To create an object of class Rectangle, we need to specify the following parameters: »The top-left corner of the rectangle (x, y co-ordinates) »The width of the rectangle »The height of the rectangle. import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; public class RectangleApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Rectangle rectangle = new Rectangle(10,10,50,50); g2.draw(rectangle); }

11 Drawing Ellipse l To draw an Ellipse, we need to import the Ellipse2D class of the java.awt.geom. package. l The Ellipse2D class contained two Ellipse classes, Ellipse2D.Float which uses float coordinates and Ellipse2D.Double uses double coordinates. We shall be using Ellipse2D.Double l Again, we need to create an object of class Ellipse2D.Double and then use the draw method of the Graphics2D object to draw it. import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; public class CircleApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Ellipse2D.Double circle1 = new Ellipse2D.Double(10,10,50,50); Ellipse2D.Double circle2 = new Ellipse2D.Double(20,20,30,30); g2.draw(circle1); g2.draw(circle2); }

12 Drawing lines l To draw a line, we need to create an object of class Line2D.Double of the java.awt.geom. package. To create such object, we need to specify the two end-points the line. This can be done in two ways: l By giving the x and y coordinates of both points as in: Line2D.Double line = new Line2D(x1, y1, x2, y2); l Or we can specify each point as an object as: Point2D.Double from = new Point2D.Double(x1, y1); Point2D.Double to = new Point2D.Double(x2, y2); Line2D.Double line = new Line2D.Double(from, to); l The latter method is more object-oriented and more useful particularly if the point objects need to be re-used.

13 Drawing lines (Cont’d) import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; public class TriangleApplet extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Point2D.Double pointA = new Point2D.Double(30,10); Point2D.Double pointB = new Point2D.Double(10,50); Point2D.Double pointC = new Point2D.Double(50,50); Line2D.Double lineAB = new Line2D.Double(pointA, pointB); Line2D.Double lineBC = new Line2D.Double(pointB, pointC); Line2D.Double lineCA = new Line2D.Double(pointC, pointA); g2.draw(lineAB); g2.draw(lineBC); g2.draw(lineCA); }