Пример #1
0
 /**
  * Draw a filled polygon with the given (x[i], y[i]) coordinates.
  *
  * @param x an array of all the x-coordindates of the polygon
  * @param y an array of all the y-coordindates of the polygon
  */
 public static void filledPolygon(double[] x, double[] y) {
   int N = x.length;
   GeneralPath path = new GeneralPath();
   path.moveTo((float) scaleX(x[0]), (float) scaleY(y[0]));
   for (int i = 0; i < N; i++) path.lineTo((float) scaleX(x[i]), (float) scaleY(y[i]));
   path.closePath();
   offscreen.fill(path);
   draw();
 }
Пример #2
0
 /**
  * Write the given text string in the current font, left-aligned at (x, y).
  *
  * @param x the x-coordinate of the text
  * @param y the y-coordinate of the text
  * @param s the text
  */
 public static void textLeft(double x, double y, String s) {
   offscreen.setFont(font);
   FontMetrics metrics = offscreen.getFontMetrics();
   double xs = scaleX(x);
   double ys = scaleY(y);
   int hs = metrics.getDescent();
   offscreen.drawString(s, (float) (xs), (float) (ys + hs));
   draw();
 }
Пример #3
0
 /**
  * Display on screen, pause for t milliseconds, and turn on <em>animation mode</em>: subsequent
  * calls to drawing methods such as <tt>line()</tt>, <tt>circle()</tt>, and <tt>square()</tt> will
  * not be displayed on screen until the next call to <tt>show()</tt>. This is useful for producing
  * animations (clear the screen, draw a bunch of shapes, display on screen for a fixed amount of
  * time, and repeat). It also speeds up drawing a huge number of shapes (call <tt>show(0)</tt> to
  * defer drawing on screen, draw the shapes, and call <tt>show(0)</tt> to display them all on
  * screen at once).
  *
  * @param t number of milliseconds
  */
 public static void show(int t) {
   defer = false;
   draw();
   try {
     Thread.currentThread().sleep(t);
   } catch (InterruptedException e) {
     System.out.println("Error sleeping");
   }
   defer = true;
 }
Пример #4
0
 /**
  * Draw filled circle of radius r, centered on (x, y).
  *
  * @param x the x-coordinate of the center of the circle
  * @param y the y-coordinate of the center of the circle
  * @param r the radius of the circle
  * @throws RuntimeException if the radius of the circle is negative
  */
 public static void filledCircle(double x, double y, double r) {
   if (r < 0) throw new RuntimeException("circle radius can't be negative");
   double xs = scaleX(x);
   double ys = scaleY(y);
   double ws = factorX(2 * r);
   double hs = factorY(2 * r);
   if (ws <= 1 && hs <= 1) pixel(x, y);
   else offscreen.fill(new Ellipse2D.Double(xs - ws / 2, ys - hs / 2, ws, hs));
   draw();
 }
Пример #5
0
 /**
  * Draw a filled square of side length 2r, centered on (x, y).
  *
  * @param x the x-coordinate of the center of the square
  * @param y the y-coordinate of the center of the square
  * @param r radius is half the length of any side of the square
  * @throws RuntimeException if r is negative
  */
 public static void filledSquare(double x, double y, double r) {
   if (r < 0) throw new RuntimeException("square side length can't be negative");
   double xs = scaleX(x);
   double ys = scaleY(y);
   double ws = factorX(2 * r);
   double hs = factorY(2 * r);
   if (ws <= 1 && hs <= 1) pixel(x, y);
   else offscreen.fill(new Rectangle2D.Double(xs - ws / 2, ys - hs / 2, ws, hs));
   draw();
 }
