14508 Applets

Embed Size (px)

Citation preview

  • 8/2/2019 14508 Applets

    1/32

  • 8/2/2019 14508 Applets

    2/32

    2

    An applet is a Java program that runs on a webpage Applets can be run within any modern browser

    To run modern Java applets, old browsers need anup-to-date Java plugin

    appletviewer is a program that can run

    An application is a Java program that runs all byitself

  • 8/2/2019 14508 Applets

    3/32

    3

    Java supplies a huge library of pre-writtencode, ready for you to use in yourprograms

    Code is organized into classes Classes are grouped into packages

    One way to use this code is to import it

    You can import a single class, or all the

    classes in a package

  • 8/2/2019 14508 Applets

    4/32

    4

    To create an applet, you must import the Appletclass This class is in the java.applet package

    TheAppletclass contains code that works with abrowser to create a display window

    Capitalization matters! applet and Applet are different names

  • 8/2/2019 14508 Applets

    5/32

    5

    Here is the directive that you need:

    import java.applet.Applet;

    import is a keyword

    java.applet is the name of the package A dot ( .) separates the package from the

    class

    Applet is the name of the class

    There is a semicolon ( ; ) at the end

  • 8/2/2019 14508 Applets

    6/32

    6

    awt stands for Abstract Window Toolkit

    The java.awt package includes classes for: Drawing lines and shapes

    Drawing letters Setting colors

    Choosing fonts

    If its drawn on the screen, then java.awt is

    probably involved!

  • 8/2/2019 14508 Applets

    7/327

    Since you may want to use many classes fromthe 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

  • 8/2/2019 14508 Applets

    8/328

    import java.applet.Applet;

    import java.awt.*;

  • 8/2/2019 14508 Applets

    9/329

    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 kindofApplet, but with added capabilities Javas Applet just makes an empty window We are going to draw in that window

    The onlyway to make an applet is to extendApplet

  • 8/2/2019 14508 Applets

    10/3210

    import java.applet.Applet;

    import java.awt.*;

    public class Drawing extends Applet {

    we still need to put some code in here...

    }

  • 8/2/2019 14508 Applets

    11/3211

    Our applet is going to have a method to paintsome colored rectangles on the screen

    This method must be named paint

    paint needs to be told where on the screen itcan draw This will be the only parameter it needs

    paintdoesnt return any result

  • 8/2/2019 14508 Applets

    12/32

    12

    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 doesnt remember what

    you have painted)

  • 8/2/2019 14508 Applets

    13/32

    13

    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}

    }

  • 8/2/2019 14508 Applets

    14/32

    14

    The java.awtpackage defines a class named Color There are 13 predefined colorshere are their

    fully-qualified names:

    For compatibility with older programs (beforethe naming conventions were established), Javaalso allows color names in lowercase: Color.black,Color.darkGray, etc.

    Color.BLACK Color.PINK Color.GREENColor.DARK_GRAY Color.RED Color.CYAN

    Color.GRAY Color.ORANGE Color.BLUE

    Color.LIGHT_GRAY Color.YELLOW

    Color.WHITE Color.MAGENTA

  • 8/2/2019 14508 Applets

    15/32

    15

    Every color is a mix of red, green, and blue

    You can make your own colors:new Color(red,green,blue)

    Amounts range from 0 to 255 Black is (0, 0, 0), white is (255, 255, 255)

    We are mixing lights, not pigments

    Yellow is red + green, or (255, 255, 0)

  • 8/2/2019 14508 Applets

    16/32

    16

    To use a color, we tell our Graphicsgwhat colorwe want:

    g.setColor(Color.RED);

    g will remember this color and use it foreverything until we tell it some different color

  • 8/2/2019 14508 Applets

    17/32

    17

    public void paint(Graphics g) {

    g.setColor(Color.BLUE);

    draw a rectangle

    g.setColor(Color.RED);draw another rectangle

    }

    }

  • 8/2/2019 14508 Applets

    18/32

    18

    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 rightcorner, where w is the width of the window andh is its height

    (0, 0)

    (0, 20)

    (50, 0)

    (50, 20)

    (w-1, h-1)

  • 8/2/2019 14508 Applets

    19/32

    19

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

    g.fillRect(left,top,width,height);

  • 8/2/2019 14508 Applets

    20/32

    20

    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);

    }

    }

  • 8/2/2019 14508 Applets

    21/32

    21

    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);

  • 8/2/2019 14508 Applets

    22/32

    22

    You can only run an applet in an HTML page The HTML looks something like this:

    DrawingApplet Applet

  • 8/2/2019 14508 Applets

    23/32

    import java.applet.Applet;

    public class TrivialApplet extends Applet { }

  • 8/2/2019 14508 Applets

    24/32

    import java.awt.*;

    import java.applet.Applet;

    public class HelloWorld extends Applet {

    public void paint( Graphics g ) {

    g.drawString( "Hello World!", 30, 30 );

    }

    }

  • 8/2/2019 14508 Applets

    25/32

    25

    Running the applet Compile

    javac HelloWorld.java

    If no errors, bytecodes stored in HelloWorld.class

    Create an HTML file Loads the applet into appletviewer or a browser

    Ends in .htm or .html

    To execute an applet

    Create an HTML file indicating which applet thebrowser (or appletviewer) should load and execute

  • 8/2/2019 14508 Applets

    26/32

    26

    Simple HTML file (HelloWorld.html) Usually in same directory as .class file

    Remember, .class file created after compilation

    HTML codes (tags) Usually come in pairs

    Begin with < and end with >

    Lines 1 and 4 - begin and end the HTML tags

    Line 2 - begins tag Specifies code to use for applet

    Specifies width and height of display area in pixels

    Line 3 - ends tag

    1 2

    3 4

  • 8/2/2019 14508 Applets

    27/32

    27

    appletviewer only understands tags

    Ignores everything else

    Minimal browser Executing the applet

    appletviewer HelloWorld.html

    Perform in directory containing .class file

    1 2

    3 4

  • 8/2/2019 14508 Applets

    28/32

    28

    destroy ()

    init()

    start()

    stop()

    java.applet.Applet

    java.awt.Panel

  • 8/2/2019 14508 Applets

    29/32

    29

    init()

    Called exactly once in an applets life.

    Called when applet is first loaded, which is

    after object creation, e.g., when the browservisits the web page for the first time.

    Used to read applet parameters, startdownloading any other images or media files,

    etc.

  • 8/2/2019 14508 Applets

    30/32

    30

    start()

    Called at least once.

    Called when an applet is started or restarted,

    i.e., whenever the browser visits the webpage.

    stop()

    Called at least once.

    Called when the browser leaves the webpage.

  • 8/2/2019 14508 Applets

    31/32

    31

    destroy()

    Called exactly once.

    Called when the browser unloads the applet.

    Used to perform any final clean-up.

    init

    destroystart stop

    start

  • 8/2/2019 14508 Applets

    32/32

    import java.awt.*;import java.applet.Applet;

    public class ab extends Applet{

    public void paint(Graphics g){int a=10, b=20;

    int d=a+b;

    String s= sum:+String.valueOf(d);

    g.drawString(s,100,100);}

    }