DOC PREVIEW
Stanford CS 106A - Graphical Structures Slides

This preview shows page 1 out of 4 pages.

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

Unformatted text preview:

Eric Roberts Handout #30CS 106A January 29, 2010Graphical Structures SlidesGraphical StructuresEric RobertsCS 106AJanuary 29, 2010The GPolygon Class•The GPolygon class is used to represent graphical objectsbound by line segments. In mathematics, such figures arecalled polygons and consist of a set of vertices connected byedges. The following figures are examples of polygons:diamond regular hexagonfive-pointed star• Unlike the other shape classes, that location of a polygon isnot fixed at the upper left corner. What you do instead is picka reference point that is convenient for that particular shapeand then position the vertices relative to that reference point.• The most convenient reference point is often the geometriccenter of the object.Constructing a GPolygon Object•The GPolygon constructor creates an empty polygon. Onceyou have the empty polygon, you then add each vertex to thepolygon, one at a time, until the entire polygon is complete.• The most straightforward way to create a GPolygon is to usethe method addVertex(x, y), which adds a new vertex to thepolygon. The x and y values are measured relative to thereference point for the polygon rather than the origin.• When you start to build up the polygon, it always makessense to use addVertex(x, y) to add the first vertex. Onceyou have added the first vertex, you can call any of thefollowing methods to add the remaining ones:– addVertex(x, y)adds a new vertex relative to the reference point– addEdge(dx, dy) adds a new vertex relative to the preceding one– addPolarEdge(r, theta) adds a new vertex using polar coordinatesEach of these strategies is illustrated in a subsequent slide.Using addVertex and addEdge•The addVertex and addEdge methods each add one newvertex to a GPolygon object. The only difference is in howyou specify the coordinates. The addVertex method usescoordinates relative to the reference point, while the addEdgemethod indicates displacements from the previous vertex.• Your decision about which of these methods to use is basedon what information you have readily at hand. If you caneasily calculate the coordinates of the vertices, addVertex isprobably the right choice. If, however, it is much easier todescribe each edge, addEdge is probably a better strategy.• No matter which of these methods you use, the GPolygonclass closes the polygon before displaying it by adding anedge from the last vertex back to the first one, if necessary.• The next two slides show how to construct a diamond-shapedpolygon using the addVertex and the addEdge strategies.Drawing a Diamond (addVertex)DrawDiamondThe following program draws a diamond using addVertex:public void run() { GPolygon diamond = createDiamond(100, 75); diamond.setFilled(true); diamond.setFillColor(Color.MAGENTA); add(diamond, getWidth() / 2, getHeight() / 2);}private GPolygon createDiamond(double width, double height) { GPolygon diamond = new GPolygon(); diamond.addVertex(-width / 2, 0); diamond.addVertex(0, -height / 2); diamond.addVertex(width / 2, 0); diamond.addVertex(0, height / 2); return diamond;}diamondDrawing a Diamond (addEdge)DrawDiamondThis program draws the same diamond using addEdge:public void run() { GPolygon diamond = createDiamond(100, 75); diamond.setFilled(true); diamond.setFillColor(Color.MAGENTA); add(diamond, getWidth() / 2, getHeight() / 2);}private GPolygon createDiamond(double width, double height) { GPolygon diamond = new GPolygon(); diamond.addVertex(-width / 2, 0); diamond.addEdge(width / 2, -height / 2); diamond.addEdge(width / 2, height / 2); diamond.addEdge(-width / 2, height / 2); diamond.addEdge(-width / 2, -height / 2); return diamond;}diamond– 2 –Using addPolarEdge• In many cases, you can determine the length and direction ofa polygon edge more easily than you can compute its x and ycoordinates. In such situations, the best strategy for buildingup the polygon outline is to call addPolarEdge(r, theta),which adds an edge of length r at an angle that extends thetadegrees counterclockwise from the +x axis, as illustrated bythe following diagram:• The name of the method reflects the fact that addPolarEdgeuses what mathematicians call polar coordinates.rthetaDrawing a Hexagonskip simulationpublic void run() { GPolygon hexagon = createHexagon(50); add(hexagon, getWidth() / 2, getHeight() / 2);}hexagonThis program draws a regular hexagon using addPolarEdge:DrawHexagonprivate GPolygon createHexagon(double side) { GPolygon hex = new GPolygon(); hex.addVertex(-side, 0); int angle = 60; for (int i = 0; i < 6; i++) { hex.addPolarEdge(side, angle); angle -= 60; } return hex;}hexside angle50.0 600-60-120-180-240-300public void run() { GPolygon hexagon = createHexagon(50); add(hexagon, getWidth() / 2, getHeight() / 2);}private GPolygon createHexagon(double side) { GPolygon hex = new GPolygon(); hex.addVertex(-side, 0); int angle = 60; for (int i = 0; i < 6; i++) { hex.addPolarEdge(side, angle); angle -= 60; } return hex;}hexDefining GPolygon Subclasses•The GPolygon class can also serve as the superclass for newtypes of graphical objects. For example, instead of calling amethod like the createHexagon method from the precedingslide, you could also define a GHexagon class like this:public class GHexagon extends GPolygon { public GHexagon(double side) { addVertex(-side, 0); int angle = 60; for (int i = 0; i < 6; i++) { addPolarEdge(side, angle); angle -= 60; } }}•The addVertex and addPolarEdge calls in the GHexagonconstructor operate on the object being created, which is setto an empty GPolygon by the superclass constructor.Exercise: Using the GPolygon ClassDefine a class GCross that represents a cross-shaped figure. Theconstructor should take a single parameter size that indicatesboth the width and height of the cross. Your definition shouldmake it possible to execute the following program to produce thediagram at the bottom of the slide:public void run() { GCross cross = new GCross(100); cross.setFilled(true); cross.setColor(Color.RED); add(cross, getWidth() / 2, getHeight() / 2);}RedCrossCreating Compound Objects•The GCompound class in the acm.graphics package makes itpossible to combine several graphical objects so that theresulting structure behaves as a single GObject.• The easiest way to think about the GCompound class is as acombination of a GCanvas and a


View Full Document

Stanford CS 106A - Graphical Structures Slides

Download Graphical Structures Slides
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 Graphical Structures Slides 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 Graphical Structures Slides 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?