63 lines
1.6 KiB
Java
63 lines
1.6 KiB
Java
import java.awt.*;
|
|
import javax.swing.*;
|
|
import java.awt.event.*;
|
|
|
|
class Observer implements ActionListener {
|
|
boolean shouldY, shouldC, shouldM;
|
|
|
|
public Observer() {
|
|
shouldY = false;
|
|
shouldC = false;
|
|
shouldM = false;
|
|
}
|
|
|
|
private void updateColor(JPanel panel) {
|
|
int r, g, b;
|
|
|
|
r = shouldC ? 0 : 255;
|
|
g = shouldM ? 0 : 255;
|
|
b = shouldY ? 0 : 255;
|
|
|
|
panel.setBackground(new Color(r, g ,b));
|
|
}
|
|
|
|
public void actionPerformed(ActionEvent evt) {
|
|
String name = evt.getActionCommand();
|
|
JRadioButton radioButton = (JRadioButton)evt.getSource();
|
|
JPanel panel = (JPanel)radioButton.getParent();
|
|
|
|
boolean selected = radioButton.isSelected();
|
|
|
|
if (name == "Jaune") shouldY = selected;
|
|
else if (name == "Magenta") shouldM = selected;
|
|
else if (name == "Cyan") shouldC = selected;
|
|
|
|
updateColor(panel);
|
|
}
|
|
}
|
|
|
|
public class Combinaison {
|
|
|
|
public static void main(String[] args) {
|
|
JFrame f = new JFrame("Fond");
|
|
f.setSize(200, 200);
|
|
f.setLocation(100, 100);
|
|
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
Observer observer = new Observer();
|
|
JRadioButton b1 = new JRadioButton("Jaune");
|
|
JRadioButton b2 = new JRadioButton("Cyan");
|
|
JRadioButton b3 = new JRadioButton("Magenta");
|
|
|
|
b1.addActionListener(observer);
|
|
b2.addActionListener(observer);
|
|
b3.addActionListener(observer);
|
|
|
|
JPanel p = new JPanel();
|
|
f.add(p);
|
|
p.add(b1);
|
|
p.add(b2);
|
|
p.add(b3);
|
|
f.setVisible(true);
|
|
}
|
|
} |