java applet

45

Upload: nyabs-yohana

Post on 16-Oct-2015

49 views

Category:

Documents


0 download

DESCRIPTION

Applets

TRANSCRIPT

  • applet

    An applet is a Java program that runs in a Web

    browser.

    An applet is typically embedded inside a web

    page and runs in the context of a browser.

    The Applet class provides the standard interface

    between the applet and the browser environment.

  • Difference between applets and java

    application.

    There are some important differences between an

    applet and a standalone Java application,

    including the following:

    An applet is a Java class that extends the java.applet.Applet class.

    A main() method is not invoked on an applet, and an applet class will

    not define main().

    Applets are designed to be embedded within an HTML page.

    When a user views an HTML page that contains an applet, the code for

    the applet is downloaded to the user's machine.

    A JVM is required to view an applet. The JVM can be either a plug-in of

    the Web browser or a separate runtime environment.

  • Java applet scenario.

  • Applets cont

    An applet program is a written as a inheritance of thejava.Appletclass

    There is no main()method in an Applet.

    The JVM on the user's machine creates an instance of the applet class and

    invokes various methods during the applet's lifetime.

    Because Java applets run inside the Java browser, they have access to the

    same capabilities that the browser has: sophisticated graphics, drawing,

    and image processing packages; user interface elements; networking; and

    event handling.

  • Life Cycle of an Applet

    Four methods in the Applet class give you the framework on which you build any serious applet:

    init: This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.

    start: This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.

    stop: This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.

    destroy: This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.

    paint: Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt

  • Every Applet needs to implement one or more of

    the init(), the start( ) and the paint( ) methods.

    At the end of the execution, the stop( ) method is

    invoked, followed by the destroy( ) method to

    deallocate the applets resources.

  • Applet life cycle

    browser visits page containing an applet

    browser calls init on that applet, once

    browser calls start on that applet

    browser goes away from that page

    browser calls stop on that applet

    browser comes back to that page

    browser calls start again on that applet

    browser shuts down

    browser calls destroy on the applet, once

  • The Applet CLASS:

    //for graphics

  • View applet (Java Virtual Machine)

    In order to view the output of applet we use either

    Appletviewer

    browser

    The coordinate system

    The applet is drawn in a rectangle, which consists of

    pixels.

  • Example.

    import java.applet.Applet;

    import java.awt.Color;

    import java.awt.Font;

    import java.awt.Graphics;

    public class qw extends Applet {

    Font f = new Font("TimesRoman",Font.BOLD,36);

    public void paint(Graphics g) {

    g.setFont(f);

    g.setColor(Color.red);

    g.drawString("Hello again!", 5, 50);

    }

    }

  • Including an Applet on a Web Page

    After you create a class or classes that contain your applet

    and compile them into class files as you would any other Java

    program, you have to create a Web page that will hold that

    applet by using the HTML language.

    There is a special HTML tag for including applets in Web

    pages; Java-capable browsers use the information contained

    in that tag to locate the compiled class files and execute the

    applet itself.

  • Applet tags

  • The Hello, World Applet

    If your browser was Java-enabled, a "Hello, World"

    message would appear here.

  • Getting Applet Parameters:

    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.

    So, you can pass the parameters from your html page to the

    applet embedded in your page. Theparam tag() is used to pass the parameters

    to an apple

  • Passing Parameter in Java Applet

    This is the applet:

  • APPLET

    import java.applet.*;

    import java.awt.*;public class appletParameter extends Applet {private String strDefault = "Hello! Java Applet.";public void paint(Graphics g) {String strParameter = this.getParameter("Message");if (strParameter == null)strParameter = strDefault;g.drawString(strParameter, 50, 25);}

    }

  • EXAMPLE:

    import java.awt.Graphics;

    import java.awt.Font;

    import java.awt.Color;

    public class MoreHelloApplet extends java.applet.Applet {

    Font f = new Font(TimesRoman,Font.BOLD,36);

    String name;

    public void init() {

    this.name = getParameter(name);

    if (this.name == null)

    this.name = Laura;

    this.name = Hello + this.name + !;

    }

    public void paint(Graphics g) {

    g.setFont(f);

    g.setColor(Color.red);

    g.drawString(this.name, 5, 50); }}

  • The HTML file

    Hello!

    Hello to whoever you are!

  • JAVA PROGRAMMING

    Graphics, Fonts, and Color

  • The Graphics Class

    With Javas graphics capabilities, you can draw lines, shapes, characters, and images to the screen inside your applet. Most of the graphics operations in Java are methods defined in the Graphics class. You dont have to create an instance of Graphics in order to draw something in your applet; in your applets paint() method , you are given a Graphics object. By drawing on that object, you draw onto your applet and the results appear on screen

    The Graphics class is part of the java.awt package, so if your applet does any painting (as it usually will), make sure you import that class at the beginning of your Java file:

    import java.awt.Graphics;

    public class MyClass extended java.applet.Applet {

    ... }

  • Drawing and Filling

    Lines

    package q;

    import java.applet.Applet;

    import java.awt.Graphics;

    public class qw extends Applet {

    public void paint(Graphics g) {

    g.drawLine(25,25,75,75);

    }

    }

  • Drawing and Filling Cont

    Plain rectangles.

    package q;

    import java.applet.Applet;

    import java.awt.Graphics;

    public class qw extends Applet {

    public void paint(Graphics g) {

    g.drawRect(20,20,60,60);

    g.fillRect(120,20,60,60);

    }

    }

    To draw a plain rectangle, use either the drawRect or fillRectmethods. Both take four arguments: the x and y coordinates of the top left corner of the rectangle, and the width and height of the rectangle to draw.

  • Drawing and Filling Cont

    Rounded rectangles, which are rectangles with rounded corners

    import java.applet.Applet;

    import java.awt.Graphics;

    public class qw extends Applet {

    public void paint(Graphics g) {

    g.drawRoundRect(20,20,60,60,10,10);

    g.fillRoundRect(120,20,60,60,20,20);

    }}

    Heres a paint method that draws two rounded rectangles: one as an outline with a rounded corner 10 pixels square; the other, filled, with a rounded corner 20 pixels square

  • Polygons

    Polygons are shapes with an unlimited number of sides. To

    draw a polygon, you need a set of x and y coordinates, and

    the drawing method then starts at one, draws a line to the

    second, then a line to the third, and so on.

    Using the first method, the drawPolygon and fillPolygon

    methods take three arguments:

    An array of integers representing x coordinates

    An array of integers representing y coordinates

    An integer for the total number of points

  • Polygon drawing.

    import java.applet.Applet;

    import java.awt.Graphics;

    public class qw extends Applet {

    public void paint(Graphics g) {

    int exes[] = { 39,94,97,142,53,58,26 };

    int whys[] = { 33,74,36,70,108,80,106 };

    int pts = exes.length;

    g.drawPolygon(exes,whys,pts);

    }}

  • Ovals

    Use ovals to draw ellipses or circles. Ovals are just like rectangles with

    overly rounded corners.

    In fact, you draw them using the same four arguments: the x and y of the

    top corner, and the width and height of the oval itself. Note that, because

    youre drawing an oval, the starting point is some distance to the left and

    up from the actual outline of the oval itself.

    As with the other drawing operations, the drawOval method draws an

    outline of an oval, and the fillOval method draws a filled oval.

    Heres an example of two ovals, a circle and an ellipse

  • Example.

    import java.applet.Applet;

    import java.awt.Graphics;

    public class qw extends Applet {

    public void paint(Graphics g) {

    g.drawOval(20,20,60,60);

    g.fillOval(120,20,100,60);

    }}

  • Arc

    An arc is a part of a oval; in fact, the easiest way to think of an arc is as a

    section of a complete oval.

    The drawArc method takes six arguments: the starting corner, the width and

    height, the angle at which to start the arc, and the degrees to draw it

    before stopping

    Once again, there is a drawArc method to draw the arcs outline and the

    fillArc to fill the arc. Filled arcs are drawn as if they were sections of a pie;

    instead of joining the two endpoints, both endpoints are joined to the center

    of the circle.

  • Arc example.

    import java.applet.Applet;

    import java.awt.Graphics;

    public class qw extends Applet {

    public void paint(Graphics g) {

    g.drawArc(100,20,60,60,45,180);

    g.fillArc(120,20,60,60,90,180);

    }}

  • Arc example.

    import java.applet.Applet;

    import java.awt.Graphics;

    public class qw extends Applet {

    public void paint(Graphics g) {

    g.drawArc(10,20,150,50,25,-130);

    g.fillArc(10,80,150,50,25,-130);

    }}

  • Copying and Clearing

    The copyArea method copies a rectangular area of the screen to

    another area of the screen.

    copyArea takes six arguments: the x and y of the top corner of the

    rectangle to copy, the width and the height of that rectangle, and

    the distance in the x and y directions to which to copy it.

    For example, this line copies a square area 100 pixels on a side

    100 pixels directly to its right:

    g.copyArea(0,0,100,100,100,0);

  • Example.

    import java.applet.Applet;

    import java.awt.*;

    public class as extends Applet {

    public void paint(Graphics g){

    g.drawRect(20,20,60,60);

    g.copyArea(0, 0, 100, 100, 200, 0);

    }

    }

  • Clearing

    To clear a rectangular area, use the clearRect method.

    clearRect, which takes the same four arguments as the

    drawRect and fillRect methods, fills the given rectangle with the

    current background color of the applet

    To clear the entire applet, you can use the size() method, which

    returns a Dimension object representing the width and height

    of the applet. You can then get to the actual values for width

    and height by using the width and height instance variables:

  • Example.

    import java.applet.Applet;

    import java.awt.*;

    public class as extends Applet {

    public void paint(Graphics g){

    g.drawRect(20,20,60,60);

    g.copyArea(0, 0, 100, 100, 200, 0);

    g.clearRect(0, 0, 50, 100);

    }

    }

  • Drawing Characters and Strings

    With a font object in hand, you can draw text on the screen using the methods

    drawChars and drawString. First, though, you need to set the current font to

    your font object using the setFont method.

    public void paint(Graphics g) {

    Font f = new Font(TimesRoman, Font.PLAIN,72);

    g.setFont(f);

    g.drawString(This is a big font.,10,100);

    }

  • Example.

    import java.applet.Applet;

    import java.awt.*;

    public class as extends Applet {

    public void paint(Graphics g){

    Font f = new Font("TimesRoman", Font.PLAIN, 18);

    Font fb = new Font("TimesRoman", Font.BOLD, 18);

    Font fi = new Font("TimesRoman", Font.ITALIC, 18);

    Font fbi = new Font("TimesRoman", Font.BOLD + Font.ITALIC, 18);

    g.setFont(f);

    g.drawString("This is a plain font", 10, 25);

    g.setFont(fb);

    g.drawString("This is a bold font ", 10, 50);

    g.setFont(fi);

    g.drawString("This is an italic font ", 10, 75);

    g.setFont(fbi);

    g.drawString("This is a bold italic font ", 10, 100);

    }

    }

  • Color

    Drawing black lines and tests on a gray background is all very

    nice, but being able to use different colors is much nicer. Java

    provides methods and behaviors for dealing with color in

    general through the Color class, and also provides methods for

    setting the current foreground and background colors so that

    you can draw with the colors you created

    Using Color Objects

  • Using Color Objects

    To draw an object in a particular color, you must create an instance of the

    Color class to represent that color. The Color class defines a set of standard

    color objects, stored in class variables, that enable you quickly to get a

    color object for some of the more popular colors.

  • Testing and Setting the Current Colors

    To draw an object or text using a color object, you have to set the current color to be that color object, just as you have to set the current font to the font in which you want to draw.

    Use the setColor method (a method for Graphics objects) to do this:

    g.setColor(Color.green);

    After setting the current color, all drawing operations will occur in that color

    The setBackground method sets the background color of the applet, which is usually a dark grey. It takes a single argument, a color object:

    setBackground(Color.white);

  • Example.

    import java.applet.Applet;

    import java.awt.*;

    public class as extends Applet {

    public void paint(Graphics g){

    g.setColor(Color.GREEN);

    g.fillRect(100,200,250,250);

    }

    }

  • AWT.

    The Java Abstract Windowing Toolkit

  • AWT

    The AWT provides the following:

    A full set of UI widgets and other components, including windows, menus,

    buttons, checkboxes, text fields, scrollbars, and scrolling lists

    Support for UI containers, which can contain other embedded

    containers or UI widgets

    An event system for managing system and user events between and

    among parts of the AWT

    Mechanisms for laying out components in a way that enables platform-

    independent UI design

  • AWT Component

    These are the major components you can work with in the AWT

    Containers. Containers are generic AWT components that can contain

    other components, including other containers. The most common form of

    container is the panel, which represents a container that can be

    displayed on screen

    Canvases. A canvas is a simple drawing surface. Although you can draw

    on panels (as youve been doing all along), canvases are good for

    painting images or other graphics operations.

    UI components. These can include buttons, lists, simple popup menus,

    checkboxes, test fields, and other typical elements of a user interface.

    Window construction components. These include windows, frames, menu

    bars, and dialogs