Chapter 4 Simple Graphics
- In your own words, define the terms class, object, and
An object is a conceptually integrated entity that ties together state information and the operations that affect that state. A class is a template that defines the attributes and operations shared by all objects of a particular type. A method is a function that belongs to a class and has access to its state.
- What is a reference?
A reference is a value internal to the computer that serves as a link to the data in the actual object.
- What is the receiver syntax?
- What is a factory method?
A factory method is a function that creates and returns a new object. By convention, names of factory methods begin with an uppercase letter.
- What is the first line in every graphical program that appears in this book?
let gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT);
- What are the four classes of graphical objects introduced in this chapter?
GRect, GOval, GLine, and GLabel
- How do you change the color of a graphical object?
You need to call the setColor method, passing in the name of the color.
- What is the purpose of the setFilled and setFillColor methods in the
GRect and GOval classes?
By default, the GRect and GOval classes create objects that are drawn as outlines. Calling setFilled(true) changes the state of the object so that the interior is filled in the same color as the outline. Calling the setFillColor method allows the caller to set the color of the interior without changing the color of the border.
- What is the format of the argument string passed to setFont?
The argument to setFont is formatted to match the designation of a font in CSS, which is the technology used to define the style and appearance of a web page. The font string consists of a combination of components—style, weight, size, and family—most of which are optional. The format of this string is described in more detail on page 129
- Define the following terms in the context of the GLabel class: baseline, origin, height, ascent, and
- The baseline is the imaginary line on which characters
- The origin is the point at which the text of a label
- The height is the distance between successive
- The ascent is the maximum extent above the
- The descent is the maximum extent below the
- Explain the purpose of the two following lines in the CenteredHelloWorld
let x = (gw.getWidth() – msg.getWidth()) / 2; let y = (gw.getHeight() + msg.getAscent()) / 2;
Why is there a minus sign in the calculation of the x coordinate and a plus sign in the calculation of the y coordinate?
These lines compute the coordinates for the origin of a string centered in the graphics window. The + in the calculation of the y coordinate reflects the fact that the value of the y coordinate increases downward.
- When you center a GLabel vertically using the getAscent method, why does the resulting text often appear to be a few pixels too low?
The getAscent method returns the maximum distance that characters extend above the baseline. If the characters in a particular string are not that tall, those characters will appear below the centerline of a string containing full-height characters.
- What is the collage model?
The term collage model refers to the metaphor used to create an image by adding shapes to the window, much as a collage artist would assemble a picture from shapes created using a variety of materials.
- What is meant by the term stacking order? What other term is often used for the same purpose?
The term stacking order refers to the order in which the graphical elements are drawn on the window. Objects toward the front of the stacking order can obscure all or parts of objects that appear further toward the back. This arrangement is often called z-ordering, which reflects the mathematical convention of having the z-axis extend outward from the x–y plane.