Beispiel #1
0
  @Override
  public void mouseDragged(
      final MouseEvent me,
      final Layer la,
      final int x_p,
      final int y_p,
      int x_d,
      int y_d,
      int x_d_old,
      int y_d_old) {
    final int tool = ProjectToolbar.getToolId();
    if (ProjectToolbar.PEN != tool) return;

    // transform to the local coordinates
    if (!this.at.isIdentity()) {
      // final Point2D.Double p = inverseTransformPoint(x_p, y_p);
      // x_p = (int)p.x;
      // y_p = (int)p.y;
      final Point2D.Double pd = inverseTransformPoint(x_d, y_d);
      x_d = (int) pd.x;
      y_d = (int) pd.y;
      final Point2D.Double pdo = inverseTransformPoint(x_d_old, y_d_old);
      x_d_old = (int) pdo.x;
      y_d_old = (int) pdo.y;
    }

    if (-1 != index) {
      if (me.isShiftDown()) {
        // resize
        item.radius =
            (int)
                Math.ceil(
                    Math.sqrt(
                        (x_d - item.p[0][index]) * (x_d - item.p[0][index])
                            + (y_d - item.p[1][index]) * (y_d - item.p[1][index])));
        if (item.radius < 1) item.radius = 1;
      } else {
        item.translate(index, x_d - x_d_old, y_d - y_d_old);
      }
      Rectangle repaint_bbox = bbox;
      final Rectangle current_bbox =
          this.at.createTransformedShape(item.getBoundingBox()).getBounds();
      if (null == bbox) repaint_bbox = current_bbox;
      else {
        repaint_bbox = (Rectangle) bbox.clone();
        repaint_bbox.add(current_bbox);
      }
      bbox = current_bbox;
      Display.repaint(layer_set, repaint_bbox);
    }
  }
Beispiel #2
0
  // Animation of display
  private void runDasher() {
    // Initial position
    content.rootBegin = 0;
    content.rootSize = content.getWidth();

    // To the end of application
    for (; ; ) {
      // Move according to current speed
      content.zoom(xSpeed);
      content.translate(ySpeed);
      content.repaint();

      // Display current sentence
      label.setText(content.updateText());

      // Wait for next frame
      try {
        Thread.sleep(frameRate);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
Beispiel #3
0
 private static void repaint() {
   Display display = Display.getInstance();
   if (display != null) {
     display.repaint();
   }
 }
Beispiel #4
0
  @Override
  public void mousePressed(
      final MouseEvent me, final Layer la, int x_p, int y_p, final double mag) {
    final int tool = ProjectToolbar.getToolId();
    if (ProjectToolbar.PEN != tool) return;

    final long lid = la.getId(); // isn't this.layer pointing to the current layer always?

    // transform the x_p, y_p to the local coordinates
    if (!this.at.isIdentity()) {
      final Point2D.Double p = inverseTransformPoint(x_p, y_p);
      x_p = (int) p.x;
      y_p = (int) p.y;
    }

    // find if the click is within radius of an existing point for the current layer
    for (final Item tmp : al_items) {
      index = tmp.find(lid, x_p, y_p, mag);
      if (-1 != index) {
        this.item = tmp;
        break;
      }
    }

    // final boolean is_zoom_invariant = "true".equals(project.getProperty("dissector_zoom"));

    // TODO: if zoom invariant, should check for nearest point. Or nearest point anyway, when
    // deleting
    // (but also for adding a new one?)

    if (me.isShiftDown() && Utils.isControlDown(me)) {
      if (-1 != index) {
        // delete
        item.remove(index);
        if (0 == item.n_points) al_items.remove(item);
        item = null;
        index = -1;
        Display.repaint(layer_set, this, 0);
      }
      // in any case:
      return;
    }
    if (-1 != index) return;
    // else try to add a point to a suitable item
    // Find an item in the previous or the next layer,
    //     which falls within radius of the clicked point
    try {
      for (final Item tmp : al_items) {
        index = tmp.add(x_p, y_p, la);
        if (-1 != index) {
          this.item = tmp;
          return;
        }
      }
      // could not be added to an existing item, so creating a new item with a new point in it
      int max_tag = 0;
      for (final Item tmp : al_items) {
        if (tmp.tag > max_tag) max_tag = tmp.tag;
      }
      int radius = 8; // default
      if (al_items.size() > 0) radius = al_items.get(al_items.size() - 1).radius;
      this.item = new Item(max_tag + 1, radius, x_p, y_p, la);
      index = 0;
      al_items.add(this.item);
    } finally {
      if (null != item) {
        bbox = this.at.createTransformedShape(item.getBoundingBox()).getBounds();
        Display.repaint(layer_set, bbox);
      } else Display.repaint(layer_set, this, 0);
    }
  }
Beispiel #5
0
  public static void main(String[] args) {

    // TEST MEASURE
    //        Point p1 = new Point(-1d, -1d);
    //        Point p2 = new Point(2d, 3d);
    //        System.out.println(measure.d(p1, p2));
    //        System.out.println(measure.s(p1, p2));
    //        return;

    Double[][] data = FileHandler.readFile(fileName);

    // cannot display points if dimension is > 2
    if (data[0].length != 2) canDisplay = false;

    // build graphic points from coords' array
    buildPointsFromData(data);
    Config.computeBoundingRect(points);

    // init display
    if (canDisplay) {
      disp = new Display();
      disp.setVisible(true);
      for (Point p : points) {
        disp.addObject(p);
      }
    }

    testResults = new double[nbTests];

    for (int t = 0; t < nbTests; ++t) {

      // define K clusters and K temporary centres
      clusters = new ArrayList<Cluster>();
      for (int i = 0; i < K; ++i) {
        clusters.add(new Cluster());
      }
      setRandomCenters();
      for (Cluster c : clusters) {
        System.out.println("center for cluster " + c + ": " + c.getCenter());
      }

      if (canDisplay) pause(1000);

      // variables used in for loops
      double minDist, currDist, diff;
      Double[] prevCoords, newCoords;
      Cluster alloc;
      Point newCenter;

      for (int i = 0; i < maxIter; ++i) {

        if (canDisplay) {
          disp.setLabel("[ iteration #" + (i + 1) + " ]");
        } else {
          System.out.println("------> iteration #" + (i + 1));
        }

        // allocate points to group which center is closest
        for (Point p : points) {

          minDist = Config.MAX;
          alloc = clusters.get(0); // default initialization

          for (Cluster c : clusters) {
            currDist = measure.d(p, c.getCenter());
            if (currDist < minDist) {
              minDist = currDist;
              alloc = c;
            }
          }

          alloc.addPoint(p);
        }

        // recenter: calculate gravity centers for formed groups
        diff = 0;
        prevCoords = null;
        for (Cluster c : clusters) {

          // delete previous center if it not a Point of the Cluster
          if (canDisplay && !c.getPoints().contains(c.getCenter())) {
            disp.removeObject(c.getCenter());
          }

          if (stopOnConverge) {
            prevCoords = c.getCenter().getCoords();
          }

          newCenter = c.makeGravityCenter();

          if (stopOnConverge) {
            newCoords = c.getCenter().getCoords();
            for (int k = 0; k < prevCoords.length; ++k) {
              diff += Math.abs(prevCoords[k] - newCoords[k]);
            }
          }

          if (canDisplay) {
            disp.addObject(newCenter);
          } else {
            // System.out.println("\tcenter for " + c + ": " + c.getCenter());
            System.out.println(c.getCenter());
          }
        } // loop over clusters

        if (canDisplay) {
          disp.repaint();
        }

        // if Clusters' centers don't change anymore, then stop (algorithm converged)
        if (diff == 0 && stopOnConverge) {
          testResults[t] = (double) i;
          if (canDisplay) {
            disp.setLabel("[ Converged at iteration #" + (i) + " ]");
            disp.repaint();
          } else {
            System.out.println("[ Converged at iteration #" + (i) + " ]");
          }
          break;
        }

        pause(100);
      } // loop over iterations

      if (testResults[t] == 0) {
        System.out.println("!!!!!!!!!! Test #" + t + " did not converge.");
        if (stopOnConverge) return;
      }

      // reset display
      if (canDisplay && t + 1 < nbTests) {
        for (Point p : points) p.setCluster(null);
        for (Cluster c : clusters) disp.removeObject(c.getCenter());
      }
    } // loop over tests

    // display test results and compute means
    DescriptiveStatistics stats = new DescriptiveStatistics(testResults);
    System.out.println("=========> Results:");
    for (int t = 0; t < nbTests; ++t) {
      System.out.println("--> [ " + testResults[t] + " ]");
    }
    System.out.println("=========> Means: " + stats.getMean());
    System.out.println("=========> Std dev: " + stats.getStandardDeviation());
  }