Codice Html
<applet code="CambiaColore2.class" width="400" height="350">...</applet>
Codice Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CambiaColore2 extends JApplet
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 JPanel paNORD, paSUD;
private JComboBox colori;
private JButton cambia, cambia2;
private Color sfondo;
private Canvas tela;
private JLabel tAlphaf, tAlpha, tAlphaH,
tBluef, tBlue, tBlueH,
tGreenf, tGreen, tGreenH,
tRedf, tRed, tRedH,
tRGB, tRGBH,
tsRGB, tsRGBH;
public void init()
{
colori=new JComboBox(); colori.setEditable(false);
for(int i=0; i < sCOLORI.length; i++)
colori.addItem(sCOLORI[i]);
tela=new Canvas(); sfondo=cCOLORI[0];
tela.setBackground(sfondo);
cambia =new JButton("Cambia"); cambia.addActionListener(this);
cambia2=new JButton("Bianco"); cambia2.addActionListener(this);
paSUD=new JPanel(); paSUD.add(new JLabel("Colore sfondo", Label.RIGHT));
paSUD.add(colori);
paSUD.add(cambia);
paSUD.add(cambia2);
paNORD=new JPanel(new GridLayout(6, 4));
tRedf=new JLabel("", Label.CENTER); paNORD.add(tRedf);
tRed =new JLabel("", Label.CENTER); paNORD.add(tRed);
tRedH=new JLabel("", Label.CENTER); paNORD.add(tRedH);
paNORD.add(new JLabel("RED"));
tGreenf=new JLabel("", Label.CENTER); paNORD.add(tGreenf);
tGreen =new JLabel("", Label.CENTER); paNORD.add(tGreen);
tGreenH=new JLabel("", Label.CENTER); paNORD.add(tGreenH);
paNORD.add(new JLabel("GREEN"));
tBluef=new JLabel("", Label.CENTER); paNORD.add(tBluef);
tBlue =new JLabel("", Label.CENTER); paNORD.add(tBlue);
tBlueH=new JLabel("", Label.CENTER); paNORD.add(tBlueH);
paNORD.add(new JLabel("BLUE"));
paNORD.add(new JLabel());
tRGB =new JLabel("", Label.CENTER); paNORD.add(tRGB);
tRGBH=new JLabel("", Label.CENTER); paNORD.add(tRGBH);
paNORD.add(new JLabel("RGB"));
tAlphaf=new JLabel("", Label.CENTER); paNORD.add(tAlphaf);
tAlpha =new JLabel("", Label.CENTER); paNORD.add(tAlpha);
tAlphaH=new JLabel("", Label.CENTER); paNORD.add(tAlphaH);
paNORD.add(new JLabel("ALPHA"));
paNORD.add(new JLabel());
tsRGB =new JLabel("", Label.CENTER); paNORD.add(tsRGB);
tsRGBH=new JLabel("", Label.CENTER); paNORD.add(tsRGBH);
paNORD.add(new JLabel("sRGB"));
setta();
this.getContentPane().add(paNORD, BorderLayout.NORTH);
this.getContentPane().add(tela, BorderLayout.CENTER);
this.getContentPane().add(paSUD, BorderLayout.SOUTH);
}
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();
}
} |