package tp2.ArdoiseMagique; /**
* ArdoiseMagique.java
*
* @author Gery Casiez
* @version
*/
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Iterator;
class Point {
public Integer x,y;
Point() {
x = 0;
y = 0;
}
Point(Integer x, Integer y) {
this.x = x;
this.y = y;
}
}
class Curve {
public ArrayList points;
Curve() {
points = new ArrayList();
}
public void addPoint(Point P) {
points.add(P);
}
public void clear() {
points.clear();
}
}
public class ArdoiseMagique extends JPanel {
private ArrayList curves;
public ArdoiseMagique(){
curves = new ArrayList();
curves.add(new Curve());
setBackground(Color.white);
}
public void addPoint(Integer x, Integer y) {
curves.get(curves.size()-1).addPoint(new Point(x,y));
repaint();
}
public void newCurve() {
curves.add(new Curve());
}
public void clear() {
curves.clear();
curves.add(new Curve());
repaint();
}
public void paintComponent(Graphics g) {
Point Pprev, Pcurrent;
super.paintComponent(g);
Iterator itcurve = curves.iterator();
Pprev = new Point();
// Pour chaque courbe
while (itcurve.hasNext()) {
Iterator it = itcurve.next().points.iterator();
if (it.hasNext()) {
Pprev = it.next();
}
// Dessine les points d'une courbe
while (it.hasNext()) {
Pcurrent = it.next();
g.drawLine(Pprev.x,Pprev.y, Pcurrent.x, Pcurrent.y);
Pprev = Pcurrent;
}
}
}
}