Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 14: Applets June 21, 2000 Nick Vallidis. Announcements zP6 is due Friday.

Similar presentations


Presentation on theme: "COMP 14: Applets June 21, 2000 Nick Vallidis. Announcements zP6 is due Friday."— Presentation transcript:

1 COMP 14: Applets June 21, 2000 Nick Vallidis

2 Announcements zP6 is due Friday

3 Applets  A Java application is a stand-alone program with a main method zAn applet is a Java program that is intended to be transported over the web and executed using a web browser  An applet can also be executed using jview /a on the command line

4 Applets  An applet doesn't have a main method zInstead, there are several special methods that serve specific purposes  paint method -- automatically executed and is used to draw the applets contents

5 Applets  The paint method accepts a parameter that is an object of the Graphics class  A Graphics object defines a graphics context on which we can draw shapes and text  The Graphics class has several methods for drawing shapes zThe class that defines the applet extends the Applet class zSee Einstein.java (page 93)

6 Applets zAn applet is embedded into an HTML file using the tag that references the bytecode file (nameOfClass.class) of your applet yEinstein.html zIt is actually the bytecode version of the program that is transported across the web zThe applet is executed by a Java interpreter that is part of the browser

7 Drawing Shapes  Graphics class is defined in the java.awt package ydrawArc ydrawLine ydrawOval ydrawRect ydrawString yfillArc yfillOval yfillRect ygetColor ysetColor

8 Drawing Shapes zA shape can be filled or unfilled, depending on which method is invoked zThe method parameters specify coordinates and sizes zMany shapes with curves, like an oval, are drawn by specifying its bounding rectangle zAn arc can be thought of as a section of an oval

9 Drawing a Line X Y 10 20 150 45 page.drawLine (10, 20, 150, 45); page.drawLine (150, 45, 10, 20); or

10 Drawing a Rectangle X Y page.drawRect (50, 20, 100, 40); 50 20 100 40

11 Drawing an Oval X Y page.drawOval (175, 20, 50, 80); 175 20 50 80 boundingrectangle

12 The Color Class  A color is defined in a Java program using an object created from the Color class (defined in the java.awt package) ycontains several static predefined colors yFigure 2.19 page 99 zEvery graphics context has a current foreground color zEvery drawing surface has a background color zSee Snowman.java (page 99-100)

13 More Drawing Techniques zConditionals and loops can greatly enhance our ability to control graphics zSee Bullseye.java (page 157) zSee Boxes.java (page 159) zSee BarHeights.java (page 162)

14 Applet Methods  In previous examples we've used the paint method of the Applet class to draw on an applet  The Applet class has several methods that are invoked automatically at certain points in an applet's life  init - executed only once when the applet is initially loaded  start - called when applet becomes active (when browser loads / returns to the page)  stop - called when applet becomes inactive (when browser leaves the page)

15 Applet Methods  The Applet class also contains other methods that generally assist in applet processing (Figure 4.6, page 211) ygetCodeBase ygetDocumentBase ygetAudioClip ygetImage

16 Graphical Objects zAny object we define by writing a class can have graphical elements  The object must simply obtain a graphics context (a Graphics object) in which to draw zAn applet can pass its graphics context to another object just as it can any other parameter zSee LineUp.java (page 212) zSee StickFigure.java (page 215)

17 Events zAn event is an object that represents some activity to which we may want to respond zWe may want our program to perform some action when the following occurs: ythe mouse is moved ya mouse button is clicked ythe mouse is dragged ya graphical button is clicked ya keyboard key is pressed ya timer expires

18 Events zThe Java standard class library contains several classes that represent typical events zCertain objects, such as an applet or a graphical button, generate (fire) an event when it occurs zOther objects, called a listeners, respond to events zWe can write listener objects to do whatever we want when an event occurs

19 Events and Listeners Generator This object may generate an event Listener This object waits for and responds to an event Event When an event occurs, the generator calls the appropriate method of the listener, passing an object that describes the event

20 Listener Interfaces zWe can create a listener object by writing a class that implements a particular listener interface zThe Java standard class library contains several interfaces that correspond to particular event categories  For example, the MouseListener interface contains methods that correspond to mouse events zAfter creating the listener, we add the listener to the component that might generate the event to set up a formal relationship between the generator and listener

21 Mouse Events zThe following are mouse events: ymouse pressed - button is pressed down ymouse released - button is released ymouse clicked - button is pressed and released ymouse entered - pointer is moved over a particular component ymouse exited - pointer is moved off of a particular component zAny given program can listen for some, none, or all of these zSee Dots.java (page 246) zSee DotsMouseListener.java (page 248)

22 Mouse Motion Events zThe following are called mouse motion events: ymouse moved - the mouse is moved ymouse dragged - the mouse is moved while the mouse button is held down  There is a corresponding MouseMotionListener interface zOne class can serve as both a generator and a listener zOne class can serve as a listener for multiple event types zSee RubberLines.java (page 249)

23 More Applet Examples zScribble.java zAnimatedMsg.java


Download ppt "COMP 14: Applets June 21, 2000 Nick Vallidis. Announcements zP6 is due Friday."

Similar presentations


Ads by Google