import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JPanel; class ArdoiseBoutons extends JPanel { static final long serialVersionUID = 1; private boolean possedeDisque = true; public ArdoiseBoutons() { setBackground(Color.YELLOW); setPreferredSize(new Dimension(200, 150)); } public void setPossedeDisque(boolean possedeDisque) { this.possedeDisque = possedeDisque; } public void dessiner(Graphics g) { g.setColor(Color.RED); g.fillOval(60, 35, 80, 80); } public void paintComponent(Graphics g) { super.paintComponent(g); if (possedeDisque) dessiner(g); } } class MesBoutons extends JPanel implements ActionListener { private JButton trace = new JButton("trace"); private JButton efface = new JButton("efface"); private ArdoiseBoutons ardoise = new ArdoiseBoutons(); MesBoutons() { setLayout(new BorderLayout(5, 5)); JPanel lesBoutons = new JPanel(); lesBoutons.add(trace); lesBoutons.add(efface); add(lesBoutons, BorderLayout.NORTH); add(ardoise, BorderLayout.CENTER); trace.addActionListener(this); efface.addActionListener(this); setBorder(BorderFactory.createLineBorder(Color.BLACK)); } public void actionPerformed(ActionEvent e) { if (e.getSource() == trace) ardoise.setPossedeDisque(true); else if (e.getSource() == efface) ardoise.setPossedeDisque(false); ardoise.repaint(); } } public class BoutonsA extends JApplet { public void init() { setLayout(new FlowLayout(FlowLayout.CENTER)); add(new MesBoutons()); } }