Bai5 Applet

download Bai5 Applet

of 35

Transcript of Bai5 Applet

  • 8/3/2019 Bai5 Applet

    1/35

    GING VIN:

    BI TIN TRNG

    NHP MN JAVA

    BI 5APPLET

    TRNG CAO NG CNG NGH THNG TIN TP.HCM

  • 8/3/2019 Bai5 Applet

    2/35

    2

    NI DUNG TRNH BY

    To cc applet

    i tng ho Graphics

    K thut khung hnh ph

  • 8/3/2019 Bai5 Applet

    3/35

    PHN 1TO CC APPLET

  • 8/3/2019 Bai5 Applet

    4/35

    4

    void setVisible(boolean):hin th hoc n

    component Dimension getSize(): tr v kch thc ca

    component

    void setSize(Dimension): thay i kch thc

    void setEnabled(): bt hoc tt component

    void repaint(): cp nht li component

    void update(Graphics g): c gi qua repaint()

    void paint(Graphics g): c gi qua update()

    void setBackground(Color): t mu nn

    ...

    MT S METHOD CA COMPONENT

  • 8/3/2019 Bai5 Applet

    5/35

    5

    Lp Applet

    Java c lp java.applet.Applet k tha tlp java.awt.Component cho php to racc applet trong Web.

    Mi lp applet do ngi dng to ra uphi k tha t lp Applet.

    XY DNG CC APPLET

  • 8/3/2019 Bai5 Applet

    6/35

    6

    V d 1: To file TestApplet.java

    XY DNG CC APPLET

    Dch: javac TestApplet.java

    import java.applet.Applet;

    import java.awt.Graphics;

    public class TestApplet extends Applet

    {

    public void paint(Graphics g)

    {

    g.drawString(Helloworld!, 50, 25);

    }

    }

  • 8/3/2019 Bai5 Applet

    7/35

    7

    Thc thi applet Cch 1:To file TestApplet.html c ni dung nhsau:

    XY DNG CC APPLET

  • 8/3/2019 Bai5 Applet

    8/35

    8

    import java.applet.Applet;

    import java.awt.Graphics;

    public class TestApplet extends Applet

    {

    public void init() {}public void start() {}

    public void stop() {}

    public void destroy {}

    public void paint(Graphics g) {}}

    KHUNG CA MT APPLET C BN

  • 8/3/2019 Bai5 Applet

    9/35

    9

    init(): khi to applet start(): applet bt u hot ng

    stop(): applet chm dt hot ng

    destroy(): gii phng applet Ch :

    paint() khng phi l phng thc ca Applet ml ca Component.

    paint() c gi mi khi ca s c v li.

    HOT NG CA APPLET

  • 8/3/2019 Bai5 Applet

    10/35

    10

    Vng i ca mt Applet Np mt applet: applet c khi to v thc thi

    Chuyn hoc tr v trang Web: Cc phng

    thc stop v start s c gi Np li applet: nh qu trnh np applet

    Thot khi trnh duyt: phng thc stop vdestroy s c gi

    HOT NG CA MT APPLET

  • 8/3/2019 Bai5 Applet

    11/35

    PHN 2LP GRAPHICS

  • 8/3/2019 Bai5 Applet

    12/35

    12

    java.awt.Graphics l lp cung cp cc phng thcv ho c bn:

    ng thng (Line)

    ng oval (Oval)

    Hnh ch nht (Rectangle)

    a gic (Polygon)

    Vn bn(Text)

    Hnh nh (Image)

    ...

    LP GRAPHICS

  • 8/3/2019 Bai5 Applet

    13/35

    13

    H ta LP GRAPHICS

  • 8/3/2019 Bai5 Applet

    14/35

    14

    V ng thng

    public void drawLine(int x1, int y1, int x2, int y2); V hnh ch nht

    public void drawRect(int x, int y, int width, int height);

    T mt hnh ch nht

    public void fillRect(int x, int y, int width, int height); Xo mt vng ch nht

    public void clearRect(int x, int y, int width, int height);

    V a gic

    public void drawPolygon(int[] x, int[] y, int numPoint);

    public void drawPolygon(Polygon p);

    LP GRAPHICS

  • 8/3/2019 Bai5 Applet

    15/35

    15

    import java.applet.Applet;

    import java.awt.Graphics;

    public class DemoRect extends Applet

    {

    public void init()

    {

    System.out.println("Demonstration of some simple figures");

    }

    public void paint(Graphics g)

    { g.drawLine(70, 300, 400, 250);

    g.drawRect(100, 50, 130, 170);

    g.fillRect(120, 70, 70, 70);

    int[] x = { 280, 310, 330, 430, 370 };

    int[] y = { 280, 140, 170, 70, 90 };

    g.drawPolygon(x, y, x.length);

    }

    }

    LP GRAPHICS

  • 8/3/2019 Bai5 Applet

    16/35

    16

    LP GRAPHICS

  • 8/3/2019 Bai5 Applet

    17/35

    17

    V ng trn/elip

    public void drawOval(int x, int y, int width, int height); T ng trn/elip

    public void fillOval(int x, int y, int width, int height);

    V cung trn

    public void drawArc(int x, int y, int width, int height, intstartAngle, int arcAngle);

    V xu k t

    public void drawString(String str, int x, int y);

    V nh

    public void drawImage(Image img, int x, int y,...);

    LP GRAPHICS

  • 8/3/2019 Bai5 Applet

    18/35

    18

    import java.applet.Applet;

    import java.awt.Graphics;

    public class DemoOval extends Applet

    {

    public void init()

    {

    System.out.println("Demonstration of some simple figures");

    }public void paint(Graphics g)

    {

    int xstart = 70, ystart = 40, size = 100;

    g.drawOval(xstart, ystart, size, size);

    g.drawOval(xstart + (size*3)/4, ystart, size, size);

    g.drawOval(xstart + size/2, ystart + size/2, size, size);

    g.drawArc(xstart, ystart, 300, 200, 0, -90);

    g.drawString("good morning !", xstart + 265, ystart + 90);

    }

    }

    LP GRAPHICS

  • 8/3/2019 Bai5 Applet

    19/35

    19

    LP GRAPHICS

  • 8/3/2019 Bai5 Applet

    20/35

    20

    import java.applet.Applet;

    import java.awt.Graphics;

    import java.awt.Image;

    public classDemoImage extends Applet

    {

    public void init()

    {System.out.println("Demonstration of imaging");

    }

    public void paint(Graphics g)

    {

    Image image = getToolkit().getImage("summer.jpg");

    g.drawImage(image, 0, 0, this);

    }

    }

    LP GRAPHICS

  • 8/3/2019 Bai5 Applet

    21/35

    21

    LP GRAPHICS

  • 8/3/2019 Bai5 Applet

    22/35

    22

    LpPoint:biu din im trn mn hnh

    Lp Dimension:biu din kch thc v

    chiu rng v chiu cao ca mt i tng Lp Rectangle:biu din hnh ch nht

    Lp Polygon:biu din a gic

    Lp Color:biu din mu sc

    CC LP TIN CH KHC

  • 8/3/2019 Bai5 Applet

    23/35

    23

    import java.applet.Applet;

    import java.awt.*;

    public class DemoColor extends Applet

    {

    public void paint(Graphics g)

    {

    Dimension size = getSize();g.setColor(Color.orange);

    g.fillRect(0, 0, size.width, size.height);

    Color color = new Color(10, 150, 20);

    g.setColor(color);g.drawString("I am a colorful string",

    size.width/2 -50, size.height/2);

    }

    }

    CC LP TIN CH KHC

  • 8/3/2019 Bai5 Applet

    24/35

    24

    CC LP TIN CH KHC

  • 8/3/2019 Bai5 Applet

    25/35

    25

    import java.applet.Applet;

    import java.awt.*;

    public class DemoFont extends Applet

    {

    public void paint(Graphics g)

    {

    Font font = newFont("Arial", Font.BOLD, 30);

    g.setFont(font);

    g.drawString("I am font Arial, bold, size 30", 50, 50);

    }

    }

    X L FONT V

  • 8/3/2019 Bai5 Applet

    26/35

    PHN 3K THUTKHUNG HNH PH

  • 8/3/2019 Bai5 Applet

    27/35

    27

    K THUT KHUNG HNH PH V d v s di chuyn b nhy

    import java.applet.Applet;

    import java.awt.*;

    public class DemoMove1 extends Applet

    {

    private int x = 50;

    private int y = 50;

    public void paint(Graphics g)

    {

    if(x > 300) x = 50;

    g.fillOval(x, y, 100, 100);

    delay(100);

    move();

    }

    //xem tip slide tip theo

  • 8/3/2019 Bai5 Applet

    28/35

    28

    K THUT KHUNG HNH PH V d v s di chuyn b nhy

    public void delay(int milisecond)

    {

    try{

    Thread.sleep(milisecond);

    } catch(Exception e) { }

    }

    public void move()

    {

    x += 5;

    repaint();

    // ve lai cua so

    }

    }

  • 8/3/2019 Bai5 Applet

    29/35

    29

    K THUT KHUNG HNH PH

    Gii thch l do nhy:

    Mi ln gi repaint() th update() c gi

    update() xo ni dung ca sv gi paint()

    Khc phc:

    V hnh vo mt khung hnh ph trong b nh thayv v trc tip ra mn hnh.

    Khi v xong, khung hnh ph c hin th ra mnhnh trong phng thc paint().

    Np chng update() trnh vic xo ni dung cas.

  • 8/3/2019 Bai5 Applet

    30/35

    30

    K THUT KHUNG HNH PH Gii php

    //...

    public class DemoMove2 extends Applet

    {

    //...

    private Image offImage;

    private Graphics offGraphics;

    public void init()

    {

    // tao khung hinh phu

    offImage = createImage(500, 500);

    // lay doi tuong do hoa de ve vao khung hinh phu

    offGraphics = offImage.getGraphics();

    }

    //xem tip slide tip theo

  • 8/3/2019 Bai5 Applet

    31/35

    31

    K THUT KHUNG HNH PH Gii php

    public void paint(Graphics g)

    {

    //...

    offGraphics.clearRect(0, 0, 500, 500);

    offGraphics.fillOval(x, y, 100, 100);

    g.drawImage(offImage, 0, 0, this);

    //...

    }

    public voidu pdate(Graphics g)

    {

    paint(g);

    }

    //...

    }

    APPLET

  • 8/3/2019 Bai5 Applet

    32/35

    32

    APPLET

    Kh nng ca Applet Applet c t ti mt Server trn mng Applet c chuyn ti my Client theo mt

    trang HTML no

    Khi mt trnh duyt (tng thch vi Java) nhnc trang web ny, n s ti m ca Applet vthc thi trn my client

    APPLET

  • 8/3/2019 Bai5 Applet

    33/35

    33

    APPLET

    Gii hn ca Applet Khng c np cc th vin hay cc phngthc s dng m gc(native code).

    Khng c c v ghi ln cc tp tin ca myang chy chng.

    Khng c khi ng bt k chng trnh notrn my ang chy.

    Khng c c bt k tnh cht no ca hthng

    BI TP

  • 8/3/2019 Bai5 Applet

    34/35

    34

    BI TP

    1. Dng k thut khung hnh ph v mt

    qu bng chuyn ng t do trong mtapplet.

    2. M rng bi ton vi 2 qu bng.

    3. M rng bi ton vi nn applet l mt hnhnh.

    4. M rng bi ton vi qu bng l mt hnh

    nh v c nhiu qu bng chuyn ngng thi.

  • 8/3/2019 Bai5 Applet

    35/35

    HTBI 5