Example #1
0
 /**
  * Draw a 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 polygon(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.draw(path);
   draw();
 }
Example #2
0
 /**
  * Draw a 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 square(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.draw(new Rectangle2D.Double(xs - ws / 2, ys - hs / 2, ws, hs));
   draw();
 }
Example #3
0
 /**
  * Draw a 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 circle(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.draw(new Ellipse2D.Double(xs - ws / 2, ys - hs / 2, ws, hs));
   draw();
 }
Example #4
0
 /**
  * Draw a 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 rectangle(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.draw(new Rectangle2D.Double(xs - ws / 2, ys - hs / 2, ws, hs));
   draw();
 }
Example #5
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 ellipse(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.draw(new Ellipse2D.Double(xs - ws / 2, ys - hs / 2, ws, hs));
   draw();
 }
Example #6
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();
 }
Example #7
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();
 }