Method in the Color Class That Returns the Blue Component Java Word Search
Java AWT | Color Class
The Color class is a part of Java Abstract Window Toolkit(AWT) package. The Color class creates color by using the given RGBA values where RGBA stands for RED, GREEN, BLUE, ALPHA or using HSB value where HSB stands for HUE, SATURATION, BRIcomponents. The value for individual components RGBA ranges from 0 to 255 or 0.0 to 0.1. The value of alpha determines the opacity of the color, where 0 or 0.0 stands fully transparent and 255 or 1.0 stands opaque.
Constructors of Color Class
- Color(ColorSpace c, float[] co, float a) : Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha.
- Color(float r, float g, float b) : creates a opaque color with specified RGB components(values are in range 0.0 – 0.1)
- Color(float r, float g, float b, float a) : creates a color with specified RGBA components(values are in range 0.0 – 0.1)
- Color(int rgb): Creates an opaque RGB 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.
- Color(int rgba, boolean b): 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. - Color(int r, int g, int b) : Creates a opaque color with specified RGB components(values are in range 0 – 255)
- Color(int r, int g, int b, int a) : Creates a color with specified RGBA components(values are in range 0 – 255)
Commonly Used Methods In Color Class
method | explanation |
---|---|
brighter() | creates a new Color that is a brighter version of this Color. |
createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform x, RenderingHints h) | creates and returns a PaintContext used to generate a solid color field pattern. |
darker() /td> | creates a new Color that is a darker version of this Color. |
decode(String nm) | converts a String to an integer and returns the specified opaque Color. |
equals(Object obj) | determines whether another Color object is equal to this Color. |
getAlpha() | returns the alpha component in the range 0-255. |
getBlue() | returns the blue component in the range 0-255 . |
getColor(String nm) | Finds a color in the system properties. |
getColor(String nm, Color v) | Finds a color in the system properties. |
getColor(String nm, int v) | Finds a color in the system properties. |
getColorComponents(ColorSpace cspace, float[] compArray) | returns a float array containing only the color components of the Color in the ColorSpace specified by the cspace parameter. |
getColorComponents(float[] compArray) | returns a float array containing only the color components of the Color, in the ColorSpace of the Color. |
getColorSpace() | returns the ColorSpace of this Color. |
getGreen() | returns the green component in the range 0-255 in the default sRGB space. |
getRed() | returns the red component in the range 0-255 in the default sRGB space. |
getRGB() | Returns the RGB value representing the color in the default sRGB ColorModel. |
getHSBColor(float h, float s, float b) | Creates a Color object based on the specified values for the HSB color model. |
getTransparency() | returns the transparency mode for this Color. |
hashCode() | computes the hash code for this Color. |
HSBtoRGB(float h, float s, float b) | Converts the HSB value to RGB value |
RGBtoHSB(int r, int g, int b, float[] hsbvals) | converts the RGB value to HSB value |
Below programs illustrate the Color class in Java AWT :
- Program to set the background color of panel using the color specified in the constants of the class
Java
import
java.awt.*;
import
javax.swing.*;
class
color
extends
JFrame {
color()
{
super
(
"color"
);
Color c = Color.yellow;
JPanel p =
new
JPanel();
p.setBackground(c);
setSize(
200
,
200
);
add(p);
show();
}
public
static
void
main(String args[])
{
color c =
new
color();
}
}
Output :
- Program to create a new Color by stating the RGB value and set it as background of panel
Java
import
java.awt.*;
import
javax.swing.*;
class
color
extends
JFrame {
color()
{
super
(
"color"
);
Color c =
new
Color(
255
,
255
,
0
);
JPanel p =
new
JPanel();
p.setBackground(c);
setSize(
200
,
200
);
add(p);
show();
}
public
static
void
main(String args[])
{
color c =
new
color();
}
}
Output :
- Program to create a new Color by stating the RGB value and alpha value, set it as background of panel
Java
import
java.awt.*;
import
javax.swing.*;
class
color
extends
JFrame {
color()
{
super
(
"color"
);
Color c =
new
Color(
255
,
0
,
0
,
100
);
JPanel p =
new
JPanel();
p.setBackground(c);
setSize(
200
,
200
);
add(p);
show();
}
public
static
void
main(String args[])
{
color c =
new
color();
}
}
Output :
- Program to create a new Color by using Color(int rgb) method, set it as background of panel
Java
import
java.awt.*;
import
javax.swing.*;
class
color
extends
JFrame {
color()
{
super
(
"color"
);
Color c =
new
Color(
255
);
JPanel p =
new
JPanel();
p.setBackground(c);
setSize(
200
,
200
);
add(p);
show();
}
public
static
void
main(String args[])
{
color c =
new
color();
}
}
Output :
- Program to take RGBA value from user and set it as background of panel
Java
import
java.awt.*;
import
javax.swing.*;
import
java.awt.event.*;
class
color
extends
JFrame
implements
ActionListener {
JTextField R, G, B, A;
JPanel p;
color()
{
super
(
"color"
);
R =
new
JTextField(
3
);
G =
new
JTextField(
3
);
B =
new
JTextField(
3
);
A =
new
JTextField(
3
);
JLabel l =
new
JLabel(
"Red= "
);
JLabel l1 =
new
JLabel(
"Green= "
);
JLabel l2 =
new
JLabel(
"Blue= "
);
JLabel l3 =
new
JLabel(
"Alpha= "
);
p =
new
JPanel();
JButton b =
new
JButton(
"ok"
);
JButton b1 =
new
JButton(
"brighter"
);
JButton b2 =
new
JButton(
"Darker"
);
b.addActionListener(
this
);
b2.addActionListener(
this
);
b1.addActionListener(
this
);
p.add(l);
p.add(R);
p.add(l1);
p.add(G);
p.add(l2);
p.add(B);
p.add(l3);
p.add(A);
p.add(b);
p.add(b1);
p.add(b2);
setSize(
200
,
200
);
add(p);
show();
}
public
void
actionPerformed(ActionEvent evt)
{
String s = evt.getActionCommand();
if
(s.equals(
"ok"
)) {
int
r, g, b, a;
r = Integer.parseInt(R.getText());
g = Integer.parseInt(G.getText());
b = Integer.parseInt(B.getText());
a = Integer.parseInt(A.getText());
Color c =
new
Color(r, g, b, a);
p.setBackground(c);
}
else
if
(s.equals(
"brighter"
)) {
Color c = p.getBackground();
c = c.brighter();
p.setBackground(c);
}
else
{
Color c = p.getBackground();
c = c.darker();
p.setBackground(c);
}
}
public
static
void
main(String args[])
{
color c =
new
color();
}
}
Output :
- Program to take HSB value from user and set it as background of panel
Java
import
java.awt.*;
import
javax.swing.*;
import
java.awt.event.*;
class
color
extends
JFrame
implements
ActionListener {
JTextField H, S, B;
JPanel p;
color()
{
super
(
"color"
);
H =
new
JTextField(
3
);
S =
new
JTextField(
3
);
B =
new
JTextField(
3
);
JLabel l =
new
JLabel(
"Hue= "
);
JLabel l1 =
new
JLabel(
"Saturation= "
);
JLabel l2 =
new
JLabel(
"Brightness= "
);
p =
new
JPanel();
JButton b =
new
JButton(
"ok"
);
JButton b1 =
new
JButton(
"brighter"
);
JButton b2 =
new
JButton(
"Darker"
);
b.addActionListener(
this
);
b2.addActionListener(
this
);
b1.addActionListener(
this
);
p.add(l);
p.add(H);
p.add(l1);
p.add(S);
p.add(l2);
p.add(B);
p.add(b);
p.add(b1);
p.add(b2);
setSize(
200
,
200
);
add(p);
show();
}
public
void
actionPerformed(ActionEvent evt)
{
String st = evt.getActionCommand();
if
(st.equals(
"ok"
)) {
float
h, s, b;
h = Float.parseFloat(H.getText());
s = Float.parseFloat(S.getText());
b = Float.parseFloat(B.getText());
Color c = Color.getHSBColor(h, s, b);
p.setBackground(c);
}
else
if
(st.equals(
"brighter"
)) {
Color c = p.getBackground();
c = c.brighter();
p.setBackground(c);
}
else
{
Color c = p.getBackground();
c = c.darker();
p.setBackground(c);
}
}
public
static
void
main(String args[])
{
color c =
new
color();
}
}
Output :
Reference : https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html
jenkinsbaccaustone1995.blogspot.com
Source: https://www.geeksforgeeks.org/java-awt-color-class/
Enviar um comentário for "Method in the Color Class That Returns the Blue Component Java Word Search"