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]);
    }
  }
Exemple #2
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 #3
0
 /** Set the pen color to the default color (black). */
 public static void setPenColor() {
   setPenColor(DEFAULT_PEN_COLOR);
 }