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).

Full documentation:


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:

  • Save your image to the same "src" (source) folder that contains your java file.
  • Find where your java file is located by going to Eclipse, right-clicking on the java file in Project Explorer (the left-hand menu), and going to "Properties."
  • If you are still having errors, make sure you can see the image file in Eclipse. Refresh the project (right-click the project folder in Eclipse and select "Refresh").