Пример #6
0
 /**
  * Draw a filled rectangle of given half width and half height, centered on (x, y).
  *
  * @param x the x-coordinate of the center of the rectangle
  * @param y the y-coordinate of the center of the rectangle
  * @param halfWidth is half the width of the rectangle
  * @param halfHeight is half the height of the rectangle
  * @throws RuntimeException if halfWidth or halfHeight is negative
  */
 public static void filledRectangle(double x, double y, double halfWidth, double halfHeight) {
   if (halfWidth < 0) throw new RuntimeException("half width can't be negative");
   if (halfHeight < 0) throw new RuntimeException("half height can't be negative");
   double xs = scaleX(x);
   double ys = scaleY(y);
   double ws = factorX(2 * halfWidth);
   double hs = factorY(2 * halfHeight);
   if (ws <= 1 && hs <= 1) pixel(x, y);
   else offscreen.fill(new Rectangle2D.Double(xs - ws / 2, ys - hs / 2, ws, hs));
   draw();
 }
Пример #7
0
 /**
  * Draw an ellipse with given semimajor and semiminor axes, centered on (x, y).
  *
  * @param x the x-coordinate of the center of the ellipse
  * @param y the y-coordinate of the center of the ellipse
  * @param semiMajorAxis is the semimajor axis of the ellipse
  * @param semiMinorAxis is the semiminor axis of the ellipse
  * @throws RuntimeException if either of the axes are negative
  */
 public static void filledEllipse(double x, double y, double semiMajorAxis, double semiMinorAxis) {
   if (semiMajorAxis < 0) throw new RuntimeException("ellipse semimajor axis can't be negative");
   if (semiMinorAxis < 0) throw new RuntimeException("ellipse semiminor axis can't be negative");
   double xs = scaleX(x);
   double ys = scaleY(y);
   double ws = factorX(2 * semiMajorAxis);
   double hs = factorY(2 * semiMinorAxis);
   if (ws <= 1 && hs <= 1) pixel(x, y);
   else offscreen.fill(new Ellipse2D.Double(xs - ws / 2, ys - hs / 2, ws, hs));
   draw();
 }
Пример #8
0
 /**
  * Draw a point at (x, y).
  *
  * @param x the x-coordinate of the point
  * @param y the y-coordinate of the point
  */
 public static void point(double x, double y) {
   double xs = scaleX(x);
   double ys = scaleY(y);
   double r = penRadius;
   // double ws = factorX(2*r);
   // double hs = factorY(2*r);
   // if (ws <= 1 && hs <= 1) pixel(x, y);
   if (r <= 1) pixel(x, y);
   else offscreen.fill(new Ellipse2D.Double(xs - r / 2, ys - r / 2, r, r));
   draw();
 }
Пример #9
0
  /**
   * Draw picture (gif, jpg, or png) centered on (x, y).
   *
   * @param x the center x-coordinate of the image
   * @param y the center y-coordinate of the image
   * @param s the name of the image/picture, e.g., "ball.gif"
   * @throws RuntimeException if the image is corrupt
   */
  public static void picture(double x, double y, String s) {
    Image image = getImage(s);
    double xs = scaleX(x);
    double ys = scaleY(y);
    int ws = image.getWidth(null);
    int hs = image.getHeight(null);
    if (ws < 0 || hs < 0) throw new RuntimeException("image " + s + " is corrupt");

    offscreen.drawImage(
        image, (int) Math.round(xs - ws / 2.0), (int) Math.round(ys - hs / 2.0), null);
    draw();
  }
Пример #10
0
 /**
  * Draw an arc of radius r, centered on (x, y), from angle1 to angle2 (in degrees).
  *
  * @param x the x-coordinate of the center of the circle
  * @param y the y-coordinate of the center of the circle
  * @param r the radius of the circle
  * @param angle1 the starting angle. 0 would mean an arc beginning at 3 o'clock.
  * @param angle2 the angle at the end of the arc. For example, if you want a 90 degree arc, then
  *     angle2 should be angle1 + 90.
  * @throws RuntimeException if the radius of the circle is negative
  */
 public static void arc(double x, double y, double r, double angle1, double angle2) {
   if (r < 0) throw new RuntimeException("arc radius can't be negative");
   while (angle2 < angle1) angle2 += 360;
   double xs = scaleX(x);
   double ys = scaleY(y);
   double ws = factorX(2 * r);
   double hs = factorY(2 * r);
   if (ws <= 1 && hs <= 1) pixel(x, y);
   else
     offscreen.draw(
         new Arc2D.Double(xs - ws / 2, ys - hs / 2, ws, hs, angle1, angle2 - angle1, Arc2D.OPEN));
   draw();
 }
