示例#1
1
 // 记录鼠标松开时的位置,计算其位于哪一个格子内,返回其序号(一个整数n)
 // 若鼠标并未位于任何格子内,重复直到输入有效
 private int input() {
   boolean isPressing = false;
   while (true) {
     StdDraw.show(40); // 每40毫秒扫描一次鼠标状况
     if (StdDraw.mousePressed() && !isPressing) { // 第一次按下鼠标
       isPressing = true;
       continue;
     }
     if (!StdDraw.mousePressed() && isPressing) { // 第一次放开鼠标
       isPressing = false;
       double x = StdDraw.mouseX(), y = StdDraw.mouseY();
       if (y <= 5 || y >= 165 || x <= 5 || x >= 195) continue;
       int i, j;
       if (y >= 155) i = 0;
       else i = (int) ((155 - y) / 17.5);
       if (x <= 15) j = 0;
       else j = (int) ((x - 15) / 20);
       if (inCircle(x, y, i, j)) return i * 9 + j;
       if (i != 8 && inCircle(x, y, i + 1, j)) return (i + 1) * 9 + j;
       if (j != 8 && inCircle(x, y, i, j + 1)) return i * 9 + j + 1;
       if (i != 8 && j != 8 && inCircle(x, y, i + 1, j + 1)) return (i + 1) * 9 + j + 1;
       continue;
     }
   }
 }
示例#2
1
 /** Handles a mouse click, placing player's mark in the square on which the user clicks. */
 public static void handleMouseClick(char[][] board, char player) {
   while (!StdDraw.mousePressed()) {
     // Wait for mouse press
   }
   double x = Math.round(StdDraw.mouseX());
   double y = Math.round(StdDraw.mouseY());
   while (StdDraw.mousePressed()) {
     // Wait for mouse release
   }
   int a = (int) x;
   int b = (int) y;
   if (board[a][b] == ' ') {
     board[a][b] = player;
   }
 }
 @Override
 public void draw() {
   if (!hidden) {
     StdDraw.setPenColor(StdDraw.YELLOW);
     StdDraw.filledCircle(myX, myY, radius);
   }
 }
示例#4
0
  /**
   * @param palette an array of Colors to choose from
   * @param llx lower left x coordinate of this rug square
   * @param lly lower left y coordinate of this rug square
   * @param size width (and therefore also height) of this rug square
   * @param north color index of the north side of this rug square
   * @param east color index of the east side of this rug square
   * @param south color index of the south side of this rug square
   * @param west color index of the west side of this rug square
   */
  private static void persianRug(
      Color[] palette,
      double llx,
      double lly,
      double size,
      int north,
      int east,
      int south,
      int west) {

    if (size < .0035) { // base case, size ridiculously small
      return;
    }

    // random formula here--if you change it, you end up with another design or a blank screen of
    // one color
    int Color = (int) (north + east + west + south + 435) / 6;

    Color = Color % 11;

    StdDraw.setPenColor(palette[Color]);

    StdDraw.line(llx + size / 2, lly, llx + size / 2, lly + size);

    StdDraw.line(llx, lly + size / 2, llx + size, lly + size / 2);

    persianRug(palette, llx, lly + size / 2, size / 2, north, Color, Color, west); // top left

    persianRug(
        palette, llx + size / 2, lly + size / 2, size / 2, north, east, Color, Color); // top right

    persianRug(palette, llx, lly, size / 2, Color, Color, south, west); // bottom left

    persianRug(palette, llx + size / 2, lly, size / 2, Color, east, south, Color); // bottom right
  }
示例#5
0
 public static void main(String[] args) {
   StdDraw.setPenRadius(0.05);
   StdDraw.setPenColor(StdDraw.BLUE);
   StdDraw.point(0.5, 0.5);
   StdDraw.setPenColor(StdDraw.MAGENTA);
   StdDraw.line(0.2, 0.2, 0.8, 0.2);
 }
