Next: Putting it together. Up: Using objects. Previous: What is an object?.


Some basic graphics classes

Textbook: Sections 6.1 and 6.2

The csbsju.cs160 package defines several useful classes. Among them are three classes for graphics with which we'll become very familiar over the course of the semester.

The DrawableFrame class

Suppose, for example, we want to create a variable referring to a window.

DrawableFrame window;
This, however, only creates a variable that refers to a window. It doesn't actually create a window. To do that, we'll have to explicitly use the new keyword.
window = new DrawableFrame();
The new keyword generates a new instance of the class - in this case the class DrawableFrame. The parentheses are required; later, we'll see that you can pass parameters inside the parentheses to specify how to set up the new instance (similar to how you pass parameters to methods).

The DrawableFrame class defines several methods. One particularly useful method in DrawableFrame is its show() method, which displays the window on the screen. We'll always want to use this basically just after we create an instance.

window.show();

Note the syntax: When you want to call an instance method on a particular object instance, name the instance, followed by a period, followed by the instance method name, followed by parentheses (which in many cases will enclose arguments to be passed into the method).

As an another example, the getWidth() method of DrawableFrame returns an integer representing how many pixels the window is wide.

IO.println(window.getWidth());

The Graphics class

You can think of a Graphics object as being something like a paintbrush that you can use to draw onto a window.

The Graphics class is a little different from DrawableFrame in that we'll never actually create instances of Graphics objects directly - instead, we'll use the getGraphics() instance method in DrawableFrame, which returns an instance of the Graphics class.

Graphics g = window.getGraphics();

The Graphics class provides several useful instance methods. For example, one of them is fillOval, which draws an oval in the window. It takes four int parameters, representing respectively the x-coordinate of the oval's left side, the y-coordinate of the oval's top side, the oval's width, and the oval's height.

Note that, in numbering pixels, Java (like just about all other windowing programs) starts at the top left corner and goes downwards and to the left. Thus the oval drawn at (90,67) is above the oval drawn at (85,85), since its y-coordinate is smaller.

Consider the following complete program.

import csbsju.cs160.*;

public class SnowPerson {
    public static void run() {
        DrawableFrame window = new DrawableFrame();
        window.show();
            
        Graphics g = window.getGraphics();
        g.fillOval(90, 67, 20, 20);
        g.fillOval(85, 85, 30, 30);
        g.fillOval(75, 112, 50, 50);
    }
}
This creates the following window.

Graphics defines a host of instance methods. Now would be a good time to open up the csbsju.cs160 documentation, which gives an exhaustive list of the methods you can use on the various objects we'll be using for the first part of this semester. One of the skills you'll need to develop this semester is being able to use this documentation to determine how classes work based on the documentation. Being able to navigate this documentation is essential for using Java well.

The Color class

The final class we'll investigate is Color, which represents a color on the canvas. To create a Color object, you pass parameters into its constructor method, representing the red, green, and blue components of the color.

Color brown = new Color(0.6, 0.6, 0.0);
In fact, the Color class defines several constants (class variables) that already represent useful colors: Color.red, Color.green, etc.

The Color class doesn't really define any methods; the class exists primarily so that you can pass instances of it into the getGraphics() instance method of Graphics. Once you do this, the Graphics instance will use it whenever it draws anything to the screen in the future.

We might modify our earlier program to draw brown arms for our snow person.

        g.setColor(new Color(0.6, 0.6, 0.0));
        g.drawLine(100, 100, 140, 80);
        g.drawLine(100, 100, 60, 80);
        g.setColor(Color.black);
        g.fillOval(90, 67, 20, 20);
        g.fillOval(85, 85, 30, 30);
        g.fillOval(75, 112, 50, 50);


Next: Putting it together. Up: Using objects. Previous: What is an object?.