System development with Java Instructors Rina ZvielGirshin Lecture

  • Slides: 51
Download presentation
System development with Java Instructors: Rina Zviel-Girshin Lecture 9 Rina Zviel-Girshin @ARC

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

Overview • Html • Applet • Graphics Rina Zviel-Girshin @ARC

Overview • Html • Applet • Graphics Rina Zviel-Girshin @ARC

Java and Applet • The reason people are excited about Java as more than

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 Zviel-Girshin @ARC

Web Browser • In the browser environment the browser acts as an intermediate between

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 Zviel-Girshin @ARC

Applets The program has to import two packages: • applet. Applet • awt The

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 Zviel-Girshin @ARC

Basic applet code The final code looks like this: import java. awt. *; import

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

Stages of Writing and Executing • Create a java file. • Compile this file

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 Zviel-Girshin @ARC

Hello. World import java. applet. Applet; import java. awt. Graphics; public class Hello. World

Hello. World import java. applet. Applet; import java. awt. Graphics; public class Hello. World extends Applet { public void paint(Graphics g) { g. draw. String("Hello world!", 50, 25); } } Rina Zviel-Girshin @ARC

Call to applet • The call to applet consist of: – the command APPLET

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 Zviel-Girshin @ARC

Example • Assuming the name of the applet class is Hello. World. class the

Example • Assuming the name of the applet class is Hello. World. class the call to applet looks like this: <APPLET CODE = "Hello. World. class" WIDTH = 150 HEIGHT= 50> </APPLET> Rina Zviel-Girshin @ARC

HTML The call to applet has to be in HTML file. The file can

HTML The call to applet has to be in HTML file. The file can look as follows: <HTML><HEAD> <TITLE>Java applet test page</TITLE></HEAD> <BODY> <APPLET CODE="Hello. World. class" WIDTH=150 HEIGHT=50> </APPLET> </BODY></HTML> Rina Zviel-Girshin @ARC

Several worlds about HTML • HTML is the Hyper. Text Markup Language. • HTML

Several worlds about HTML • HTML is the Hyper. Text 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 <html>, <head>, and <title> are called tags. • Tags are case insensitive. <html> means the same thing as <HTML> as <Html> <Ht. Ml>. Rina Zviel-Girshin @ARC

Several worlds about HTML • Most tags are matched with closing tags, and affect

Several worlds about HTML • Most tags are matched with closing tags, and affect the text contained between them. • The closing tag syntax: </tag. Name> • Some tags have attributes. • An attribute is a name, followed by an = sign and the value. <applet code=“File. Name. class” width=160> </applet> • To learn more about HTML: HTML Tutorial Rina Zviel-Girshin @ARC

Execution Assuming the file is called Hello. World. html 1. The stages common to

Execution Assuming the file is called Hello. World. html 1. The stages common to all, described above. 2. Open the file Hello. World. html in the Browser. 3. The result is: Hello. World. html Rina Zviel-Girshin @ARC

What is an applet • According to Sun "An applet is a small program

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 Zviel-Girshin @ARC

Applet hierarchy Every applet is implemented by creating a subclass of the Applet class.

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 Zviel-Girshin @ARC

Applet import declarations • The java. applet. and java. awt. prefixes tell the compiler

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 Zviel-Girshin @ARC

Hello. World use predefined classes import java. applet. Applet; import java. awt. Graphics; Inherits

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

Applet tag • <APPLET> references a source file that is not part of the

Applet tag • <APPLET> references a source file that is not part of the HTML page. • This reference is code=“File. Name. 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 Zviel-Girshin @ARC

Applet tag <APPLET CODE="Hello. World. class" CODEBASE="http: //www. yarden. ac. il/ip 2" WIDTH=200 HEIGHT=200>

Applet tag <APPLET CODE="Hello. World. class" CODEBASE="http: //www. yarden. ac. il/ip 2" WIDTH=200 HEIGHT=200> </APPLET> • Browser would try to retrieve the applet from http: //www. yarden. ac. il/ip 2/Hello. World. class regardless of where the HTML page was. • The formula for retrieving the applet from the URL: codebase + "/" + code. Rina Zviel-Girshin @ARC

Applet Lifecycle • The applet can go through a series of stages in it's

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 Zviel-Girshin @ARC

Applet Lifecycle • The methods are two symmetrical pairs: – init - destroy –

Applet Lifecycle • The methods are two symmetrical pairs: – init - destroy – start - stop • In some cases we will wish to override the methods. • Life. Cycle. html example. Rina Zviel-Girshin @ARC

