Java by API/java.awt/Color
Содержание
- 1 Color: equals(Object obj)
- 2 Color: getComponents(float[] compArray)
- 3 Color: getRGB()
- 4 Color.GRAY
- 5 Color: HSBtoRGB(float hue,float saturation,float brightness)
- 6 Color.lightGray
- 7 Color.magenta
- 8 Color.pink
- 9 Color: RGBtoHSB(int r, int g, int b, float[] hsbvals)
- 10 new Color(int rgb)
- 11 new Color(int r, int g, int b)
Color: equals(Object obj)
import java.awt.Color;
public class Main {
public static void main(String[] a) {
Color myBlack = new Color(0, 0, 0); // Color black
Color myWhite = new Color(255, 255, 255); // Color white
System.out.println(myBlack.equals(myWhite));
}
}
Color: getComponents(float[] compArray)
import java.awt.Color;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
public class Main {
public static void main(String[] args) {
byte ff = (byte) 0xff;
byte[] r = { ff, 0, 0, ff, 0 };
byte[] g = { 0, ff, 0, ff, 0 };
byte[] b = { 0, 0, ff, ff, 0 };
ColorModel cm = new IndexColorModel(3, 5, r, g, b);
Color[] colors = { new Color(255, 0, 0), new Color(0, 255, 0),
new Color(0, 0, 255), new Color(64, 255, 64),
new Color(255, 255, 0), new Color(0, 255, 255) };
for (int i = 0; i < colors.length; i++) {
float[] normalizedComponents = colors[i].getComponents(null);
int[] unnormalizedComponents = cm.getUnnormalizedComponents(
normalizedComponents, 0, null, 0);
int rgb = colors[i].getRGB();
Object pixel = cm.getDataElements(rgb, null);
System.out.println(colors[i] + " -> " + ((byte[]) pixel)[0]);
}
for (byte i = 0; i < 5; i++) {
int[] unnormalizedComponents = cm.getComponents(i, null, 0);
System.out.print(i + " -> unnormalized components");
for (int j = 0; j < unnormalizedComponents.length; j++)
System.out.print(" " + unnormalizedComponents[j]);
System.out.println();
}
}
}
Color: getRGB()
import java.awt.Color;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
public class MainClass {
public static void main(String[] args) throws Exception {
byte ff = (byte) 0xff;
byte[] r = { ff, 0, 0, ff, 0 };
byte[] g = { 0, ff, 0, ff, 0 };
byte[] b = { 0, 0, ff, ff, 0 };
ColorModel cm = new IndexColorModel(3, 5, r, g, b);
Color[] colors = { new Color(255, 0, 0), new Color(0, 255, 0), new Color(0, 0, 255)};
for (int i = 0; i < colors.length; i++) {
int rgb = colors[i].getRGB();
Object pixel = cm.getDataElements(rgb, null);
System.out.println(colors[i] + " -> " + ((byte[]) pixel)[0]);
}
for (byte i = 0; i < 5; i++) {
int[] unnormalizedComponents = cm.getComponents(i, null, 0);
System.out.print(i + " -> unnormalized components");
for (int j = 0; j < unnormalizedComponents.length; j++)
System.out.print(" " + unnormalizedComponents[j]);
System.out.println();
}
}
}
Color.GRAY
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
frame.setTitle("JLabel Test");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("First Name");
label.setFont(new Font("Courier New", Font.ITALIC, 12));
label.setForeground(Color.GRAY);
frame.add(label);
frame.pack();
frame.setVisible(true);
}
}
Color: HSBtoRGB(float hue,float saturation,float brightness)
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
int n = 3;
int xdata[] = new int[n];
int ydata[] = new int[n];
xdata[0] = 50;
ydata[0] = 150;
xdata[1] = 200;
ydata[1] = 50;
xdata[2] = 350;
ydata[2] = 150;
int rgb = Color.HSBtoRGB(1.0f, 1.0f, 1.0f);
g.setColor(new Color(rgb));
g.fillPolygon(xdata, ydata, n);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Color.lightGray
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
Color colors[] = { Color.black, Color.blue, Color.cyan, Color.darkGray,
Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange,
Color.pink, Color.red, Color.white, Color.yellow };
public void paint(Graphics g) {
int deltax = 260 / colors.length;
for (int i = 0; i < colors.length; i++) {
g.setColor(colors[i]);
g.fillRect(i * deltax, 0, (i + 1) * deltax, 260);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Color.magenta
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
Color colors[] = { Color.black, Color.blue, Color.cyan, Color.darkGray,
Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange,
Color.pink, Color.red, Color.white, Color.yellow };
public void paint(Graphics g) {
int deltax = 260 / colors.length;
for (int i = 0; i < colors.length; i++) {
g.setColor(colors[i]);
g.fillRect(i * deltax, 0, (i + 1) * deltax, 260);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Color.pink
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
Color colors[] = { Color.black, Color.blue, Color.cyan, Color.darkGray,
Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange,
Color.pink, Color.red, Color.white, Color.yellow };
public void paint(Graphics g) {
int deltax = 260 / colors.length;
for (int i = 0; i < colors.length; i++) {
g.setColor(colors[i]);
g.fillRect(i * deltax, 0, (i + 1) * deltax, 260);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Color: RGBtoHSB(int r, int g, int b, float[] hsbvals)
import java.awt.Color;
public class Main {
public static void main() {
int red = 23;
int green = 66;
int blue = 99;
float[] hsb = Color.RGBtoHSB(red, green, blue, null);
float hue = hsb[0];
float saturation = hsb[1];
float brightness = hsb[2];
}
}
new Color(int rgb)
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
Color[] colors = new Color[24];
public MyCanvas() {
int rgb = 0xff;
for (int i = 0; i < 24; i++) {
colors[i] = new Color(rgb);
rgb <<= 1;
if ((rgb & 0x1000000) != 0) {
rgb |= 1;
rgb &= 0xffffff;
}
}
}
public void paint(Graphics g) {
int i = 10;
for (Color c : colors) {
g.setColor(c);
i += 10;
g.drawLine(i, 10, 200, 200);
}
}
}
public class Main {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
}
}
new Color(int r, int g, int b)
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
JFrame f = new JFrame("JColorChooser Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton("Pick to Change Background");
Color myBlack = new Color(0,0,0); // Color black
// Color myWhite = new Color(255,255,255); // Color white
// Color myGreen = new Color(0,200,0); // A shade of green
button.setBackground(myBlack);
f.add(button, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}