APL/APL2.1/TP10/Combinaison/Combinaison.java

63 lines
1.6 KiB
Java
Raw Normal View History

2022-03-21 17:26:34 +01:00
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Observer implements ActionListener {
2022-03-22 16:56:37 +01:00
boolean shouldY, shouldC, shouldM;
2022-03-21 17:26:34 +01:00
public Observer() {
2022-03-22 16:56:37 +01:00
shouldY = false;
shouldC = false;
shouldM = false;
}
private void updateColor(JPanel panel) {
int r, g, b;
2022-03-21 17:26:34 +01:00
2022-03-22 16:56:37 +01:00
r = shouldC ? 0 : 255;
g = shouldM ? 0 : 255;
b = shouldY ? 0 : 255;
panel.setBackground(new Color(r, g ,b));
2022-03-21 17:26:34 +01:00
}
public void actionPerformed(ActionEvent evt) {
String name = evt.getActionCommand();
2022-03-22 16:56:37 +01:00
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);
2022-03-21 17:26:34 +01:00
}
}
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);
2022-03-22 16:56:37 +01:00
Observer observer = new Observer();
2022-03-21 17:26:34 +01:00
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);
}
}