Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.5. Colors and fonts

5.5. Colors and fonts
Last updated: 25 January 2013.

This tutorial explains how to use colors and fonts in Java.


5.5.1. Colors

A color in Java is a standard RGB color, that is, a mixture of red, green and blue shades. The class Color provides several constructors including Color(int r, int g, int b) that allows you to create a color with the specified red, green and blue shades ranging from 0 to 255. The RGB value (0, 0, 0) is the color black and the value (255, 255, 255) is white. You can use the values in between as necessary.

The color red is (255, 0, 0), green is (0, 255, 0) and blue is (0, 0, 255). You can get the color gray with a mixture of 3 shades that have the same values like (100, 100, 100). Comparatively, (50, 50, 50) is a darker gray and (200, 200, 200) a lighter gray.

Many constant fields that define colors are provided by the class Color, such as BLACK, BLUE, CYAN and so on. As an example, you can get an instance of the color blue in 2 ways as shown in the following statements:

Color blue = new Color(0, 0, 255);
Color alsoBlue = Color.BLUE;

You can set the background of a Swing component (like JPanel) by calling the method setBackground(Color c) provided by the class Component. The sample shown below creates a JPanel object and sets its background to the color blue:

JPanel panel = new JPanel();
panel.setBackground
(Color.BLUE);

You can also set the foreground of certain components by calling the method setForeground(Color c). The next sample creates a button and sets its foreground to the color red:

JButton button = new JButton("Button");
button.setForeground
(Color.RED);


5.5.2. Fonts

You can set the font of a component or container to the desired value by invoking the method setFont(Font font). The expected argument is an instance of the class Font. A Font instance can be created by calling the constructor Font(String name, int style, int size). For example, you can set the font of a button to Times New Roman like this:

JButton button = new JButton("Button");
button.setFont
(new Font("Times New Roman", Font.PLAIN, 15));

The possible values for the argument style are: Font.PLAIN, Font.ITALIC, Font.BOLD and (Font.ITALIC | Font.BOLD).

The argument name is the font name. You can get the list of available font names on your system by calling the method getAvailableFontFamilyNames provided by the class GraphicsEnvironment. The following sample prints to the standard output the names of the available fonts:

String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for(String name : fontNames)
   
System.out.println(name);


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.5. Colors and fonts
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.6. Look and Feel

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7