Bon voilà je viens de choper un pgm sympa style paint, juste pour dessiner et effacer le contenu.
Par contre je ne comprends pas du tout comment fonctionne la classe java.util.Vector. C'est une histoire d'outrepasser cette classe....
Bref si qq un peut m'expliquer ou alors me montrer ou je pourré trouvé un explication....
Merci bcp !!
Bilouz
Voici le code :
import java.applet.*;
import java.awt.*;
import java.util.Vector;
abstract class Forme {
abstract void dessiner(Graphics g);
}
class Dessin {
Graphics g;
Vector listeFormes;
Dessin(Graphics gc) {
g = gc;
listeFormes = new Vector();
}
void ajoute(Forme f) {
listeFormes.addElement(f);
}
void dessiner() {
int n = listeFormes.size();
Forme f;
for(int i=0; i<n; i++) {
f = (Forme) listeFormes.elementAt(i);
f.dessiner(g);
}
}
void effacer() {
listeFormes.removeAllElements();
}
}
class Point {
int x;
int y;
Point(int x1, int y1) {
x = x1;
y = y1;
}
}
class Trace extends Forme {
Color couleur;
Vector listePoints;
Trace(Color c) {
couleur = c;
listePoints = new Vector();
}
void ajoute(Point p) {
listePoints.addElement(p);
}
void ajoute(int x, int y) {
ajoute(new Point(x,y));
}
Point nieme(int n) {
if (listePoints.capacity() < n) return null;
return (Point) listePoints.elementAt(n);
}
int nbPoints() {
return listePoints.size();
}
void dessiner(Graphics g) {
int n = nbPoints();
if(n<=1) return;
g.setColor(couleur);
int x_precedent = nieme(0).x;
int y_precedent = nieme(0).y;
int x,y;
for(int i=1; i<n; i++) {
x = nieme(i).x;
y = nieme(i).y;
g.drawLine(x_precedent, y_precedent, x, y);
x_precedent = x;
y_precedent = y;
}
}
}
public class DessinerV2 extends Applet {
private Dessin dessin;
private Trace tr;
private int x_precedent;
private int y_precedent;
private boolean gomme;
private String figure;
private Color couleur_courrante;
private Button bouton_effacer;
private Choice bouton_figure;
private Choice bouton_couleur;
private Choice taille_gomme;
private String taille;
public void init() {
dessin = new Dessin(getGraphics());
this.setBackground(Color.white);
gomme = false;
figure = "trace";
couleur_courrante = Color.black;
bouton_effacer = new Button("Effacer");
bouton_effacer.setForeground(Color.black);
bouton_effacer.setBackground(Color.lightGray);
this.add(bouton_effacer);
}
public void paint(Graphics gc) {
dessin.dessiner();
}
public boolean mouseDown(Event e, int x, int y) {
if(figure.equals("trace")) {
tr = new Trace(couleur_courrante);
tr.ajoute(x, y);
}
x_precedent = x;
y_precedent = y;
return true;
}
public boolean mouseDrag(Event e, int x, int y) {
Graphics g = this.getGraphics();
{
tr.ajoute(x, y);
g.setColor(couleur_courrante);
g.drawLine(x_precedent, y_precedent, x, y);
x_precedent = x;
y_precedent = y;
}
return true;
}
public boolean mouseUp(Event e, int x, int y) {
Graphics g = this.getGraphics();
g.setColor(couleur_courrante);
{
dessin.ajoute(tr);
}
return true;
}
public boolean action(Event event, Object arg) {
if(event.target == bouton_effacer) {
dessin.effacer();
Graphics g = this.getGraphics();
Rectangle r = this.bounds();
g.setColor(this.getBackground());
g.fillRect(r.x, r.y, r.width, r.height);
return true;
}
else if(event.target == bouton_figure) {
figure = (String) arg;
return true;
}
else if(event.target == taille_gomme) {
taille = (String) arg;
return true;
}
else return super.action(event, arg);
}
}