|
|
|
1000 Java Tips ebook
|
|
 

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java
programming articles and tips. Please take your copy here.
Take your copy of free "Java Technology Screensaver"!. |
|
The Java Lesson 29: The Color and Font classes
|
JavaFAQ Home » Java Lessons by Jon Huhtala

Java Lesson 29 by
Jon Huhtala: The Color
and Font classes
The Color class
-
Encapsulates a particular color
by mixing specified levels of red, green, and blue (RGB) light
-
Is used extensively in graphical programming. The Color class makes it easy to specify the
background and foreground (text) colors of components and set the pen color to
be used in drawing lines and shapes.
| 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:
-
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.
-
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.
-
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
Font myFont = new Font("Serif", Font.ITALIC,
24);
where the parameters specify
the font name, font style, and point size.
-
Can use any font available on
the system. For compatibility, however, only "Serif", "SansSerif", and
"Monospaced" are recommended. These are roughly equivalent to the "Times
Roman", "Arial", and "Courier" fonts of Windows.
-
Has public fields for
specifying the font style. These can be referenced by coding
| Font.BOLD |
| Font.ITALIC |
| Font.PLAIN |
To combine styles, code an
expression such as Font.BOLD + Font.ITALIC
-
Provides for characters of any
size specified as an integer point size
-
Has many methods but few are
needed to satisfy standard programming requirements. For more detail, consult
the help facility of your IDE or the Java API documentation.
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("SansSerif", Font.BOLD + Font.ITALIC,
4 );
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:
-
The sample declares a single
Button object reference
(b).
-
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,
"SansSerif" 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
-
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); |
-
Compilation will fail at
line 2
-
Compilation will fail at
line 3
-
Compilation will succeed
but a runtime error will occur
-
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.
-
Compilation will succeed
but the button's text will not be visible.
-
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.
-
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?
-
setForeground(Color.YELLOW)); setFont(new Font("Serif", Font.BOLD,
1 );
-
setForeground(Color.YELLOW)); setFont(new Font("Serif", Font.bold,
1 );
-
setForeground(Color.yellow)); setFont(new Font("Serif", Font.BOLD,
1 );
-
setForeground(Color.yellow)); setFont(new Font("Serif", Font.bold,
1 );
-
setForeground(Color.yellow)); setFont(new Font("Serif", BOLD,
1 );
-
What background color will
result from the following?
setBackground(Color.gray.blue);
-
The statement will not
compile
-
Compilation succeeds but a
runtime error will occur
-
A mix of gray and blue
-
gray
-
blue
Printer Friendly Page
Send to a Friend
..
Search here again if you need more info!
|
|
|