Working With Colors and Fonts -


Working with Colors -

AWT provides java.awt.Color class that enables to specify and work with different colors.
Color class defines some constant to specify a number of common colors (for example - Color.RED/Color.red, Color.GREEN/ Color.green, Color.PINK/Color.pink etc).
Note - Color class supports both uppercase and lowercase color constants.
Color class also have several constructors to specify your own colors. Some popular one is as follows -

1- public Color(int r, int g, int b ) -

where r, g, b indicates the red, green and blue colors whose values must be between 0 and 255.
if r, g or b are outside of the range 0 to 255 it will throw an IllegalArgumentException.

Example -

Color c1 = new Color(0, 0, 0);  // Black color
Color c2 = new Color(255, 255, 255);   //White Color
Color c3 = new Color(100, 0 ,0);   // Light red color

 

2- public Color(float r, float g, float b) -

where r, g, b indicates the red, green and blue colors whose values must be between 0.0 and 1.0.
if r, g or b are outside of the range 0.0 to 1.0, it will throw an IllegalArgumentException .

Example -

Color c1 = new Color(0.0f, 0.0f, 0.0f);    // Black color 
Color c2 = new Color(1.0f, 1.0f, 1.0f);    // White color
Color c3 = new Color(0.5f, 0.0f, 0.0f);    //Light red color

3- public Color(int rgb) -

It will creates a color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

Example -

Color c = new Color(0xff0000);  // Red color
Color c = new Color(0x00ff00);  //Green color
Color c = new Color(0x0000ff);   // Blue color


Setting the current graphics color -

You can change the color of graphics you are drawing by using Graphics class setColor() method. It has the following syntax -
public abstract void setColor(Color c) 
Where c is new rendering color.

Example -

import java.awt.*;
import java.applet.*;
public class Test extends Applet
{
    public void paint(Graphics g)
    {
       Color c1 = new Color(255,100,50);
       Color c2 = new Color(50,100,255);
      
       g.setColor(c1);
       g.drawLine(10,10,100,10);
       g.drawRect(20,20,100,100);
       
       g.setColor(c2);
       g.drawLine(30,30,100,30);
       g.drawRect(40,40,100,100);
    }    
}

Output -



Working with Fonts -

You can select the font style of your choice by using Font class of java.awt package. Font class support the following constructor to create a new font -
public Font(String name, int style, int size) 
Where -
name specifies the font name. The font name can be a font face name or a font family name.
Font style can have one of the following three values ? Font.BOLD, Font.PLAIN, Font.ITALIC. We can also combine these styles. For example if we want bold and italic then we can combine Font.BOLD + Font.ITALIC.
size specifies the font size.

Using Fonts -

After creating new font, you can use it by using setFont() method.It has the following syntax -
public abstract void setFont(Font fontObj)
Where fontObj specifies the new font you cerated.

Example -

import java.awt.*;
import java.applet.*;
public class Test extends Applet
{
    public void paint(Graphics g)
    {
       Font f1 = new Font("Dialog",Font.BOLD + Font.ITALIC, 35);
        g.setFont(f1);
        g.drawString("This is juat a demo", 100, 100);  
    }    
}

Output -