Tino APCS

Using Objects

In order to use an object in a program you must:

  1. Declare the type of object you want to create
  2. Give the object a variable name
  3. Create the object using one of its constructors

Here's an example of declaring and creating a SketchPad object named patty:

SketchPad patty;                    // Declare a SketchPad variable named "patty"
patty = new SketchPad(800, 600);    // Create patty the SketchPad using a SketchPad constructor
                                    // The parameters 800 and 600 represent the width and height of the SketchPad


A constructor is a method with the same name as the class, and it is used to create and initialize an object as shown above.

A method is an action that an object can perform.

Let's add a second object, a DrawingTool, that can draw on patty the SketchPad.

SketchPad patty;                    // Declare a SketchPad variable named "patty"
patty = new SketchPad(800, 600);    // Create patty the SketchPad using a SketchPad constructor
                                    // The parameters 800 and 600 represent the width and height of the SketchPad

DrawingTool bob;                    // Declare a DrawingTool object named bob
bob = new DrawingTool(patty);       // Initizlize bob using one of its constructors.
                                    // Notice that bob is told where to draw (on patty) in the constructor.


Once you have declared and created an object you can then command it to do things using its methods. Methods (behaviors of an object) are the actions an object can perform. Here are some of the methods that DrawingTool objects can perform:

Method name Parameters Description
forward (double distance) Moves the DrawingTool forward this many pixels
turnRight (double degrees) Rotates the DrawingTool clockwise by this many degrees
setWidth (int width) Changes the width of the DrawingTool
fillCircle (double radius) Draws a filled circle with the given radius


The parameters of a method tell you what kind of information you must provide in order to use the method. Each parameter will be shown in two parts:

datatype parametername

The data type tells you the kind of information you must provide to the method (a type of number, a piece of text, a specific kind of object like a SketchPad, etc.) and the parameter name tells you what the data represents. For example, the method

setWidth(int width)


means that in order to use the setWidth method, you must pass in an integer that represents the new width of the DrawingTool. If you declared and created a DrawingTool named bob then you would change bob's width by saying something like:

bob.setWidth(10);  // Tells bob (who is a kind of DrawingTool) to perform his setWidth() command.


Java has eight primitive data types plus objects that are each their own type. We will study the eight primitive data types in Lesson 3. For now, you should know these:

Data type What it represents Examples
int an integer 5, 312, -18
double a floating-point number 3.14, -1000, 0.0001 1
String a piece of text "Hello Java!", "I like apple 3.14159" 2


In order to use non-default libraries in Java (ex: gpdraw), you must tell Java to include the library by using an import statement at the top of your code. We'll go into more detail later but for now you need to know that in order to use DrawingTool and SketchPad objects you must add the following import statements at the top of your code:

import gpdraw.DrawingTool;  // Imports the DrawingTool class
import gpdraw.SketchPad;    // Imports the SketchPad class


Alternately, you could import everything inside the gpdraw package by using a * wildcard - which means all classes.

import gpdraw.*;  // Imports all classes (DrawingTool and SketchPad) inside the gpdraw.jar library

Putting it altogether

Let's put this all together to write a program that uses a DrawingTool object to draw a square on a SketchPad object. (Follow along in BlueJ!)

Begin by creating a new class in BlueJ. Name the class DrawSquare, remove BlueJ's starter code, and add this code:

public class DrawSquare {

}


Next, add import statements above the class header, so we can use the gpdraw library objects.

import gpdraw.DrawingTool;  // Import DrawingTool class
import gpdraw.SketchPad;    // Import SketchPad class

public class DrawSquare {

}


Add a main method.

import gpdraw.DrawingTool;  // Import DrawingTool class
import gpdraw.SketchPad;    // Import SketchPad class

public class DrawSquare {

     public static void main(String[] args) {

     }
}


Declare variables for a SketchPad and DrawingTool. Then create/initialize them using their constructors.

import gpdraw.DrawingTool;  // Import DrawingTool class
import gpdraw.SketchPad;    // Import SketchPad class

public class DrawSquare {

     public static void main(String[] args) {

          SketchPad paper;  // Declare a SketchPad variable
          DrawingTool pen;  // Declare a DrawingTool variable

          paper = new SketchPad(400, 300);  // Create and initialize the paper
          pen = new DrawingTool(paper);     // Create and initialize the pen
     }

}


Finally, command the DrawingTool object to draw a square using its methods.

import gpdraw.DrawingTool;  // Import DrawingTool class
import gpdraw.SketchPad;    // Import SketchPad class

public class DrawSquare {

     public static void main(String[] args) {

          SketchPad paper;  // Declare a SketchPad variable
          DrawingTool pen;  // Declare a DrawingTool variable

          paper = new SketchPad(400, 300);  // Create and initialize the paper
          pen = new DrawingTool(paper);     // Create and initialize the pen

          pen.setWidth(10);     // Change the pen width to 10

          pen.forward(100);     // Draw left side, 100 pixels long
          pen.turnRight(90);
          pen.forward(100);     // Draw top
          pen.turnRight(90);
          pen.forward(100);     // Draw right
          pen.turnRight(90);
          pen.forward(100);     // Draw bottom
     }

}

Closing thoughts


There is much about the SketchPad and DrawingTool that we don't know yet.

  • What is the coordinate system?
  • What is the default thickness?
  • How do I know what to pass into the constructors?
  • How do I change the drawing color?

In the next section we will look at the full SketchPad and DrawingTool APIs, which tells you everything you need to know about how to use them.


  1. Note that a double can also hold an integer. 

  2. Note that a String is a kind of object, not a primitive 

Last modified: December 12, 2022

Back to First Program

Dark Mode

Outline