Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 9.

Slides:



Advertisements
Similar presentations
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Advertisements

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.
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,
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.
 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.
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.
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.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
1 L44 Introduction to Java Applets. 2 OBJECTIVES  To differentiate between applets and applications.  To observe some of Java's exciting capabilities.
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.
©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.
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.
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.
A Simple Applet.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
©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.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
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.
Internet Software Development Applets Paul J Krause.
JAPPLET.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010.
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 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.
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.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
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.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
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.
APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
25/2/00SEM107 - © Kamin & ReddyClass 9 - HTML - 1 Class 9 - HTML r Creating a personal web page r Structure of HTML documents r HTML tags r The APPLET.
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.
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.
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.
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
 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.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
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.
Object Oriented Programming
Applets In Java Visit for more Learning Resources 1.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Java Applets.
Java applets 1/3/2019.
Applet in Java.
Handout-14 Applets and Graphics
Enabling Application Delivery Via the Web
APPLET PROGRAMMING.
Presentation transcript:

Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 9

Rina Overview Html Applet Graphics

Rina Java and Applet The reason people are excited about Java as more than just another OOP language is because it allows them to write interactive applets on the web.

Rina Web Browser In the browser environment the browser acts as an intermediate between the program and the operating system. The JVM resides inside the browser. The program can be simpler. The program has to work in a graphical mode. The program is called “Applet” (a small application).

Rina Applets The program has to import two packages: applet.Applet awt The program (the class) has to extend the Applet class, using the following syntax: public class My extends Applet { // your code }

Rina Basic applet code The final code looks like this: import java.awt.*; import java.applet.*; public class My extends Applet { // your code }

Rina Stages of Writing and Executing Create a java file. Compile this file in the usual way and create.class file. Applet can’t be run directly. Applet has to be run in Browser and by the Browser. Now you need to create an HTML file that will include your applet.

Rina HelloWorld import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); }

Rina Call to applet The call to applet consist of: –the command APPLET –the name of the applet class –the dimensions of the panel in which the applet will run Web browser will run your applet.

Rina Example Assuming the name of the applet class is HelloWorld.class the call to applet looks like this: <APPLET CODE = "HelloWorld.class" WIDTH = 150 HEIGHT= 50>

Rina HTML The call to applet has to be in HTML file. The file can look as follows: Java applet test page <APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=50>

Rina Several worlds about HTML HTML is the HyperText Markup Language. HTML files are text files featuring semantically tagged elements. HTML filenames are suffixed with.htm or.html. Elements that are enclosed in angle brackets like,, and are called tags. Tags are case insensitive. means the same thing as as.

Rina Several worlds about HTML Most tags are matched with closing tags, and affect the text contained between them. The closing tag syntax: Some tags have attributes. An attribute is a name, followed by an = sign and the value. To learn more about HTML: HTML TutorialHTML Tutorial

Rina Execution Assuming the file is called HelloWorld.html 1.The stages common to all, described above. 2.Open the file HelloWorld.html in the Browser. 3.The result is: HelloWorld.htmlHelloWorld.html

Rina 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 another application....The Applet class provides a standard interface between applets and their environment." Additional 4 definitions of the Applet: –A small application –A secure program that runs inside a web browser –A subclass of java.applet.Applet –An instance of a subclass of java.applet.Applet

Rina Applet hierarchy Every applet is implemented by creating a subclass of the Applet class. java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet

Rina Applet import declarations The java.applet. and java.awt. prefixes tell the compiler which packages it should search for the Applet and Graphics classes. The java.applet package contains classes that are essential to Java applets. The java.awt package contains the most frequently used classes in the Abstract Window Toolkit (AWT), which provides the Java graphical user interface (GUI).

Rina HelloWorld import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } use predefined classes Inherits all functionality of the Applet the main function of our applet an instance of the predefined painter Graphics

Rina Applet tag references a source file that is not part of the HTML page. This reference is code=“FileName.class” But if your applet code resides somewhere other than the same directory as the page it lives on you use codebase attribute to specify its location. The codebase attribute is a URL that points at the directory where the.class file is. The code attribute is the name of the.class file itself.

Rina Applet tag Browser would try to retrieve the applet from regardless of where the HTML page was. The formula for retrieving the applet from the URL: codebase + "/" + code.