示例#6
0
 /** Plot bars from (0, a[i]) to (i, a[i]) to standard draw. */
 public static void plotBars(double[] a) {
   final int N = a.length;
   StdDraw.setXscale(0, N - 1);
   for (int i = 0; i < N; i++) {
     StdDraw.filledRectangle(i, a[i] / 2, .25, a[i] / 2);
   }
 }
  private void drawNode(Node2d node) {
    if (node == null) return;

    StdDraw.setPenColor(StdDraw.BLACK);
    StdDraw.setPenRadius(.005);
    node.p.draw();

    StdDraw.setPenRadius(.003);

    if (node.depth % 2 == 0) {
      StdDraw.setPenColor(StdDraw.RED);
      node.p.drawTo(new Point2D(node.p.x(), node.rect.ymax()));
      node.p.drawTo(new Point2D(node.p.x(), node.rect.ymin()));

      drawNode(node.left);
      drawNode(node.right);
    } else {
      StdDraw.setPenColor(StdDraw.BLUE);
      node.p.drawTo(new Point2D(node.rect.xmax(), node.p.y()));
      node.p.drawTo(new Point2D(node.rect.xmin(), node.p.y()));

      drawNode(node.left);
      drawNode(node.right);
    }
  }
示例#8
0
  public static void main(String[] args) {

    DNodeNet net = new DNodeNet();

    double k = 0.05;
    int L = 500;

    for (int i = 0; i < L; i++) {
      net.addDNode(new DNode());
    }

    for (int i = 0; i < net.nodes.length; i++) {
      for (int j = 0; j < net.nodes.length; j++) {
        if (Math.random() < k) {
          net.flow(i, j);
        }
      }
    }

    //    for(int i=0; i<L; i++){
    //      net.printI(i);}

    StdDraw.setXscale(-1.1, 1.1);
    StdDraw.setYscale(-1.1, 1.1);

    //    for(int i=0;i<L;i++){
    //      StdDraw.circle(Math.cos(6.28*i/L), Math.sin(6.28*i/L), 0.05);
    //    StdDraw.text(Math.cos(6.28*i/L), Math.sin(6.28*i/L), Integer.toString(i));}

    net.circle();

    for (int i = 0; i < L; i++) {
      net.graphI(i);
    }
  }
示例#9
0
  // MAIN FUNCTION
  public static void main(String[] args) throws InterruptedException { // notre fonction principale

    while (true) {

      initGame(); // on initialise la fenêtre du jeu
      generateGround(); // on génère le terrain dans des tableaux
      Menu.navigation(); // on affiche le menu de navigation

      while (!endOfGame) { // TANT QUE LA PARTIE N'EST PAS FINIE

        //				StdDraw.clear(Constants.BACKGROUND_COLOR);
        StdDraw.picture(Constants.X_MAX / 2, Constants.Y_MAX / 2, "./src/ciel.png");
        for (int i = 0; i < Menu.numberOfPlayers; i++) {
          runPlayer(i); // actions de chaque joueur (déplacement, Obus, mine)
        }
        endOfGame = Tank.endGame(); // on vérifie si il reste des joueurs
        showGround(); // (on fait varier le x de chaque rectangle)

        controlTanks(); // gérer les joueurs (collisions, vie, scores)
        controlObus(); // gérer les Obus (déplacement, collisions)
        controlMines(); // gérer les Mines (déplacement, collisions)
        controlMissiles(); // gérer les Missiles (déplacement, collisions)

        StdDraw.show(10); // durée d'affichage de la vue (cadence)
        Thread.sleep(10); // pause pendant l'affichage
      }
      endGame(); // quand un joueur est mort (affichage gagnant)
      clearGame(); // effacer toutes les données du jeu pour recommencer une partie
    }
  }
示例#10
0
 private void drawNode(TreePosition p, Node x) {
   StdDraw.setPenColor(Color.GREEN);
   StdDraw.filledCircle(p.x, p.y, 0.3);
   StdDraw.setPenColor(Color.RED);
   StdDraw.text(p.x, p.y, x.key.toString());
   StdDraw.setPenColor(Color.BLACK);
 }
