The Color and Font classes

 

The Color class

Object 

\

Color 
black
blue
cyan
darkGray
gray
green
lightGray
magenta
orange
pink
red
white
yellow

To reference one of these predefined objects you need only code an expression such as Color.yellow.

NOTE: Beginning with Java 2 version 1.4, the Color class has been expanded to contain capitalized versions of the above object references (Color.YELLOW, Color.PINK, etc.). Avoid the capitalized references if using older versions of Java.

Color brown = new Color(255, 128, 0);

where the parameters specify the level of red, green, and blue as integers in the range of 0 to 255.

Method

Usage

getRed()

Returns the level of red in the range of 0 - 255

getBlue()

Returns the level of blue in the range of 0 - 255

getGreen()

Returns the level of green in the range of 0 - 255

Example:

import java.awt.*;
import java.awt.event.*;

public class App extends Frame implements WindowListener, ActionListener {

  Button blot, more, less;
  int greenLevel = 160;

  public static void main(String[] args) {
    App myWindow = new App("Green Blot");
    myWindow.setSize(200,150);
    myWindow.setVisible(true);
  }

  public App(String title) {
    super(title);
    setLayout(new BorderLayout());
    addWindowListener(this);
    
    blot = new Button("Green level is " + greenLevel);
    blot.setForeground(Color.white);
    blot.setBackground(new Color(0,greenLevel,0));
    add(blot, BorderLayout.NORTH);

    Panel moreOrLess = new Panel();
    moreOrLess.setLayout(new GridLayout(1,2));
    more = new Button("More Green");
    more.setBackground(Color.black);
    more.setForeground(Color.green);
    more.addActionListener(this);
    moreOrLess.add(more);
    less = new Button("Less Green");
    less.setBackground(Color.black);
    less.setForeground(new Color(0,160,0));
    less.addActionListener(this);
    moreOrLess.add(less);
    add(moreOrLess);
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(more)) {
      if (greenLevel < 255) {
        greenLevel += 5;
      }
    }
    else {
      if (greenLevel > 0) {
        greenLevel -= 5;
      }
    }
    blot.setBackground(new Color(0,greenLevel,0));
    blot.setLabel("Green level is " + blot.getBackground().getGreen());
  }

  public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
  }

  public void windowOpened(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
}

Notes:

  1. The sample declares three Button object references. blot is used to display the current level of green, more is used to increase the level of green, and less is used to decrease the level of green. The integer variable greenLevel will maintain the current level of green (from 0 to 255) and is initialized to 160.

  2. In the constructor, blot is instantiated with a white text color and the initial level of green for its background color. It is placed in the NORTH of the frame's BorderLayout. A Panel object is then constructed having a GridLayout to hold the more and less buttons. The buttons are instantiated (with foreground color, background color, and registered action listener) and added to the panel which is then added to the application frame.

  3. The actionPerformed() method determines which button (more or less) was clicked and adjusts the background color of blot either up or down accordingly. The label of blot is also changed to display the current level of green in its background color.

 

The Font class

Object 

\

Font 

Font myFont = new Font("Serif", Font.ITALIC, 24);

where the parameters specify the font name, font style, and point size.

Font.BOLD
Font.ITALIC
Font.PLAIN

To combine styles, code an expression such as  Font.BOLD + Font.ITALIC

Example:

import java.awt.*;
import java.awt.event.*;

public class App extends Frame implements WindowListener {

  Button b;

  public static void main(String[] args) {
    App myWindow = new App("Font sample");
    myWindow.setSize(250,100);
    myWindow.setVisible(true);
  }

  public App(String title) {
    super(title);
    addWindowListener(this);
    b = new Button("Click me");
    b.setBackground(Color.red);
    b.setForeground(Color.white);
    b.setFont(new Font("Monospaced", Font.PLAIN, 12));
    add(b);
    b.addActionListener(
      new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          b.setFont(new Font("SanSerif", Font.BOLD + Font.ITALIC, 48));
          b.setLabel("Ouch!!");
        }
      }
    );
  }

  public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
  }

  public void windowOpened(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
}

Notes:

  1. The sample declares a single Button object reference (b).

  2. In the constructor, b is instantiated with a red background color, a white text color, and a "Monospaced", plain, 10 point font. It is added to the CENTER of the frame's BorderLayout (both by default) and its ActionListener is both registered and defined using an anonymous inner interface. When using this technique, the interface isn't implemented in the class header. When the user clicks the button, its font is changed to 48 point, bold, italicized, "SanSerif" and its label is modified. A slight delay may be noticed as these operations are time consuming in Java.

 

Lab exercise for Ferris students

E-mail your answers to this assignment no later than the due date listed in the class schedule.

 

Review questions

  1. Assuming that all unseen code is correct and that the container has a foreground color of blue, what will result from attempting to compile and execute the following code with a Java 2 version 1.2 compiler? The line numbers are for reference purposes only.

1
2
3
Button blue = new Button("Am I blue?");
blue.setBackground(Color.BLUE);
add(blue);
  1. Compilation will fail at line 2

  2. Compilation will fail at line 3

  3. Compilation will succeed but a runtime error will occur

  4. Compilation will succeed. A button with the text "Am I blue?" and having white letters on a blue background will be added to the container.

  5. Compilation will succeed but the button's text will not be visible.

  1. If somePanel is a container, code a single statement to set its text color to a custom shade having maximum levels of red and blue, but no green.

  2. Which of the following will set a component's text to display in yellow, 18 point, bold, serif if using Java 2 version 1.2?

  1. setForeground(Color.YELLOW));
    setFont(new Font("Serif", Font.BOLD, 18));

  1. setForeground(Color.YELLOW));
    setFont(new Font("Serif", Font.bold, 18));

  2. setForeground(Color.yellow));
    setFont(new Font("Serif", Font.BOLD, 18));

  3. setForeground(Color.yellow));
    setFont(new Font("Serif", Font.bold, 18));

  4. setForeground(Color.yellow));
    setFont(new Font("Serif", BOLD, 18));

  1. What background color will result from the following?

setBackground(Color.gray.blue);

  1. The statement will not compile

  2. Compilation succeeds but a runtime error will occur

  3. A mix of gray and blue

  4. gray

  5. blue