Rina Applet Lifecycle The applet can go through a series of stages in it's lifecycle: –initial loading –starting to perform –suspending (e.g. when the focus shifts to another page) –destruction of the applet (closing the page) For each stage there is a method called: –init() –start() –stop() –destroy()

Rina Applet Lifecycle The methods are two symmetrical pairs: –init - destroy –start - stop In some cases we will wish to override the methods. LifeCycle.html example.LifeCycle.html

Rina LifeCycle import java.awt.*; import java.applet.*; public class LifeCycle extends Applet{ StringBuffer message; public void init() { message = new StringBuffer(); message.append("Initialised..."); } public void start() { message.append("Started..."); } public void paint(Graphics g) { g.drawString(message.toString(), 0, 25); } }

Rina Applet Lifecycle MethodWhen CalledHow manyDescription initupon loading the applet onceinitializations,resource allocation,operations needed once for the lifetime of the applet startafter the init, and each time user returns to the page any number of times updating, calls method update(), which calls paint() to update the screen output, awakens suspended operations stopeach time user leaves the page any number of times suspension of operation destroyupon closing the applet (the page) oncedestruction, release of resources

Rina Finding an Applet's Size import java.applet.*; import java.awt.*; public class SizeApplet extends Applet { public void paint(Graphics g) { Dimension appletSize = this.getSize(); int appletHeight = appletSize.height; int appletWidth = appletSize.width; g.drawString("This applet is " + appletHeight + " pixels high by " + appletWidth + " pixels wide.", 15, appletHeight/2); } }

Rina Applet's Size Retrieving the applet size is straightforward with the getSize() method. java.applet.Applet inherits this method from java.awt.Component. getSize() returns a java.awt.Dimension object. A Dimension object has two public int fields, height and width. The output file SizeApplet.htmlSizeApplet.html

Rina Passing Parameters to Applets Parameters are passed to applets in name=value pairs in tags between the opening and closing APPLET tags. Inside the applet, you read the values passed through the PARAM tags with the getParameter() method of the java.applet.Applet class.

Rina DrawStringApplet import java.applet.*; import java.awt.*; public class DrawStringApplet extends Applet { private String defaultMessage = "Hello!"; public void paint(Graphics g) { String inputFromPage = this.getParameter("Message"); if (inputFromPage == null) inputFromPage = defaultMessage; g.drawString(inputFromPage, 50, 25); } }

Rina PARAM An applet is not limited to one PARAM. You can pass as many named PARAMs to an applet as you like. An applet does not necessarily need to use all the PARAMs that are in the HTML. The resulting output DrawStringApplet.html. DrawStringApplet.html

Rina What applet can do? An applet can: –Draw pictures on a web page –Create a new window and draw in it. –Play sounds. –Receive input from the user through the keyboard or the mouse. –Make a network connection to the server from which it came and can send to and receive arbitrary data from that server.

Rina What applet can’t do? Write data on any of the host's disks. Read any data from the host's disks without the user's permission. Delete files Read from or write to arbitrary blocks of memory, even on a non-memory-protected operating system like the MacOS. All memory access is strictly controlled.

Rina What applet can’t do? Make a network connection to a host on the Internet other than the one from which it was downloaded. Call the native API directly (though Java API calls may eventually lead back to native API calls). Introduce a virus or trojan horse into the host system. An applet is not supposed to be able to crash the host system. However in practice Java isn't quite stable enough to make this claim yet.

Rina Drawing Shapes Let's explore some of the methods of the Graphics class that draw shapes in more detail. A shape can be filled or unfilled, depending on which method is invoked. The method parameters specify coordinates and sizes. Java coordinate system has the origin in the upper left corner. Many shapes with curves, like an oval, are drawn by specifying its bounding box.

Rina Graphics class In Java all drawing takes place via a Graphics object. The Graphics class provide methods for drawing a variety of graphical shapes: –lines –rectangles –ovals –circles –polygons

Rina Drawing a Line X Y g.drawLine (10, 20, 150, 45); g.drawLine (150, 45, 10, 20); or

Rina Draw Line Drawing straight lines with Java is easy. Syntax: g.drawLine(xStart, yStart, xEnd, yEnd); where (xStart, yStart) and (xEnd, yEnd) are the endpoints of your lines and g is the Graphics object you're drawing with. DrawLine.html example.DrawLine.html

