diff --git a/APL2.1/TP10/Combinaison/Combinaison.class b/APL2.1/TP10/Combinaison/Combinaison.class new file mode 100644 index 0000000..0f302c6 Binary files /dev/null and b/APL2.1/TP10/Combinaison/Combinaison.class differ diff --git a/APL2.1/TP10/Combinaison/Combinaison.java b/APL2.1/TP10/Combinaison/Combinaison.java index 05905b9..27ebffa 100644 --- a/APL2.1/TP10/Combinaison/Combinaison.java +++ b/APL2.1/TP10/Combinaison/Combinaison.java @@ -3,22 +3,36 @@ import javax.swing.*; import java.awt.event.*; class Observer implements ActionListener { - public Observer() { + 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 rb = (JRadioButton)evt.getSource(); - JPanel f = (JPanel)rb.getParent(); + JRadioButton radioButton = (JRadioButton)evt.getSource(); + JPanel panel = (JPanel)radioButton.getParent(); - if (name == "Jaune") { - f.setBackground(Color.YELLOW); - } else if (name == "Cyan") { - f.setBackground(Color.CYAN); - } else if (name == "Magenta") { - f.setBackground(Color.MAGENTA); - } + boolean selected = radioButton.isSelected(); + + if (name == "Jaune") shouldY = selected; + else if (name == "Magenta") shouldM = selected; + else if (name == "Cyan") shouldC = selected; + + updateColor(panel); } } @@ -30,12 +44,11 @@ public class Combinaison { 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"); - Observer observer = new Observer(); - b1.addActionListener(observer); b2.addActionListener(observer); b3.addActionListener(observer); diff --git a/APL2.1/TP10/Combinaison/Observer.class b/APL2.1/TP10/Combinaison/Observer.class index de48404..c91dec5 100644 Binary files a/APL2.1/TP10/Combinaison/Observer.class and b/APL2.1/TP10/Combinaison/Observer.class differ diff --git a/APL2.1/TP10/Commande/Commande.class b/APL2.1/TP10/Commande/Commande.class new file mode 100644 index 0000000..66cd30d Binary files /dev/null and b/APL2.1/TP10/Commande/Commande.class differ diff --git a/APL2.1/TP10/Commande/Commande.java b/APL2.1/TP10/Commande/Commande.java new file mode 100644 index 0000000..d7aadb2 --- /dev/null +++ b/APL2.1/TP10/Commande/Commande.java @@ -0,0 +1,45 @@ +import java.awt.*; +import javax.swing.*; +import java.awt.event.*; + +class Observer implements ActionListener { + public Observer() {} + + public void actionPerformed(ActionEvent evt) { + String input = evt.getActionCommand().toLowerCase().trim(); + JTextField textField = (JTextField)evt.getSource(); + JPanel panel = (JPanel)textField.getParent(); + + if (input.equals("cyan")) panel.setBackground(Color.CYAN); + else if (input.equals("jaune")) panel.setBackground(Color.YELLOW); + else if (input.equals("magenta")) panel.setBackground(Color.MAGENTA); + + textField.setText(""); + } +} + +public class Commande { + + 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(); + + JTextField t = new JTextField(); + t.setLocation(0, 0); + t.setSize(200, 20); + t.addActionListener(observer); + + JPanel p = new JPanel(); + p.setSize(200, 200); + p.setLocation(0, 0); + p.setLayout(null); + + f.add(p); + p.add(t); + f.setVisible(true); + } +} \ No newline at end of file diff --git a/APL2.1/TP10/Commande/Observer.class b/APL2.1/TP10/Commande/Observer.class new file mode 100644 index 0000000..efb84de Binary files /dev/null and b/APL2.1/TP10/Commande/Observer.class differ