Пример #11
0
  /**
   * Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees, rescaled
   * to w-by-h.
   *
   * @param x the center x-coordinate of the image
   * @param y the center y-coordinate of the image
   * @param s the name of the image/picture, e.g., "ball.gif"
   * @param w the width of the image
   * @param h the height of the image
   * @param degrees is the number of degrees to rotate counterclockwise
   * @throws RuntimeException if the image is corrupt
   */
  public static void picture(double x, double y, String s, double w, double h, double degrees) {
    Image image = getImage(s);
    double xs = scaleX(x);
    double ys = scaleY(y);
    double ws = factorX(w);
    double hs = factorY(h);
    if (ws < 0 || hs < 0) throw new RuntimeException("image " + s + " is corrupt");
    if (ws <= 1 && hs <= 1) pixel(x, y);

    offscreen.rotate(Math.toRadians(-degrees), xs, ys);
    offscreen.drawImage(
        image,
        (int) Math.round(xs - ws / 2.0),
        (int) Math.round(ys - hs / 2.0),
        (int) Math.round(ws),
        (int) Math.round(hs),
        null);
    offscreen.rotate(Math.toRadians(+degrees), xs, ys);

    draw();
  }
Пример #12
0
 /**
  * Draw picture (gif, jpg, or png) centered on (x, y), rescaled to w-by-h.
  *
  * @param x the center x coordinate of the image
  * @param y the center y coordinate of the image
  * @param s the name of the image/picture, e.g., "ball.gif"
  * @param w the width of the image
  * @param h the height of the image
  * @throws RuntimeException if the width height are negative
  * @throws RuntimeException if the image is corrupt
  */
 public static void picture(double x, double y, String s, double w, double h) {
   Image image = getImage(s);
   double xs = scaleX(x);
   double ys = scaleY(y);
   if (w < 0) throw new RuntimeException("width is negative: " + w);
   if (h < 0) throw new RuntimeException("height is negative: " + h);
   double ws = factorX(w);
   double hs = factorY(h);
   if (ws < 0 || hs < 0) throw new RuntimeException("image " + s + " is corrupt");
   if (ws <= 1 && hs <= 1) pixel(x, y);
   else {
     offscreen.drawImage(
         image,
         (int) Math.round(xs - ws / 2.0),
         (int) Math.round(ys - hs / 2.0),
         (int) Math.round(ws),
         (int) Math.round(hs),
         null);
   }
   draw();
 }
Пример #13
0
 /**
  * Display on-screen and turn off animation mode: subsequent calls to drawing methods such as
  * <tt>line()</tt>, <tt>circle()</tt>, and <tt>square()</tt> will be displayed on screen when
  * called. This is the default.
  */
 public static void show() {
   defer = false;
   draw();
 }
Пример #14
0
 /**
  * Draw a line from (x0, y0) to (x1, y1).
  *
  * @param x0 the x-coordinate of the starting point
  * @param y0 the y-coordinate of the starting point
  * @param x1 the x-coordinate of the destination point
  * @param y1 the y-coordinate of the destination point
  */
 public static void line(double x0, double y0, double x1, double y1) {
   offscreen.draw(new Line2D.Double(scaleX(x0), scaleY(y0), scaleX(x1), scaleY(y1)));
   draw();
 }
Пример #15
0
 /**
  * Clear the screen to the given color.
  *
  * @param color the Color to make the background
  */
 public static void clear(Color color) {
   offscreen.setColor(color);
   offscreen.fillRect(0, 0, width, height);
   offscreen.setColor(penColor);
   draw();
 }