DOC PREVIEW
Stanford CS 106A - Interactive Graphics

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Interactive GraphicsEric RobertsCS 106AJanuary 27, 2010The acm.graphics Model• The acm.graphics package uses a collage model in whichyou create an image by adding various objects to a canvas.• A collage is similar to a child’s felt board that serves as abackdrop for colored shapes that stick to the felt surface. Asan example, the following diagram illustrates the process ofadding a red rectangle and a green oval to a felt board:• Note that newer objects can obscure those added earlier. Thislayering arrangement is called the stacking order.2Structure of the acm.graphics PackageThe following diagram shows the classes in the acm.graphicspackage and their relationship in the Java class hierarchy:GCanvas GPoint GDimension GRectangleGObjectGCompoundGMathGTurtleGPenGLabel GRect GOval GLine GArc GImage GPolygonGRoundRect G3DRectInterfaces:GFillableGResizableGScalableGContainerThe GCanvas Class• The GCanvas class is used to represent the background canvasfor the collage model and therefore serves as a virtual feltboard. When you use the acm.graphics package, you createpictures by adding various GObjects to a GCanvas.• For simple applications, you won’t actually need to work withan explicit GCanvas object. Whenever you run a program thatextends GraphicsProgram, the initialization process for theprogram automatically creates a GCanvas and resizes it so thatit fills the program window.• Most of the methods defined for the GCanvas class are alsoavailable in a GraphicsProgram, thanks to an importantstrategy called forwarding. If, for example, you call the addmethod in a GraphicsProgram, the program passes thatmessage along to the underlying GCanvas object by calling itsadd method with the same arguments.3Methods in the GCanvas ClassThe following methods are available in both the GCanvas andGraphicsProgram classes:Adds the object to the canvas at the front of the stack Moves the object to (x, y) and then adds it to the canvasRemoves the object from the canvasRemoves all objects from the canvasReturns the frontmost object at (x, y), or null if none Returns the width in pixels of the entire canvasReturns the height in pixels of the entire canvasSets the background color of the canvas to c.add(object)add(object, x, y)remove(object)removeAll()getElementAt(x, y)getWidth()getHeight()setBackground(c)Pauses the program for the specified time in millisecondsSuspends the program until the user clicks the mousepause(milliseconds)waitForClick()The following methods are available in GraphicsProgram only:The Two Forms of the add Method• The add method comes in two forms. The first is simplyadd(object);which adds the object at the location currently stored in itsinternal structure. You use this form when you have alreadyset the coordinates of the object, which usually happens at thetime you create it.• The second form isadd(object, x, y);which first moves the object to the point (x, y) and then addsit there. This form is useful when you need to determinesome property of the object before you know where to put it.If, for example, you want to center a GLabel, you must firstcreate it and then use its size to determine its location.4Methods Common to All GObjectssetLocation(x, y)move(dx, dy)movePolar(r, theta)getX()getY()getWidth()getHeight()contains(x, y)setColor(c)getColor()setVisible( flag)isVisible()sendToFront()sendToBack()sendForward()sendBackward()Resets the location of the object to the specified pointMoves the object dx and dy pixels from its current position Moves the object r pixel units in direction thetaReturns the x coordinate of the objectReturns the y coordinate of the objectReturns the horizontal width of the object in pixelsReturns the vertical height of the object in pixelsReturns true if the object contains the specified pointSets the color of the object to the Color c Returns the color currently assigned to the objectSets the visibility flag (false = invisible, true = visible) Returns true if the object is visibleSends the object to the front of the stacking orderSends the object to the back of the stacking orderSends the object forward one position in the stacking orderSends the object backward one position in the stacking orderSharing Behavior through Interfaces• In addition to the methods defined for all GObjects shown onthe preceding slide, there are a few methods that apply tosome GObject subclasses but not others. You already know,for example, that you can call setFilled on either a GOval ora GRect. At the same time, it doesn’t make sense to callsetFilled on a GLine because there is no interior to fill.• In Java, the best strategy when you have methods that applyto some subclasses but not others is to define an interface thatspecifies the shared methods. An interface definition issimilar to a class definition except that the interface omits theimplementation of each method, retaining only the header linethat shows the types of each argument.• In the acm.graphics package, there are three interfaces thatdefine methods for certain GObject subclasses: GFillable,GResizable, and GScalable. The methods in these interfacesappear on the next slide.5Methods Defined by InterfacessetFilled( flag)isFilled()setFillColor(c)getFillColor()Sets the fill state for the object (false = outlined, true = filled) Returns the fill state for the objectSets the color used to fill the interior of the object to cReturns the fill colorGFillable (GArc, GOval, GPolygon, GRect)setSize(width, height)setBounds(x, y, width, height)Sets the dimensions of the object as specifiedSets the location and dimensions togetherGResizable (GImage, GOval, GRect)scale(sf )scale(sx, sy)Scales both dimensions of the object by sfScales the object by sx horizontally and sy verticallyGScalable (GArc, GCompound, GLine, GImage, GOval, GPolygon, GRect)Using the Shape Classes• The shape classes are the GObject subclasses that appear inyellow at the bottom of the hierarchy diagram.GObjectGRoundRect G3DRect• Each of the shape classes corresponds precisely to a methodin the Graphics class in the java.awt package. Once youhave learned to use the shape classes, you will easily be ableto transfer that knowledge to Java’s standard graphics tools.GLabel GRect GOval GLine GArc GImage GPolygon6The GLabel ClassYou’ve been using the GLabel


View Full Document

Stanford CS 106A - Interactive Graphics

Download Interactive Graphics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Interactive Graphics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Interactive Graphics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?