Tino APCS

Color class

Java comes with thousands of classes built-in. Every class in the java.lang package is automatically imported, so you don't have to write an import statement to use these classes. Two common classes you will be using from java.lang are:

Representing Color

To change the DrawingTool's color, you will need to understand how colors are represented in programming languages. One of the most common representations is the RGB model.

As shown below, combining Red, Green, and Blue light creates white light (shown in the middle). It's like mixing paint, but with light instead of paint.

Every color visible to the human eye is simply an amount of red, green, and blue light. In programming languages, the amount of red, green, and blue ranges from 0 to 255. Experiment with this Color Picker and verify that:

  • Black is all colors set to 0
  • White is all colors set to 255
  • Yellow is a mixture of Red and Green
  • What ratio of colors creates Turquoise?
  • What ratio of colors creates Brown?

Colors in Java

Now that you understand RGB color components, you can use the Color class to create a Color and use it to change the DrawingTool's color.

Let's start with the code from the bottom of page F, earlier in this lesson. You should already have written this code by hand:

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 
    }  
}  


Open the Color API and read the class summary.

  • In order to use the Color class, you have to import it because it's not part of the default java.lang package.
  • On the previous page, you were told where to find the import location.
  • Use this knowledge to add an import statement for the Color class just below the SketchPad import statement.


Open the DrawingTool API and scroll down to the setColor method. Here's the summary:

  • This tells us that in order to use this method, we must create a Color object c and pass it into the setColor method. When calling the method, you don't have to name your Color variable 'c'. It can be anything.


Next, create a Color object using the second-to-last Constructor in the Constructor section. It looks like this:

Color myColor;                     // Declare a Color object named myColor  
myColor = new Color(127, 0, 127);  // Create and initialize myColor to purple  


You can also declare and create objects in one step:

Color myColor = new Color(127, 0, 127);  // Declare and create in one step  


Finally, use the DrawingTool's setColor method to set the color. (Put this before drawing.)

pen.setColor(myColor);


Here is the final code of DrawSquare.java if you need to see it.

Last modified: August 22, 2023

Back to Using Apis

Dark Mode

Outline