COLORS IN SWING
COLORS IN SWING
the color class encapsulates colors in the default sRGB color space or colors in the arbitrary color spaces identified by a ColorSpace class. Every color has an implicit alpha value of 1.0 or an explicit one provided in the constructor, which alpha values denote the color's transparency and 1.0 means totally transparent if denoted with a float range or 255 if an int range. Also alpha values of 0 or 0.0 means completely transparent color. Note that color classes do not pre-multiply alpha values.
Information on the sRGB color space is defined by the World Wide Web consortium
the overloaded constructors for color take a colorspace parameter, float or int values. Check this out in the api.
Like the font accessor methods, getXX() methods are specified for the color class but in modifying any color instance, one is usually expected to create a new one. By specifying an alpha value, we can use a color instance on a component's background to make it transparent. Note that when setting the foreground of a component the look and feel may ignore the color specified.
Our button and textareas colored with Color.
package tests;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Property extends JPanel{
//declaring an instance
static Color dColor = new Color(0, 255, 100, 125);
static JButton jButton1 = new JButton(" ");
static JTextArea jText;
/** Creates a new instance of Property */
public Property() {
jButton1 = new JButton("click me");
add(jButton1);
//we'll use the instance as foreground.
jButton1.setBackground(dColor);
jText = new JTextArea(" ", 10, 20);
add(jText);
GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = genv.getAvailableFontFamilyNames();
for (int i=0; i
}
}
private static void dButton(){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Property");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Property nProp = new Property();
nProp.setOpaque(true);
//and also use it here too
nProp.setBackground(dColor);
frame.setContentPane(nProp);
frame.pack();
frame.setVisible(true);
}
public static void simplyText(String txt){
jText.append(txt+"\n");
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
dButton();
}
});
}
}
keywords: color, swing, colorspace, alpha value, sRGB, GraphicsEnvironment
No comments:
Post a Comment