示例#11
0
 public void draw() {
   // draw all of the points to standard draw
   for (Point2D point : set) {
     StdDraw.point(point.x(), point.y());
   }
   StdDraw.show();
 }
  public static void main(String[] args) {
    Scanner in;
    try {
      // input file
      in = new Scanner(new File(args[0]), "UTF-8");
    } catch (Exception e) {
      System.out.println("invalid or no input file ");
      return;
    }

    int N = in.nextInt(); // N-by-N percolation system

    // turn on animation mode
    StdDraw.show(0);

    // repeatedly read in sites to open and draw resulting system
    Percolation perc = new Percolation(N);
    draw(perc, N);
    StdDraw.show(DELAY);
    while (in.hasNext()) {
      int i = in.nextInt();
      int j = in.nextInt();
      perc.open(i, j);
      // System.out.println(i+" " + j);
      draw(perc, N);
      StdDraw.show(DELAY);
    }
  }
 public Maze(int N) {
   this.N = N;
   StdDraw.setXscale(0, N + 2);
   StdDraw.setYscale(0, N + 2);
   init();
   generate();
 }
示例#14
0
 /** Plot points (i, a[i]) to standard draw. */
 public static void plotPoints(double[] a) {
   final int N = a.length;
   StdDraw.setXscale(0, N - 1);
   StdDraw.setPenRadius(1.0 / (3.0 * N));
   for (int i = 0; i < N; i++) {
     StdDraw.point(i, a[i]);
   }
 }
示例#15
0
 /** Plot line segments connecting points (i, a[i]) to standard draw. */
 public static void plotLines(double[] a) {
   final int N = a.length;
   StdDraw.setXscale(0, N - 1);
   StdDraw.setPenRadius();
   for (int i = 1; i < N; i++) {
     StdDraw.line(i - 1, a[i - 1], i, a[i]);
   }
 }
示例#16
0
 public void draw() {
   StdDraw.setPenColor(Color.BLACK);
   StdDraw.setPenRadius(0.005);
   StdDraw.setYscale(-1, root.height + 2);
   StdDraw.setXscale(-1, root.N + 1);
   TreePosition rootTree = draw(root, 0, root.height);
   drawNode(rootTree, root);
 }
  public static void main(String[] args) {
    In in = new In(args[0]);

    StdDraw.setXscale(0, 32768);
    StdDraw.setYscale(0, 32768);

    Fast f = new Fast(in);
    f.search();
  }
示例#18
0
 public Game2() {
   r = new Random();
   board = new int[9][9];
   cat = new Cat(); // 猫的初始位置在board[4][4]
   count = 0;
   getShit = false;
   StdDraw.setXscale(0, 200);
   StdDraw.setYscale(0, 200);
   initBoard();
 }
示例#19
0
 /**
  * @param tn - TreeNode object
  * @param even - Vertical or horizontal cuts
  * @param left - left or right
  * @param pivot - Pivot point This function recursively draws the binary tree and displays how the
  *     tree slices the unit square. It works by sending it the current node to draw, whether or
  *     not it is a vertical cut, whether or not it is a left node, and a "pivot" point. The pivot
  *     point tells the method where to start and stop the line.
  */
 private void drawPoints(TreeNode tn, boolean even, boolean left, double pivot) {
   if (tn == null) return;
   StdDraw.setPenColor();
   // StdDraw.filledCircle(tn.p.x(), tn.p.y(), .005);
   if (even) { // Vertical cuts
     StdDraw.setPenColor(Color.red);
     if (left) {
       StdDraw.line(tn.p.x(), 0, tn.p.x(), pivot);
     } else {
       StdDraw.line(tn.p.x(), pivot, tn.p.x(), 1);
     }
     pivot = tn.p.x();
   } else { // Horizontal cuts
     StdDraw.setPenColor(Color.blue);
     if (left) {
       StdDraw.line(0, tn.p.y(), pivot, tn.p.y());
     } else {
       StdDraw.line(pivot, tn.p.y(), 1, tn.p.y());
     }
     pivot = tn.p.y();
   }
   StdDraw.setPenColor();
   StdDraw.filledCircle(tn.p.x(), tn.p.y(), .007);
   drawPoints(tn.left, !even, true, pivot);
   drawPoints(tn.right, !even, false, pivot);
 } // End drawPoints() function
