Practical Exercise 2 – Color and Font objects

This prac demonstrates how to work with colours and fonts in Java using Color and Font objects.

Consider the following class:

import java.applet.Applet;
import java.awt.*;

// This class demonstrates simple Graphics with colors and fonts

// declared and then instantiated in the init method.

public class Prac2 extends Applet

{

// To use color and font information from init in paint we must first declare

// the colors and font as global "objects”

Color color1, color2, color3; //declare three Color objects

Font font1; //declare a Font object

// Note init is only called once during any running of a program

public void init()

{

setBackground(Color.yellow);

color1 = new Color(130, 0, 0); //instantiate color1 (medium red)

color2 = color1.brighter(); //instantiate color2

color3 = new Color(0, 0, 255); //instantiate color3 (blue)

font1 = new Font("SansSerif", Font.ITALIC, 14);
} // end init

//The paint method can now make use of the colours and fonts defined in init().

public void paint (Graphics g)

{

g.setColor(color1);

g.drawRect(20,20,120,60);

g.setColor(color2);

g.fillOval(20,20,120,60);

g.setColor (color3);

g.setFont(font1);

g.drawString("Hello World", 50, 50);

}

} //end of program

Objects

Objects are the basic “modules” of an object-oriented program. In the Prac2 class above, color1, color2, color3 and font1 are objects. Objects are stored in memory like variables, but have many more features than variables.

Objects include

·  data (such as the RGB values of the color objects)

·  “methods” or operations on that data (such as the brighter() method).

When objects are used in a class they must be set up in memory. This is called “instantiating” the object.

Setting up objects in a class

To set an output colour, use code such as the following to instantiate a new Color object and set it as the current graphics colour that will be output.

g.setColor ( Color.red ); or

g.setColor ( new Color(255, 0, 0)); 2 ways to choose the colour red

When we do this, we set the new colour as an object in memory. However, this new colour is only used until the next new colour is set. If we have a colour that we want to use on different occasions, we must declare the colour by giving it a name before we instantiate it.

We declare the colours we a going to use with code such as:

Color color1, color2, color3;

When we want to give the colour a value in memory (i.e. we instantiate the Color object) we use the new command:

color1 = new Color( 255, 50, 50);

We can now set our new colour in the paint() method using the setColor() method.

g.setColor(color1);

Global declaration of the objects

In this class, we declare the Color and Font objects immediately after the main class definition. By declaring the objects inside the main class in this way, they become available to all the methods used in the class. This means that we can instantiate the objects in the init() method and then use the same objects in the paint() method.

When the objects are made available to the whole class in this way we say that they are declared globally.

C Standard

  1. Copy and run the above class. Be sure to give your project the same name as the class (e.g. Prac2_C). Remember that class names cannot have spaces.
  2. Declare some new Color and Font objects. Make up your own names for the objects (a combination of letters and numbers). Instantiate the objects in the init() method using the new operator.
  3. Make use of your custom colors and fonts in the paint() method of your applet.

B Standard

  1. Using Paint or a colour selection website, choose three colours. Write down the red, green and blue values of each of the colours.
  2. In your applet, create a Color object for each of your three colours. For each colour, display its name and RGB values, using its own colour.
    Eg. The output for the colour TARDIS_Blue (0,58,105) would be:
    TARDIS_Blue is R: 0, G: 58 and B:105

A Standard

  1. Output the sentence “The quick brown fox jumped over the lazy dog” making the word brown the colour brown and the word lazy in italics. Choose whatever font you like.
    Hint : You will need to use Paint and then Options > Edit Colors (or use a website such as http://www.colorpicker.com/ or http://www.easyrgb.com/ ) to find the red, green and blue components that make up brown.
  2. Find out what fonts are available to be used in Java. Are there any limitations?

Hobart College 2017, adapted from Rosny College 2009 Page 1 of 3