ArdoiseMagique.java 1.61 KB
package tp2.ArdoiseMagique; /**
 * ArdoiseMagique.java
 *
 * @author <a href="mailto:gery.casiez@lifl.fr">Gery Casiez</a>
 * @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<Point> points;

  Curve() {
    points = new ArrayList<Point>();
  }

  public void addPoint(Point P) {
    points.add(P);
  }

  public void clear() {
    points.clear();
  }
}

public class ArdoiseMagique extends JPanel {
  private ArrayList<Curve> curves;

  public ArdoiseMagique(){
    curves = new ArrayList<Curve>();
    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<Curve> itcurve = curves.iterator();

    Pprev = new Point();

    // Pour chaque courbe
    while (itcurve.hasNext()) {
      Iterator<Point> 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;
      }
    }
  }
}