24 First Applet

download 24 First Applet

of 25

Transcript of 24 First Applet

  • 7/31/2019 24 First Applet

    1/25

    1

    A Simple Applet

  • 7/31/2019 24 First Applet

    2/25

    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 modern Java applets, old browsers need an up-to-date

    Java plugin appletviewer is a program that can run

    An application is a Java program that runs all by itself

  • 7/31/2019 24 First Applet

    3/25

  • 7/31/2019 24 First Applet

    4/25

    4

    The Applet class

    To create an applet, you must import the Applet class

    This class is in the java.applet package

    TheAppletclass contains code that works with a

    browser to create a display window Capitalization matters!

    applet and Applet are different names

  • 7/31/2019 24 First Applet

    5/25

    5

    Importing the Applet class

    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

  • 7/31/2019 24 First Applet

    6/25

    6

    Thejava.awtpackage

    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 isprobably involved!

  • 7/31/2019 24 First Applet

    7/257

    Importing the java.awt package

    Since you may want to use many classes from the

    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

  • 7/31/2019 24 First Applet

    8/258

    C and C++ programmers only

    C and C++ have an #include directive that copies a

    library function into your program

    This makes your program bigger

    Javas import gives you access to the library

    It does notmake your program bigger

    Its OK to use lots ofinclude directives!

  • 7/31/2019 24 First Applet

    9/259

    The applet so far

    import java.applet.Applet;

    import java.awt.*;

  • 7/31/2019 24 First Applet

    10/2510

    Your applet class

    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 kind ofApplet, but with added capabilities Javas Applet just makes an empty window

    We are going to draw in that window The only way to make an applet is to extend Applet

  • 7/31/2019 24 First Applet

    11/2511

    The applet so far

    import java.applet.Applet;

    import java.awt.*;

    // CIT 591 example

    public class Drawing extends Applet {

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

    }

  • 7/31/2019 24 First Applet

    12/25

  • 7/31/2019 24 First Applet

    13/2513

    The paint method, part 2

    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 youhave painted)

  • 7/31/2019 24 First Applet

    14/25

  • 7/31/2019 24 First Applet

    15/2515

    Colors

    The java.awtpackage defines a class named Color

    There are 13 predefined colorshere are their fully-qualified names:

    For compatibility with older programs (before the naming

    conventions were established), Java also 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

  • 7/31/2019 24 First Applet

    16/2516

    New colors

    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)

  • 7/31/2019 24 First Applet

    17/2517

    Setting a color

    To use a color, we tell our Graphicsgwhat color we

    want:

    g.setColor(Color.RED);

    g will remember this color and use it for everythinguntil we tell it some different color

  • 7/31/2019 24 First Applet

    18/2518

    The paint method so far

    public void paint(Graphics g) {

    g.setColor(Color.BLUE);

    draw a rectangle

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

    }

    }

  • 7/31/2019 24 First Applet

    19/2519

    Pixels

    A pixel is a picture (pix) element

    one pixel is one dot on your screen

    there are typically 72 to 90 pixels per inch

    java.awt measures everything in pixels

  • 7/31/2019 24 First Applet

    20/2520

    Javas coordinate system

    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 right corner, where w

    is the width of the window and h is its height

    (0, 0)

    (0, 20)

    (50, 0)

    (50, 20)

    (w-1, h-1)

  • 7/31/2019 24 First Applet

    21/25

    21

    Drawing rectangles

    There are two ways to draw rectangles:

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

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

  • 7/31/2019 24 First Applet

    22/25

    22

    The complete applet

    import java.applet.Applet;

    import java.awt.*;

    // CIT 591 example

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

    }

    }

  • 7/31/2019 24 First Applet

    23/25

    23

    Some more java.awt methods

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

  • 7/31/2019 24 First Applet

    24/25

    24

    The HTML page

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

    DrawingApplet Applet

    BlueJ will create this HTML for you

  • 7/31/2019 24 First Applet

    25/25