In alto compare una tabella di decodifica RGB del colore scelto import java.awt.*;
import java.awt.event.*;
public class CambiaColore2 extends Frame
implements ActionListener
{
private Color[] cCOLORI={
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 };
private String[] sCOLORI={
"Nero" , "Blu" , "Azzurro" ,
"Grigio scuro" , "Grigio" , "Verde" ,
"Grigio chiaro", "Magenta" , "Arancione" ,
"Rosa", "Rosso", "Bianco" ,
"Giallo" };
private Panel paNORD, paSUD;
private Choice colori;
private Button cambia, cambia2;
private Color sfondo;
private Canvas tela;
private Label tAlphaf, tAlpha, tAlphaH,
tBluef, tBlue, tBlueH,
tGreenf, tGreen, tGreenH,
tRedf, tRed, tRedH,
tRGB, tRGBH,
tsRGB, tsRGBH;
public CambiaColore2()
{
colori=new Choice(); for(int i=0; i < sCOLORI.length; i++)
colori.add(sCOLORI[i]);
tela=new Canvas(); sfondo=cCOLORI[0];
tela.setBackground(sfondo);
cambia =new Button("Cambia"); cambia.addActionListener(this);
cambia2=new Button("Bianco"); cambia2.addActionListener(this);
paSUD=new Panel(); paSUD.add(new Label("Colore sfondo", Label.RIGHT));
paSUD.add(colori);
paSUD.add(cambia);
paSUD.add(cambia2);
paNORD=new Panel(new GridLayout(6, 4));
tRedf=new Label("", Label.CENTER); paNORD.add(tRedf);
tRed =new Label("", Label.CENTER); paNORD.add(tRed);
tRedH=new Label("", Label.CENTER); paNORD.add(tRedH);
paNORD.add(new Label("RED"));
tGreenf=new Label("", Label.CENTER); paNORD.add(tGreenf);
tGreen =new Label("", Label.CENTER); paNORD.add(tGreen);
tGreenH=new Label("", Label.CENTER); paNORD.add(tGreenH);
paNORD.add(new Label("GREEN"));
tBluef=new Label("", Label.CENTER); paNORD.add(tBluef);
tBlue =new Label("", Label.CENTER); paNORD.add(tBlue);
tBlueH=new Label("", Label.CENTER); paNORD.add(tBlueH);
paNORD.add(new Label("BLUE"));
paNORD.add(new Label());
tRGB =new Label("", Label.CENTER); paNORD.add(tRGB);
tRGBH=new Label("", Label.CENTER); paNORD.add(tRGBH);
paNORD.add(new Label("RGB"));
tAlphaf=new Label("", Label.CENTER); paNORD.add(tAlphaf);
tAlpha =new Label("", Label.CENTER); paNORD.add(tAlpha);
tAlphaH=new Label("", Label.CENTER); paNORD.add(tAlphaH);
paNORD.add(new Label("ALPHA"));
paNORD.add(new Label());
tsRGB =new Label("", Label.CENTER); paNORD.add(tsRGB);
tsRGBH=new Label("", Label.CENTER); paNORD.add(tsRGBH);
paNORD.add(new Label("sRGB"));
setta();
this.add(paNORD, BorderLayout.NORTH);
this.add(tela, BorderLayout.CENTER);
this.add(paSUD, BorderLayout.SOUTH);
this.setSize(400, 350);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String nome=e.getActionCommand();
if(nome.equals("Cambia"))
{
int scelta=colori.getSelectedIndex();
sfondo=cCOLORI[scelta];
}
else if(nome.equals("Bianco"))
sfondo=Color.white;
tela.setBackground(sfondo);
setta()
}
private void setta()
{
int Alpha=sfondo.getAlpha(); tAlpha.setText("" + Alpha);
tAlphaH.setText(Integer.toHexString(Alpha).toUpperCase());
int Blue =sfondo.getBlue(); tBlue.setText("" + Blue);
tBlueH.setText(Integer.toHexString(Blue).toUpperCase());
int Green=sfondo.getGreen(); tGreen.setText("" + Green);
tGreenH.setText(Integer.toHexString(Green).toUpperCase());
int Red =sfondo.getRed(); tRed.setText("" + Red);
tRedH.setText(Integer.toHexString(Red).toUpperCase());
int sRGB =sfondo.getRGB(); tsRGB.setText("" + sRGB);
tsRGBH.setText(Integer.toHexString(sRGB).toUpperCase());
int RGB =sRGB & 0x00FFFFFF; tRGB.setText("" + RGB);
tRGBH.setText(Integer.toHexString(RGB).toUpperCase());
float[] RGBA=sfondo.getRGBComponents(null);
tRedf.setText("" + RGBA[0]);
tGreenf.setText("" + RGBA[1]);
tBluef.setText("" + RGBA[2]);
tAlphaf.setText("" + RGBA[3]);
}
public static void main(String args[])
{
CambiaColore2 cc=new CambiaColore2();
}
}
Osserva
-
setta()
getAlpha(), getBlue(), getGreen(), getRed(), getRGB(), getRGBComponents()
toHexString(), toUpperCase()
|