Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit."— Presentation transcript:

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

2 Program Design With Methods And Graphics / Chapter 4 2 Abstract Window Toolkit class library –graphics lines rectangles circles –GUI text boxes labels command buttons

3 Program Design With Methods And Graphics / Chapter 4 3 Java Application –javac SomeApp.java –java SomeApp byte code javac java source machine code JVM byte code compiling executing

4 Program Design With Methods And Graphics / Chapter 4 4 Java Application execution starts with main public static void main(String args[] ) { … }

5 Program Design With Methods And Graphics / Chapter 4 5 Java Applet –javac SomeApplet.java –appletviewer SomeApplet.html byte code javac java source machine code appletviewer html source compiling executing

6 Program Design With Methods And Graphics / Chapter 4 6 Java Applet execution starts with the browser / appletviewer –browser / appletviewer loads the.class file

7 Program Design With Methods And Graphics / Chapter 4 7 Appletviewer executes applet utility to test applets included with J2SDK

8 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

9 Program Design With Methods And Graphics / Chapter 4 9 Java Applet java applet has the following methods: public void init() public void start() public void paint(Graphics g) public void stop() public void destroy()

10 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

11 Program Design With Methods And Graphics / Chapter 4 11 Java Applet public void paint(Graphics g) after init() refreshing the browser

12 Program Design With Methods And Graphics / Chapter 4 12 HyperText Markup Language (HTML) Class name.html: name of classwidth of display area

13 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:

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

15 Program Design With Methods And Graphics / Chapter 4 15 Graphics setColor() drawLine() drawRect() drawString()

16 Program Design With Methods And Graphics / Chapter 4 16 setColor g.setColor(Color.black); –where g is the object sets color

17 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

18 Program Design With Methods And Graphics / Chapter 4 18 drawLine() g.drawLine( 0, 0, 300, 200); starting pointend point method name draws a line

19 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

20 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,

21 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

22 Program Design With Methods And Graphics / Chapter 4 22 drawString object.drawString(“The sum is” + sum, 25, 25); string to printx-coordinatey-coordinate

23 Program Design With Methods And Graphics / Chapter 4 23 Additional Graphics See page 163


Download ppt "Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit."

Similar presentations


Ads by Google