Abstract Window Toolkit Program Design With Methods And

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

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

Java Application – javac Some. App. java – java Some. App compiling java source javac byte code executing byte code JVM machine code Program Design With Methods And Graphics / Chapter 4 3
![Java Application • execution starts with main public static void main(String args[] ) { Java Application • execution starts with main public static void main(String args[] ) {](http://slidetodoc.com/presentation_image_h2/8d00a7f20641d2ca898c856f6acbebad/image-4.jpg)
Java Application • execution starts with main public static void main(String args[] ) { … } Program Design With Methods And Graphics / Chapter 4 4

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

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

Appletviewer • executes applet • utility to test applets • included with J 2 SDK Program Design With Methods And Graphics / Chapter 4 7

Java Applet keyword public class Intersection extends Applet { …} class name base class Intersection inherits from. With base class Program Design Methods And Applet Graphics / Chapter 4 8

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() Program Design With Methods And Graphics / Chapter 4 9

Java Applet public void init() • first method called by the browser or appletviewer • initializes the applet Program Design With Methods And Graphics / Chapter 4 10

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

Hyper. Text Markup Language (HTML) width of display area Class name. html: <html> <applet code =“class name. class” width=width height=height> </applet> </html> name of class width of display area Program Design With Methods And Graphics / Chapter 4 12

Java Applet the following must exist before executing an applet: • class. Name. html • class. Name. java • class. Name. class Program Design With Methods And Graphics / Chapter 4 13

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. set. Color(Color. black); g. draw. Line( 0, 0, 300, 200); //draws line from (300, 0) to (0, 200) g. set. Color(Color. blue); g. draw. Line( 300, 0, 0, 200); } } Program Design With Methods And Graphics / Chapter 4 14

Graphics • • set. Color() draw. Line() draw. Rect() draw. String() Program Design With Methods And Graphics / Chapter 4 15

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

draw. Line() • draws a line g. draw. Line( start point x-coordinate, start point y-coordinate, end point x-coordinate, end point y-coordinate ); Program Design With Methods And Graphics / Chapter 4 17

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

draw. Rect() • draws a rectangle based on its coordinates object. draw. Rect( upper left x-coordinate, upper left y-coordinate, width of rectangle, • width and height of rectangle should be non negative values height of rectangle, ); Program Design With Methods And Graphics / Chapter 4 19

draw. Rect() width of rectangle, height of rectangle, g. draw. Rect(15, 10, 270, 20); upper left x-coordinate, upper left y-coordinate, Program Design With Methods And Graphics / Chapter 4 20

draw. String() draws a string at the specified location syntax object. draw. String(“string”, x-coordinate, y-coordinate) 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 21

draw. String object. draw. String(“The sum is” + sum , 25); string to print x-coordinate Program Design With Methods And Graphics / Chapter 4 y-coordinate 22

Additional Graphics • See page 163 Program Design With Methods And Graphics / Chapter 4 23
- Slides: 23