示例#20
0
文件: Htree.java 项目: Ultimus/Test2
 public static void draw(int n, double sz, double x, double y) {
   if (n == 0) return;
   double x0 = x - sz / 2, x1 = x + sz / 2;
   double y0 = y - sz / 2, y1 = y + sz / 2;
   StdDraw.line(x0, y, x1, y);
   StdDraw.line(x0, y0, x0, y1);
   StdDraw.line(x1, y0, x1, y1);
   draw(n - 1, sz / 2, x0, y0);
   draw(n - 1, sz / 2, x0, y1);
   draw(n - 1, sz / 2, x1, y0);
   draw(n - 1, sz / 2, x1, y1);
 }
示例#21
0
  public static void main(String[] args) {

    // rescale coordinates and turn on animation mode
    StdDraw.setXscale(0, 32768);
    StdDraw.setYscale(0, 32768);
    StdDraw.show(0);
    StdDraw.setPenColor(StdDraw.BLUE);
    // read in the input
    String filename = args[0];
    In in = new In(filename);
    int N = in.readInt();
    Point[] points = new Point[N];
    for (int i = 0; i < N; i++) {
      int x = in.readInt();
      int y = in.readInt();
      points[i] = new Point(x, y);
      points[i].draw();
    }
    Quick.sort(points);
    boolean[] flags = new boolean[N];
    for (int i = 0; i < N; i++) {
      Point[] pfs = new Point[N];
      for (int m = 0; m < N; m++) pfs[m] = points[m];
      Arrays.sort(pfs, points[i].SLOPE_ORDER);
      for (int j = 0; j < N; ) {
        double t1 = points[i].slopeTo(pfs[j]);
        int idx = j;
        int count = 0;
        while (idx < N && points[i].slopeTo(pfs[idx]) == t1 && points[i].compareTo(pfs[idx]) < 0) {
          idx++;
          count++;
        }
        if (count >= 3) {
          int result = 0;
          for (int k = j; k < idx; k++) if (flags[i]) result++;
          if (result == count) {
            j = idx;
            continue;
          }
          StdOut.print(points[i] + " -> ");
          for (int k = j; k < idx; k++) {
            if (k == idx - 1) StdOut.print(pfs[k] + "\n");
            else StdOut.print(pfs[k] + " -> ");
            flags[k] = true;
            points[i].drawTo(pfs[k]);
          }
          j = idx;
        } else j++;
      }
    }
    StdDraw.show(0);
  }
示例#22
0
 public static void main(String[] args) {
   StdDraw.setXscale(0, 600);
   StdDraw.setYscale(0, 600);
   while (true) {
     Pankenski(
         new Point2D(300, 0),
         new Point2D(0, 300),
         new Point2D(600, 300),
         new Point2D(300, 600),
         20);
     StdDraw.clear();
   }
 }
  public static void main(String[] args) {
    double T;
    double dt;
    String filename;
    int numberOfPlanets;
    double universeRadius;
    int i;
    Planet[] list;
    double time = 0;
    T = Double.parseDouble(args[0]);
    dt = Double.parseDouble(args[1]);
    filename = args[2];
    In in = new In(filename);
    numberOfPlanets = in.readInt();
    universeRadius = in.readDouble();
    i = 0;

    StdDraw.setScale(-universeRadius, universeRadius);
    StdDraw.picture(0, 0, "images/starfield.jpg");
    list = new Planet[numberOfPlanets];
    int j = 0;
    while (j < numberOfPlanets) {
      list[j] = getPlanet(in);
      j = j + 1;
    }

    while (time <= T) {
      int n = 0;
      StdDraw.setScale(-universeRadius, universeRadius);
      StdDraw.picture(0, 0, "images/starfield.jpg");
      while (n < numberOfPlanets) {

        list[n].setNetForce(list);
        list[n].update(dt);
        list[n].xNetForce = 0;
        list[n].yNetForce = 0;
        list[n].draw();
        n = n + 1;
      }
      StdDraw.show(1);
      time = time + dt;
    }

    StdOut.printf("%d\n", numberOfPlanets);
    StdOut.printf("%.2e\n", universeRadius);
    for (int q = 0; q < numberOfPlanets; q++) {
      StdOut.printf(
          "%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
          list[q].x, list[q].y, list[q].xVelocity, list[q].yVelocity, list[q].mass, list[q].img);
    }
  }
  private static void draw(KdNode tree, boolean vertical) {
    if (tree == null) return;
    tree.p.draw();

    if (vertical) {
      StdDraw.setPenColor(255, 0, 0);
      StdDraw.line(0, tree.p.y(), 1, tree.p.y());
    } else {
      StdDraw.setPenColor(0, 0, 255);
      StdDraw.line(tree.p.x(), 0, tree.p.x(), 1);
    }
    draw(tree.l, !vertical);
    draw(tree.r, !vertical);
  }
  // plot an H, centered on (x, y) of the given side length
  public static void drawH(double x, double y, double size) {

    // compute the coordinates of the 4 tips of the H
    double x0 = x - size / 2;
    double x1 = x + size / 2;
    double y0 = y - size / 2;
    double y1 = y + size / 2;

    // draw the 3 line segments of the H
    StdDraw.line(x0, y0, x0, y1); // left vertical segment of the H
    StdDraw.line(x1, y0, x1, y1); // right vertical segment of the H
    StdDraw.line(x0, y, x1, y); // connect the two vertical segments of the
    // H
  }
