Программа создания цвета
Листинг 12.5. Программа создания цвета
import j ava.awt.*;
import j ava.awt.event.*;
class ScrollTestl extends Frame{
private Scroiibar
sbRed = new Scroiibar(Scroiibar.VERTICAL, 127, 16, 0, 271),
sbGreen = new Scroiibar(Scroiibar.VERTICAL, 127, 16, 0, 271),
sbBlue = new Scroiibar(Scroiibar.VERTICAL, 127, 16, 0, 271);
private Color с = new Color(127, 127, 127);
private Label 1m = new Label();
private Button
b1= new Button("Применить"),
b2 = new Button("Отменить");
ScrollTestl(String s){
super(s);
setLayout(null);
setFont(new Font("Serif", Font.BOLD, 15));
Panel p = new Panel();
p.setLayout(null);
p.setBounds(10,50, 150, 260);
add(p);
Label Ic = new Label("Подберите цвет");
lc.setBounds(20, 0, 120, 30);
p.add(lc);
Label Imin = new Label("0", Label.RIGHT);
lmin.setBounds(0, 30, 30, 30);
p.add(lmin);
Label Middle = new Label("127", Label.RIGHT);
lmiddle.setBounds(0, 120, 30, 30);
p.add(Imiddle);
Label Iroax = new Label("255", Label.RIGHT);
Imax.setBoundsfO, 200, 30, 30);
p.add(lraax);
sbRed.setBackground(Color.red);
sbRed.setBounds(40, 30, 20, 200);
p.add(sbRed);
sbRed.addAdjustmentListener(new ChColorO);
sbGreen.setBackground(Color.green);
sbGreen.setBounds(70, 30, 20, 200);
p.add(sbGreen);
sbGreen.addAdjustmentListener(new ChColor());
sbBlue.setBackground(Color.blue);
sbBlue.setBoundsds (100, 30, 20, 200);
p.add(sbBlue);
sbBlue.addAdjustmentListener(new ChColor());
Label Ip = new Label("Образец:");
lp.setBoundS(250, 50, 120, 30);
add(lp);
1m.setBackground(new Color(127, 127, 127));
Im.setBounds(220, 80, 120, 80);
add(lm);
bl.setBounds(240, 200, 100, 30);
add(bl);
bl.addActionListener(new ApplyColor());
b2.setBounds(240, 240, 100, 30);
add(b2);
b2.addActionListener(new CancelColor());
setSize(400, 300);
setVisible(true);
)
class ChColor implements AdjustmentListener{
public void adjustmentValueChanged(AdjustmentEvent e){
int red = с.getRed(), green = с.getGreen(), blue = с.getBlue();
if (e.getAdjustable() == sbRed) red = e.getValue();
else if (e.getAdjustablet) == sbGreen) green = e.getValue();
else if (e.getAdjustable() == sbBlue) blue = e.getValue();
с = new Color(red, green, blue);
lm.setBackground(c);
}
}
class ApplyColor implements ActionListener {
public void actionPerformed(ActionEvent ae){
setBackground(c);
}
}
class CancelColor implements ActionListener {
public void actionPerformed(ActionEvent ae){
с = new Color(127, 127, 127);
sbRed.setValue(127);
sbGreen.setValue(127);
sbBlue.setValue(127);
lm.setBackground(c);
setBackground(Color.white);
}
}
public static void main(String[] args){
Frame f = new ScrollTestl(" Выбор цвета");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
System.exit(0);
}
});
}
}