Written by Eric Roberts and Lisa Yan
Here are the most common graphics commands:
Some other helpful graphics commands:
getWidth(); // returns the width of the screen.
getHeight(); // returns the height of the screen.
add(object); // adds an object to the screen.
add(object, x, y); // adds an object to the screen at coordinates (x, y).
remove(object); // removes an object from the screen.
getElementAt(x, y); // returns the element at (x, y).
Recall that setLocation(x,y) sets the origin of a GRect, GOval, or GLine to the coordinates (x,y), where the origin is defined as the top-left corner. GLabel has the same definition of origin, but the height is defined a bit taller than the actual text height. Here is a detailed look at all the different height definitions for GLabel:
Side note: The origin is the middle-left in the image above, because we are dealing with a multi-line GLabel. For GLabels with single lines, the origin is the top-left.
So to center a GLabel vertically, we would actually use the ascent of a GLabel:
double y = getHeight()/2 + label.getAscent()/2;
You can also upload your own images and add them to your graphics program!
A few tips: