import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class MouseCircle3 extends Applet { int x, y; String key = ""; Color color = Color.black; public void init() { addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent event) { x = event.getX(); y = event.getY(); repaint(); requestFocus(); } } ); addKeyListener( new KeyAdapter() { public void keyPressed(KeyEvent e) { char c = e.getKeyChar(); int k = e.getKeyCode(); int modifiers = e.getModifiers(); key = KeyEvent.getKeyModifiersText(modifiers) + " " + KeyEvent.getKeyText(k) + " (" + c + ")"; repaint(); } } ); Button btnRed = new Button("Rouge"); add( btnRed ); btnRed.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { color = Color.red; repaint(); } } ); Button btnGreen = new Button("Vert"); add( btnGreen ); btnGreen.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { color = Color.green; repaint(); } } ); Button btnBlue = new Button("Bleu"); add( btnBlue ); btnBlue.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { color = Color.blue; repaint(); } } ); } public void paint( Graphics g ) { int width = getSize().width; int height = getSize().height; int dx = width/10; int dy = height/10; g.setColor( color ); g.fillRect( 0, 0, width, height ); g.setColor( Color.yellow ); g.fillOval( x-dx, y-dy, 2*dx, 2*dy ); g.setColor( Color.red ); g.drawString( key, 50, 50 ); } }