Life. Cycle import java. awt. *; import java. applet. *; public class Life. Cycle

Life. Cycle import java. awt. *; import java. applet. *; public class Life. Cycle extends Applet{ String. Buffer message; public void init() { message = new String. Buffer(); message. append("Initialised. . . "); } public void start() { message. append("Started. . . "); } public void paint(Graphics g) { g. draw. String(message. to. String(), 0, 25); } Rina Zviel-Girshin @ARC }

Applet Lifecycle Method When Called How many Description init upon loading the applet once

Applet Lifecycle Method When Called How many Description init upon loading the applet once initializations, resource allocation, operations needed once for the lifetime of the applet start after the init, and each time user returns to the page any number updating, calls method of times update(), which calls paint() to update the screen output, awakens suspended operations stop each time user leaves the page any number suspension of operation of times destroy upon closing the applet (the page) once Rina Zviel-Girshin @ARC destruction, release of resources

Finding an Applet's Size import java. applet. *; import java. awt. *; public class

Finding an Applet's Size import java. applet. *; import java. awt. *; public class Size. Applet extends Applet { public void paint(Graphics g) { Dimension applet. Size = this. get. Size(); int applet. Height = applet. Size. height; int applet. Width = applet. Size. width; g. draw. String("This applet is " + applet. Height + " pixels high by " + applet. Width + " pixels wide. ", 15, applet. Height/2); } } Rina Zviel-Girshin @ARC

Applet's Size • Retrieving the applet size is straightforward with the get. Size() method.

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

Passing Parameters to Applets • Parameters are passed to applets in name=value pairs in

Passing Parameters to Applets • Parameters are passed to applets in name=value pairs in <PARAM> tags between the opening and closing APPLET tags. • Inside the applet, you read the values passed through the PARAM tags with the get. Parameter() method of the java. applet. Applet class. <APPLET code="Draw. String. Applet. class" width=300 height=50> <PARAM name="Message" value=“Hello from Rina!"> </APPLET> Rina Zviel-Girshin @ARC

Draw. String. Applet import java. applet. *; import java. awt. *; public class Draw.

Draw. String. Applet import java. applet. *; import java. awt. *; public class Draw. String. Applet extends Applet { private String default. Message = "Hello!"; public void paint(Graphics g) { String input. From. Page = this. get. Parameter("Message"); if (input. From. Page == null) input. From. Page = default. Message; g. draw. String(input. From. Page, 50, 25); } Rina Zviel-Girshin @ARC }

PARAM • An applet is not limited to one PARAM. • You can pass

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 Draw. String. Applet. html. Rina Zviel-Girshin @ARC

What applet can do? • An applet can: – Draw pictures on a web

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 Zviel-Girshin @ARC

What applet can’t do? • Write data on any of the host's disks. •

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 Mac. OS. All memory access is strictly controlled. Rina Zviel-Girshin @ARC

What applet can’t do? • Make a network connection to a host on the

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 Zviel-Girshin @ARC

Drawing Shapes • Let's explore some of the methods of the Graphics class that

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 Zviel-Girshin @ARC

Graphics class • In Java all drawing takes place via a Graphics object. •

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 Zviel-Girshin @ARC

