23
Program Design With Metho ds And Graphics / Chapter 4 1 Abstract Window Toolkit

Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit

  • View
    225

  • Download
    0

Embed Size (px)

Citation preview

Program Design With Methods And Graphics / Chapter 4

1

Abstract Window Toolkit

Program Design With Methods And Graphics / Chapter 4

2

Abstract Window Toolkit

• class library – graphics

• lines

• rectangles

• circles

– GUI• text boxes

• labels

• command buttons

Program Design With Methods And Graphics / Chapter 4

3

Java Application

– javac SomeApp.java– java SomeApp

byte codejavacjava source

machine codeJVM

byte code

compiling

executing

Program Design With Methods And Graphics / Chapter 4

4

Java Application

• execution starts with main

public static void main(String args[] ) { … }

Program Design With Methods And Graphics / Chapter 4

5

Java Applet

– javac SomeApplet.java– appletviewer SomeApplet.html

byte codejavacjava source

machine codeappletviewerhtml source

compiling

executing

Program Design With Methods And Graphics / Chapter 4

6

Java Applet

• execution starts with the browser / appletviewer– browser / appletviewer loads the .class file

Program Design With Methods And Graphics / Chapter 4

7

Appletviewer

• executes applet

• utility to test applets

• included with J2SDK

Program Design With Methods And Graphics / Chapter 4

8

Java Applet

public class Intersection extends Applet { …}

class name

keyword

base class

class Intersection inherits from base class Applet

Program Design With Methods And Graphics / Chapter 4

9

Java Appletjava applet has the following methods:

public void init()

public void start()

public void paint(Graphics g)

public void stop()

public void destroy()

Program Design With Methods And Graphics / Chapter 4

10

Java Applet

public void init()

•initializes the applet

•first method called by the browser or appletviewer

Program Design With Methods And Graphics / Chapter 4

11

Java Applet

public void paint(Graphics g)

•after init()

•refreshing the browser

Program Design With Methods And Graphics / Chapter 4

12

HyperText Markup Language (HTML)

<html>

<applet code =“class name.class” width=width height=height>

</applet>

</html>

Class name.html:

name of class width of display area

width of display area

Program Design With Methods And Graphics / Chapter 4

13

Java Applet

• className.html

• className.java

• className.class

the following must exist before executing an applet:

Program Design With Methods And Graphics / Chapter 4

14

Intersection Example(Intersection.java)

import java.awt.*;

import java.applet.Applet;

public class Intersection extends Applet{

public void paint(Graphics g){

//draws line from (0,0) to (300,200)

g.setColor(Color.black);

g.drawLine( 0, 0, 300, 200);

//draws line from (300, 0) to (0, 200)

g.setColor(Color.blue);

g.drawLine( 300, 0, 0, 200);

}

}

}

Program Design With Methods And Graphics / Chapter 4

15

Graphics

• setColor()

• drawLine()

• drawRect()

• drawString()

Program Design With Methods And Graphics / Chapter 4

16

setColor

• g.setColor(Color.black);– where g is the object

sets color

Program Design With Methods And Graphics / Chapter 4

17

drawLine()

•draws a line

g.drawLine(

);

start point x-coordinate,

end point x-coordinate,

start point y-coordinate,

end point y-coordinate

Program Design With Methods And Graphics / Chapter 4

18

drawLine()

g.drawLine( 0, 0, 300, 200);

starting point end point

•method name

•draws a line

Program Design With Methods And Graphics / Chapter 4

19

drawRect()•draws a rectangle based on its coordinates

object.drawRect(

upper left x-coordinate,

width of rectangle,

upper left y-coordinate,

height of rectangle,

);

•width and height of rectangle should be non negative values

Program Design With Methods And Graphics / Chapter 4

20

drawRect()

g.drawRect(15, 10, 270, 20);

upper left x-coordinate, upper left y-coordinate,

width of rectangle, height of rectangle,

Program Design With Methods And Graphics / Chapter 4

21

drawString()

draws a string at the specified location

object.drawString(“string”, x-coordinate, y-coordinate)

syntax

string to print

•coordinates (or position) at which the string is drawn

•coordinates are measured from the upper left corner of applet

Program Design With Methods And Graphics / Chapter 4

22

drawString

object.drawString(“The sum is” + sum , 25, 25);

string to print x-coordinate y-coordinate

Program Design With Methods And Graphics / Chapter 4

23

Additional Graphics

• See page 163