AWT Color
AWT Color
Introduction
The Color class states colors in the default sRGB color space or colors in arbitrary color spaces
identified by a ColorSpace.
Class declaration
Following is the declaration for java.awt.Color class:
Field
Following are the fields for java.awt.geom.Arc2D class:
Class constructors
1
ColorColorSpacecspace, float[]components, floatalpha
Creates a color in the specified ColorSpace with the color components specified in the
float array and the specified alpha.
2
Colorfloatr, floatg, floatb
Creates an opaque sRGB color with the specified red, green, and blue values in the range
0.0 − 1.0.
3
Colorfloatr, floatg, floatb, floata
Creates an sRGB color with the specified red, green, blue, and alpha values in the range
0.0 − 1.0.
4
Colorintrgb
Creates an opaque sRGB 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.
5
Colorintrgba, booleanhasalpha
Creates an sRGB color with the specified combined RGBA value consisting of the alpha
component in bits 24-31, the red component in bits 16-23, the green component in bits 8-
15, and the blue component in bits 0-7.
6
Colorintr, intg, intb
Creates an opaque sRGB color with the specified red, green, and blue values in the range
0 − 255.
7
Colorintr, intg, intb, inta
Creates an sRGB color with the specified red, green, blue, and alpha values in the range
0 − 255.
Class methods
2
PaintContext createContext
ColorModelcm, Rectangler, Rectangle2Dr2d, AffineTransformxform, RenderingHintshints
3
Color darker
4
static Color decodeStringnm
5
boolean equalsObjectobj
6
int getAlpha
7
int getBlue
Returns the blue component in the range 0-255 in the default sRGB space.
8
static Color getColorStringnm
9
static Color getColorStringnm, Colorv
10
static Color getColorStringnm, intv
11
float[] getColorComponentsColorSpacecspace, float[]compArray
Returns a float array containing only the color components of the Color in the ColorSpace
specified by the cspace parameter.
12
float[] getColorComponentsfloat[]compArray
Returns a float array containing only the color components of the Color, in the
ColorSpace of the Color.
13
ColorSpace getColorSpace
14
float[] getComponentsColorSpacecspace, float[]compArray
Returns a float array containing the color and alpha components of the Color, in the
ColorSpace specified by the cspace parameter.
15
float[] getComponentsfloat[]compArray
Returns a float array containing the color and alpha components of the Color, in the
ColorSpace of the Color.
16
int getGreen
Returns the green component in the range 0-255 in the default sRGB space.
17
static Color getHSBColorfloath, floats, floatb
Creates a Color object based on the specified values for the HSB color model.
18
int getRed
Returns the red component in the range 0-255 in the default sRGB space.
19
int getRGB
Returns the RGB value representing the color in the default sRGB ColorModel.
20
float[] getRGBColorComponentsfloat[]compArray
Returns a float array containing only the color components of the Color, in the default
sRGB color space.
21
float[] getRGBComponentsfloat[]compArray
Returns a float array containing the color and alpha components of the Color, as
represented in the default sRGB color space.
22
int getTransparency
23
int hashCode
Computes the hash code for this Color.
24
static int HSBtoRGBfloathue, floatsaturation, floatbrightness
Converts the components of a color, as specified by the HSB model, to an equivalent set
of values for the default RGB model.
25
static float[] RGBtoHSBintr, intg, intb, float[]hsbvals
26
String toString
Methods inherited
This class inherits methods from the following classes:
java.lang.Object
Color Example
Create the following java program using any editor of your choice in say D:/ > AWT > com >
tutorialspoint > gui >
AWTGraphicsDemo.java
package com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public AWTGraphicsDemo(){
super("Java AWT Examples");
prepareGUI();
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Font plainFont = new Font("Serif", Font.PLAIN, 24);
g2.setFont(plainFont);
g2.setColor(Color.red);
g2.drawString("Welcome to TutorialsPoint", 50, 70);
g2.setColor(Color.GRAY);
g2.drawString("Welcome to TutorialsPoint", 50, 120);
}
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following
command.
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
Loading [MathJax]/jax/output/HTML-CSS/jax.js