Utilisation des actionListener ()
bonjour
Je suis débutant en java et je dois rendre un projet sur un applet qui a pour but la réalisation d' un circuit électrique élémentaire composé d'une lampe s'allumant lorsque l'on sélectionne le bouton "circuit fermé" .
Mon problème est l'utilisation de repaint() voilà mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Circuit1 extends Applet implements ActionListener {
Button b;
Button bu;
public void init () {
setLayout(new FlowLayout());
b = new Button("Circuit Ouvert");
bu = new Button("Circuit Fermé");
add(b); add(bu);
b.addActionListener(this);
bu.addActionListener(this);
}
public void paint (Graphics g) {
int RAYON = 20; int u = 209; int i = 280;
setSize(460, 350);
g.setColor (Color.white);
g.fillRect (0, 0, getSize().width, getSize().height);
Rectangle r = new Rectangle(50,100,350,200);
Rectangle re = new Rectangle(40,170,20,50);
g.setColor(Color.black);
g.fillRect(re.x,re.y,re.width,re.height);
g.setColor(Color.black);
g.drawRect(r.x,r.y,r.width,r.height);
g.setColor(Color.WHITE);
g.fillOval (u,i, RAYON* 2, RAYON* 2);
g.setColor(Color.black);
g.drawOval (u,i, RAYON* 2, RAYON* 2);
}
public void actionPerformed(ActionEvent evt ) {
if( evt.getSource() == b)
repaint();
else if(evt.getSource()==bu)
{
}
}
} |
Je suis bloqué au niveau de l'écriture de la suite . Merci de votre précieuse aide.:yaisse2: