private static void draw(int frame) {
    // Draw Worm
    int offset = pos[frame] + 1;
    int len = skel[offset - 1];
    double[] x = new double[len];
    double[] y = new double[len];
    double _x = 0;
    double _y = 0;
    for (int i = 0; i < len; i++) {
      int s = i * 2;
      x[i] = skel[offset + s];
      y[i] = skel[offset + s + 1];
      _x += x[i];
      _y += y[i];
    }
    _x /= len;
    _y /= len;
    StdDraw.clear();
    StdDraw.setPenColor(StdDraw.BLACK);
    StdDraw.text(0, 120, "" + frame);
    StdDraw.setPenRadius(0.003);
    StdDraw.setXscale(-128, 128);
    StdDraw.setYscale(-128, 128);
    for (int i = 0; i < len; i++) {
      x[i] -= _x;
      y[i] -= _y;
      StdDraw.point(x[i], y[i]);
    }
    StdDraw.setPenColor(StdDraw.GREEN);
    StdDraw.setPenRadius(0.01);
    StdDraw.point(x[0], y[0]);

    StdDraw.setXscale(-128, 128);
    StdDraw.setYscale(128, -128);
    StdDraw.setPenColor(StdDraw.MAGENTA);
    StdDraw.setPenRadius(0.005);
    // Recreate from vectors
    offset = frame * n;
    x = new double[n + 1];
    y = new double[n + 1];
    double dist = 3.0;
    for (int i = 0; i < n; i++) {
      x[i + 1] = x[i] + (dist * Math.sin(vec[offset + i]));
      y[i + 1] = y[i] + (dist * Math.cos(vec[offset + i]));
    }
    _x = 0;
    _y = 0;
    for (int i = 0; i < n + 1; i++) {
      _x += x[i];
      _y += y[i];
    }
    _x /= n + 1;
    _y /= n + 1;
    for (int i = 0; i < n + 1; i++) {
      x[i] -= _x;
      y[i] -= _y;
      StdDraw.point(x[i], y[i]);
    }
  }
 public static void main(String[] args) throws Exception {
   skel = loadRawInteger("data/skeleton.dat");
   vec = loadRawDouble("data/vectorsCorrected.dat");
   pos = new int[2000];
   int seek = 0;
   int frame = 0;
   while (frame < pos.length) {
     pos[frame++] = seek;
     seek += (skel[seek] * 2) + 1;
   }
   StdDraw.setCanvasSize(512, 512);
   StdDraw.setXscale(-128, 128);
   StdDraw.setYscale(-128, 128);
   StdDraw.show(100);
   StdDraw.setPenRadius(0.005);
   int f = 0;
   while (true) {
     if (StdDraw.isKeyPressed(KeyEvent.VK_Q)) {
       break;
     }
     if (StdDraw.isKeyPressed(KeyEvent.VK_RIGHT)) {
       if (StdDraw.isKeyPressed(KeyEvent.VK_SHIFT)) {
         f += 10;
         draw(f);
       } else draw(f++);
     }
     if (StdDraw.isKeyPressed(KeyEvent.VK_LEFT)) {
       if (StdDraw.isKeyPressed(KeyEvent.VK_SHIFT)) {
         f -= 10;
         draw(f);
       } else draw(f--);
     }
     StdDraw.show(100);
   }
 }
 public static void main(String[] args) {
   final int N = 100;
   StdDraw.setXscale(0, N);
   StdDraw.setYscale(0, N * N);
   StdDraw.setPenRadius(.01);
   for (int i = 1; i <= N; i++) {
     StdDraw.point(i, i);
     StdDraw.point(i, i * i);
     StdDraw.point(i, i * Math.log(i));
   }
 }
Exemple #4
0
  // init
  private static void init() {
    if (frame != null) frame.setVisible(false);
    frame = new JFrame();
    offscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    onscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    offscreen = offscreenImage.createGraphics();
    onscreen = onscreenImage.createGraphics();
    setXscale();
    setYscale();
    offscreen.setColor(DEFAULT_CLEAR_COLOR);
    offscreen.fillRect(0, 0, width, height);
    setPenColor();
    setPenRadius();
    setFont();
    clear();

    // add antialiasing
    RenderingHints hints =
        new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    offscreen.addRenderingHints(hints);

    // frame stuff
    ImageIcon icon = new ImageIcon(onscreenImage);
    JLabel draw = new JLabel(icon);

    draw.addMouseListener(std);
    draw.addMouseMotionListener(std);

    frame.setContentPane(draw);
    frame.addKeyListener(std); // JLabel cannot get keyboard focus
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // closes all windows
    // frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);      // closes only current window
    frame.setTitle("Standard Draw");
    frame.setJMenuBar(createMenuBar());
    frame.pack();
    frame.requestFocusInWindow();
    frame.setVisible(true);
  }
Exemple #5
0
 /** Set the pen size to the default (.002). */
 public static void setPenRadius() {
   setPenRadius(DEFAULT_PEN_RADIUS);
 }