Rina DrawLine example import java.awt.*; import java.applet.*; public class DrawLine extends Applet { public void paint(Graphics g){ g.drawLine(50,20,50,150); g.drawLine(20,150,200,150); g.drawLine(0,0,200,200); } }

Rina Drawing a Rectangle X Y g.drawRect (50, 20, 100, 40);

Rina Drawing Rectangles Three different types of rectangles can be drawn: –ordinary rectangles –rounded corners rectangles –three-dimensional rectangles with shaded border Ordinary rectangles and rounded corners rectangles can be drawn in outline or filled with color form.

Rina Rectangles Ordinary rectangles g.drawRect(xStart, yStart, width, height); Rounded corners rectangles g.drawRoundRect(xStart, yStart, width, height, arcWidth, arcHeight); Three-dimensional rectangles with shaded border g.draw3DRect(xStart, yStart, width, height, raised); where fifth argument - raised - is boolean argument: true value means rectangle is raised, false value means rectangle is indented.

Rina Rectangle example import java.awt.*; import java.applet.*; public class Rectangles extends Applet { public void paint(Graphics g) { g.drawRect(20,20,50,50); g.drawRoundRect(120,20,120,80,20,30); g.draw3DRect(20, 120, 50, 75, true); }

Rina Filling Rectangles The drawRect() method draws an open rectangle, a box if you prefer. If you want to draw a filled rectangle, use the fill…() method. First you need to specify rectangle color g.setColor(Color) and then use one of the: g.fillRect(xStart, yStart, width, height); g.fillRoundRect(xStart, yStart, width, height, arcWidth, arcHeight); g.fill3DRect(xStart, yStart, width, height, raised); FillRectangles.html example.FillRectangles.html

Rina Filling Rectangles import java.awt.*; import java.applet.*; public class FillRectangles extends Applet { public void paint(Graphics g) { g.setColor(Color.blue); g.fillRect(20,20,50,50); g.setColor(Color.red); g.fillRoundRect(120,20,50,50,20,30); g.setColor(Color.lightGray); g.draw3DRect(20, 100, 50, 50, true); g.draw3DRect(120, 100, 50, 50, false); g.fill3DRect(80, 180, 50, 50, false); } }

Rina Color Color is a class in the AWT. Individual colors like red or blue are instances of java.awt.Color: Color.red Color.blue You create new colors using the same RGB triples. Syntax Color pureRed = new Color(255, 0, 0) ; Pure white is Color(255, 255, 255). Values of first(red), second(green) and third (blue) arguments can move from

Rina Clear Rectangle It is also possible to clear a rectangle that you've drawn. Syntax is exactly what you'd expect: public abstract void clearRect(int x, int y, int width, int height) Blink.html uses clearRect() to blink a rectangle on the screen.Blink.html

Rina Blink import java.applet.*; import java.awt.*; public class Blink extends Applet { public void paint(Graphics g) { int appletHeight = this.getSize().height; int appletWidth = this.getSize().width; int rheight=appletHeight/3; int rwidth=appletWidth/3; int rectTop = (appletHeight - rectHeight)/2; int rectLeft = (appletWidth - rectWidth)/2; for (int i=0; i < 1000; i++) { g.fillRect(rectLeft, rectTop, rwidth-1, rheight-1); g.clearRect(rectLeft, rectTop, rectWidth-1, rectHeight-1); } }}

Rina Drawing an Oval X Y g.drawOval (175, 20, 50, 80); boundingrectangle

Rina Ovals Java has methods to draw outlined and filled ovals. g.drawOval(xStart, yStart, width, height); g.fillOval(xStart, yStart, width, height); where width and height are “bounded box” properties. The oval is drawn as large as it can be to touch the “bounded box”’s edges at their centers. Ovals.html example.Ovals.html

Rina Oval example import java.awt.*; import java.applet.Applet; public class Ovals extends Applet { public void paint(Graphics g) { int left = 5; int top = 5; // method that outputs series of ovals drawFilledOvals(g, left, top, this.size().width-10, this.size().height-10); }

Rina Oval example public void drawFilledOvals(Graphics g, int x, int y, int w, int h) { g.setColor(Color.blue); while (h > 0) { g.fillOval(x, y, w, h); x += 10; y += 10; w -= 20; h -= 20; if (g.getColor() == Color.white) g.setColor(Color.blue); else g.setColor(Color.white); } } }//end of class

Rina Any Questions?