示例#26
0
 protected static void showGround() {
   for (int i = 0; i < myRect.size(); i++) {
     myRect.get(i).show();
     if (i % 2 == 0) {
       myRect.get(i).contactBottom(); // contactBottom avec le sol
     }
     //				else {
     //				myRect.get(i).contactTop();// contactTop avec le haut
     //			}
     if (myRect.get(0).getXGround() > 7000) {
       StdDraw.setPenColor(Color.BLACK);
       StdDraw.setFont(new Font("Dialog", Font.PLAIN, 20));
       StdDraw.text(Constants.X_MAX / 2, Constants.Y_MAX / 2, "Player vs player");
     }
   }
 }
示例#27
0
  /** @param args */
  public static void main(String[] args) {
    StdDraw.setXscale(0, 32768);
    StdDraw.setYscale(0, 32768);
    String inputFileName = args[0];
    In in = new In(inputFileName);
    int n = Integer.parseInt(in.readLine());
    Point[] points = new Point[n];
    for (int i = 0; i < n; i++) {
      Point p = new Point(in.readInt(), in.readInt());
      points[i] = p;
      p.draw();
    }

    Brute brute = new Brute(points);
    brute.printLineSegments();
  }
示例#28
0
  public static void main(String[] args) {
    double xs0 = StdIn.readDouble();
    double xs1 = StdIn.readDouble();
    double ys0 = StdIn.readDouble();
    double ys1 = StdIn.readDouble();
    double x0 = StdIn.readDouble();
    double y0 = StdIn.readDouble();
    double x1 = StdIn.readDouble();
    double y1 = StdIn.readDouble();

    StdDraw.setXscale(xs0, xs1);
    StdDraw.setYscale(ys0, ys1);
    //		StdDraw.setPenColor(StdDraw.BLUE);
    //		StdDraw.setPenRadius(.01);
    StdDraw.line(x0, y0, x1, y1);
  }
  public static void main(String[] args) {
    In in = new In(args[0]); // input file
    int N = in.readInt(); // N-by-N percolation system

    // repeatedly read in sites to open and draw resulting system
    Percolation perc = new Percolation(N);
    draw(perc, N);
    while (!in.isEmpty()) {
      StdDraw.show(0); // turn on animation mode
      int i = in.readInt();
      int j = in.readInt();
      perc.open(i, j);
      draw(perc, N);
      StdDraw.show(100); // pause for 100 miliseconds
    }
  }
示例#30
0
 /** blendet Bild 'Gewonnen' ein Spieler erhaelt Erfahungspunkte */
 public void sterben() {
   Interface.player[0].erfahrungspunkteSammeln(Spieler.ERHALTERFAHRUNGSPUNKTE);
   Spielfeld.wertSetzenBeiXY(Interface.getLevel(), Interface.getRaum(), x, y, Interface.SIEG);
   StdDraw.picture(
       Interface.PIC1 + Interface.PIC2 * x,
       Interface.PIC1 + Interface.PIC2 * y,
       Interface.SIEGIMG);
 }