03 Applets

Embed Size (px)

Citation preview

  • 8/3/2019 03 Applets

    1/26

  • 8/3/2019 03 Applets

    2/26

    2

    Applets and applications

    An application is an ordinary program

    Examples: Notepad, MS Word, Firefox, Halo, etc.

    An applet is a Java program that runs within anotherprogram (usually a browser)

    Applets can be run within any browser

    To run Java applets, browsers need an up-to-date Javaplugin

    appletviewer is a program that can run applets

    When you download the Java SDK, appletviewer comes with it

    appletviewer is always up-to-date with your Java system

    Eclipse has an built-in applet viewer

  • 8/3/2019 03 Applets

    3/26

    3

    Packages and classes

    Java supplies a huge library of pre-written code,ready for you to use in your programs

    Code is organized into classes

    Classes are grouped intopackages One way to use this code is to import it

    You can import a single class, or all the classes in apackage

    For this applet, you will need to import two drawingpackages, awt and swing

  • 8/3/2019 03 Applets

    4/26

    4

    Importing some things we need

    To create an applet, you will import the JApplet class

    The JApplet class is in the javax.swingpackage

    This is the only class we will need from the swing package

    import javax.swing.JApplet;

    Capitalization matters!

    Japplet and JApplet are different names; be careful!

    Since you will want to use many classes from thejava.awtpackage, we will just import them all:

    import java.awt.*;

  • 8/3/2019 03 Applets

    5/26

    5

    Your first class, part 2

    public class Drawing extends JApplet {

    }

    Drawing is the name of your class Class names should always be capitalized

    extends JApplet says that ourDrawing is a kind of

    JApplet, but with added capabilities

    Javas JAppletjust makes an empty window

    We are going to draw in that window

  • 8/3/2019 03 Applets

    6/26

    6

    Your first class, part 3

    public class Drawing extends JApplet {

    the code for your class goes in here

    }

    The braces, { }, mark the beginning and ending ofyour code

  • 8/3/2019 03 Applets

    7/26

    7

    The applet so far

    import javax.swing.JApplet;

    import java.awt.*;

    // CIT 591 examplepublic class Drawing extends JApplet {

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

    }

  • 8/3/2019 03 Applets

    8/26

    8

    Methods

    A method is a group of commands that tell the

    computer to do something

    C programmers: methods are similar to functions

    A method takes information in, does something

    with it, and returns a result

    The input information is called the methodsparameters,

    orarguments

    The result is just called a result

  • 8/3/2019 03 Applets

    9/26

    9

    The paint method

    Our applet is going to have a method to paint some

    colored rectangles on the screen

    This method mustbe named paint

    In applets, this is a special method name

    paint needs to be told where on the screen it can draw

    This will be the only parameter it needs

    paint doesnt return any result

  • 8/3/2019 03 Applets

    10/26

    10

    The paint method, part 2

    public void paint(Graphicsg) { }

    public says that anyone can use this method

    void says that it does not return a result

    paint is the name of the method Method names should begin with a lowercase letter

    The argument, or parameter (theres only one) is

    inside parentheses

    The methods commands are inside braces

  • 8/3/2019 03 Applets

    11/26

    11

    By the waynames

    ( ) areparentheses

    { } arebraces

    [ ] arebrackets

  • 8/3/2019 03 Applets

    12/26

    12

    The paint method, part 3

    public void paint(Graphicsg) { }

    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)

    In this program, The type of the parameter is Graphics

    The name of the parameter is g

  • 8/3/2019 03 Applets

    13/26

    13

    The applet so far

    import javax.swing.JApplet;

    import java.awt.*;

    // CIT 590 examplepublic class Drawing extends JApplet {

    public void paint(Graphics g) {

    w

    e still need to put some code in here

    }

    }

  • 8/3/2019 03 Applets

    14/26

    14

    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

  • 8/3/2019 03 Applets

    15/26

    15

    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)

  • 8/3/2019 03 Applets

    16/26

    16

    Setting a color

    To use a color, we tell our Graphics g what color we

    want:

    g.setColor(Color.RED);

    g will remember this color and use it for everything

    until we tell it some different color

  • 8/3/2019 03 Applets

    17/26

    17

    The paint method so far

    public void paint(Graphics g) {

    g.setColor(Color.BLUE);

    draw a rectangle

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

    }

    }

  • 8/3/2019 03 Applets

    18/26

    18

    Pixels

    Apixel 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

  • 8/3/2019 03 Applets

    19/26

    19

    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)

    (50, 0)(0, 0)

    (0, 20) (50, 20)

  • 8/3/2019 03 Applets

    20/26

    20

    Drawing rectangles

    There are two ways to draw rectangles:

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

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

  • 8/3/2019 03 Applets

    21/26

    21

    Drawing strings

    A String is a sequence of characters enclosed in doublequote marks

    "Hello, World!"

    A double quote mark in a String must be preceded by abackslash (\ ) "He said, \"Please don't go!\""

    To draw a string, you need to specify not only whatyou wantto say, but where to say it

    g.drawString( string, left, top ); For example,

    g.drawString("Example JApplet", 20, 80);

  • 8/3/2019 03 Applets

    22/26

    22

    The complete applet

    import javax.swing.JApplet;

    import java.awt.*;

    // CIT 591 example

    public class Drawing extends JApplet {

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

    g.setColor(Color.BLACK);

    g.drawString("Example JApplet", 20, 80);

    }

    }

  • 8/3/2019 03 Applets

    23/26

    23

    More java.awt.Graphics 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,

    arcWidth, arcHeight); arcWidth, arcHeightdefine the roundedness of corners

    g.fillRoundRect(left, top, width, height, arcWidth, arcHeight);

    g.drawArc( left, top, width, height, startAngle, arcAngle);

    Angles are in degrees

    0 degrees is the 3 oclock position

    Positive angles are to the right

    g.FillArc( left, top, width, height, startAngle, arcAngle);

  • 8/3/2019 03 Applets

    24/26

    24

    Still more Graphics methods

    g.drawPolygon(xPoints,yPoints, n);

    g.fillPolygon(xPoints,yPoints, n); xPoints and yPoints are int arrays of size n

    One way to write an int array is:new int[] { value1, value2, ..., valueN}

    Example: g.drawPolygon(new int[] { 250, 290, 210 },new int[] { 210, 290, 290 }, 3);

    draws a triangle using the 3 points (250, 210), (290, 290), and(210, 290).

    g.drawPolyline(xPoints,yPoints, n); A polyline is like a polygon, except the first and last points are not

    automatically connected

    Hence, there is no fillPolyline method

  • 8/3/2019 03 Applets

    25/26

    25

    The HTML page

    You can only run an applet from an HTML page

    The HTML looks something like this:

    Drawing Applet

    Eclipse will create this HTML for you

    You dont even need to think about the HTML just yet

  • 8/3/2019 03 Applets

    26/26

    26

    The End