10 Drawing a Line 150 20 45 Y g. draw. Line (10, 20, 150,

10 Drawing a Line 150 20 45 Y g. draw. Line (10, 20, 150, 45); or g. draw. Line (150, 45, 10, 20); Rina Zviel-Girshin @ARC X

Draw Line • Drawing straight lines with Java is easy. • Syntax: g. draw.

Draw Line • Drawing straight lines with Java is easy. • Syntax: g. draw. Line(x. Start, y. Start, x. End, y. End); • where (x. Start, y. Start) and (x. End, y. End) are the endpoints of your lines and g is the Graphics object you're drawing with. • Draw. Line. html example. Rina Zviel-Girshin @ARC

Draw. Line example import java. awt. *; import java. applet. *; public class Draw.

Draw. Line example import java. awt. *; import java. applet. *; public class Draw. Line extends Applet { public void paint(Graphics g){ g. draw. Line(50, 20, 50, 150); g. draw. Line(20, 150, 200, 150); g. draw. Line(0, 0, 200); } } Rina Zviel-Girshin @ARC

Drawing a Rectangle 50 20 40 100 Y g. draw. Rect (50, 20, 100,

Drawing a Rectangle 50 20 40 100 Y g. draw. Rect (50, 20, 100, 40); Rina Zviel-Girshin @ARC X

Drawing Rectangles • Three different types of rectangles can be drawn: – ordinary rectangles

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 Zviel-Girshin @ARC

Rectangles • Ordinary rectangles g. draw. Rect(x. Start, y. Start, width, height); • Rounded

Rectangles • Ordinary rectangles g. draw. Rect(x. Start, y. Start, width, height); • Rounded corners rectangles g. draw. Round. Rect(x. Start, y. Start, width, height, arc. Width, arc. Height); • Three-dimensional rectangles with shaded border g. draw 3 DRect(x. Start, y. Start, width, height, raised); where fifth argument - raised - is boolean argument: true value means rectangle is raised, false value means rectangle is indented. Rina Zviel-Girshin @ARC

Rectangle example import java. awt. *; import java. applet. *; public class Rectangles extends

Rectangle example import java. awt. *; import java. applet. *; public class Rectangles extends Applet { public void paint(Graphics g) { g. draw. Rect(20, 50, 50); g. draw. Round. Rect(120, 80, 20, 30); g. draw 3 DRect(20, 120, 50, 75, true); } } Rina Zviel-Girshin @ARC

Filling Rectangles • The draw. Rect() method draws an open rectangle, a box if

Filling Rectangles • The draw. Rect() 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. set. Color(Color) and then use one of the: g. fill. Rect(x. Start, y. Start, width, height); g. fill. Round. Rect(x. Start, y. Start, width, height, arc. Width, arc. Height); g. fill 3 DRect(x. Start, y. Start, width, height, raised); • Fill. Rectangles. html example. Rina Zviel-Girshin @ARC

Filling Rectangles import java. awt. *; import java. applet. *; public class Fill. Rectangles

Filling Rectangles import java. awt. *; import java. applet. *; public class Fill. Rectangles extends Applet { public void paint(Graphics g) { g. set. Color(Color. blue); g. fill. Rect(20, 50, 50); g. set. Color(Color. red); g. fill. Round. Rect(120, 50, 20, 30); g. set. Color(Color. light. Gray); g. draw 3 DRect(20, 100, 50, true); g. draw 3 DRect(120, 100, 50, false); g. fill 3 DRect(80, 180, 50, false); } } Rina Zviel-Girshin @ARC

Color • Color is a class in the AWT. • Individual colors like red

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 pure. Red = new Color(255, 0, 0) ; • Pure white is Color(255, 255). • Values of first(red), second(green) and third (blue) arguments can move from 0 -255. Rina Zviel-Girshin @ARC

Clear Rectangle • It is also possible to clear a rectangle that you've drawn.

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

Blink import java. applet. *; import java. awt. *; public class Blink extends Applet

Blink import java. applet. *; import java. awt. *; public class Blink extends Applet { public void paint(Graphics g) { int applet. Height = this. get. Size(). height; int applet. Width = this. get. Size(). width; int rheight=applet. Height/3; int rwidth=applet. Width/3; int rect. Top = (applet. Height - rect. Height)/2; int rect. Left = (applet. Width - rect. Width)/2; for (int i=0; i < 1000; i++) { g. fill. Rect(rect. Left, rect. Top, rwidth-1, rheight-1); g. clear. Rect(rect. Left, rect. Top, rect. Width-1, rect. Height-1); } }} Rina Zviel-Girshin @ARC

Drawing an Oval 175 X 20 80 bounding rectangle 50 Y g. draw. Oval

Drawing an Oval 175 X 20 80 bounding rectangle 50 Y g. draw. Oval (175, 20, 50, 80); Rina Zviel-Girshin @ARC

Ovals • Java has methods to draw outlined and filled ovals. g. draw. Oval(x.

Ovals • Java has methods to draw outlined and filled ovals. g. draw. Oval(x. Start, y. Start, width, height); g. fill. Oval(x. Start, y. Start, 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. Rina Zviel-Girshin @ARC

Oval example import java. awt. *; import java. applet. Applet; public class Ovals extends

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 draw. Filled. Ovals(g, left, top, this. size(). width-10, this. size(). height-10); } Rina Zviel-Girshin @ARC

Oval example public void draw. Filled. Ovals(Graphics g, int x, int y, int w,

Oval example public void draw. Filled. Ovals(Graphics g, int x, int y, int w, int h) { g. set. Color(Color. blue); while (h > 0) { g. fill. Oval(x, y, w, h); x += 10; y += 10; w -= 20; h -= 20; if (g. get. Color() == Color. white) g. set. Color(Color. blue); else g. set. Color(Color. white); } } }//end of class Rina Zviel-Girshin @ARC

Any Questions? Rina Zviel-Girshin @ARC 51

Any Questions? Rina Zviel-Girshin @ARC 51