public static Color gradientColor(double value) {
   if (value < 0.0) return Color.gray;
   if (value > 1.0) value = 1.0;
   int red = Math.min(255, (int) (512.0 - (value * 512.0)));
   int green = Math.min(255, (int) (value * 512.0));
   int blue = 0;
   return new Color(red, green, blue);
 }
 public static void drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth) {
   if (lineWidth == 1) g.drawLine(x1, y1, x2, y2);
   else {
     double angle;
     double halfWidth = ((double) lineWidth) / 2.0;
     double deltaX = (double) (x2 - x1);
     double deltaY = (double) (y2 - y1);
     if (x1 == x2) angle = Math.PI;
     else angle = Math.atan(deltaY / deltaX) + Math.PI / 2;
     int xOffset = (int) (halfWidth * Math.cos(angle));
     int yOffset = (int) (halfWidth * Math.sin(angle));
     int[] xCorners = {x1 - xOffset, x2 - xOffset + 1, x2 + xOffset + 1, x1 + xOffset};
     int[] yCorners = {y1 - yOffset, y2 - yOffset, y2 + yOffset + 1, y1 + yOffset + 1};
     g.fillPolygon(xCorners, yCorners, 4);
   }
 }
  public static void drawValueBar(
      int x, int y, String labelhead, double value, double percentage, int colorindex, Graphics g) {
    g.setColor(GetColor(colorindex));
    g.drawRect(x, y, VALUE_BAR_WIDTH, VALUE_BAR_HEIGHT);

    int barwidth;
    if (percentage < 0.0) {
      // Unknown value
      barwidth = VALUE_BAR_WIDTH - 1;
    } else {
      barwidth = (int) ((VALUE_BAR_WIDTH - 1) * Math.min(1.0, percentage));
    }
    // g.setColor(gradientColor(percentage));
    g.fillRect(x + 1, y + 1, barwidth, VALUE_BAR_HEIGHT - 1);

    if (labelhead != null) {
      g.setFont(MainFrame.defaultFont);
      // g.setColor(MainFrame.labelColor);
      int off = 2;
      g.drawString(labelhead, x + VALUE_BAR_WIDTH + 2, y + VALUE_BAR_HEIGHT);
      // off += (labelhead.length()+1) * 4; // Just a guess
      String maxStr = new String("DateRate [/sec]:");
      off += (int) ((maxStr.length() + 1) * 5.5);

      if (value < 0.0) {
        g.drawString("?", x + VALUE_BAR_WIDTH + off, y + VALUE_BAR_HEIGHT);
      } else {
        if (percentage < 0.0) {
          g.drawString(format(value), x + VALUE_BAR_WIDTH + off, y + VALUE_BAR_HEIGHT);
        } else {
          g.drawString(
              format(value) + " (" + format(percentage * 100) + "%)",
              x + VALUE_BAR_WIDTH + off,
              y + VALUE_BAR_HEIGHT);
        }
      }
    }
  }
  /* CALCULATE VALUES QUADRANTS: Calculate x-y values where direction is not
  parallel to eith x or y axis. */
  public static void calcValuesQuad(int x1, int y1, int x2, int y2) {
    double arrowAng = Math.toDegrees(Math.atan((double) haw / (double) al));
    double dist = Math.sqrt(al * al + aw);
    double lineAng =
        Math.toDegrees(Math.atan(((double) Math.abs(x1 - x2)) / ((double) Math.abs(y1 - y2))));

    // Adjust line angle for quadrant
    if (x1 > x2) {
      // South East
      if (y1 > y2) lineAng = 180.0 - lineAng;
    } else {
      // South West
      if (y1 > y2) lineAng = 180.0 + lineAng;
      // North West
      else lineAng = 360.0 - lineAng;
    }

    // Calculate coords
    xValues[0] = x2;
    yValues[0] = y2;
    calcCoords(1, x2, y2, dist, lineAng - arrowAng);
    calcCoords(2, x2, y2, dist, lineAng + arrowAng);
  }
  /* CALCULATE COORDINATES: Determine new x-y coords given a start x-y and
  a distance and direction */
  public static void calcCoords(int index, int x, int y, double dist, double dirn) {
    while (dirn < 0.0) dirn = 360.0 + dirn;
    while (dirn > 360.0) dirn = dirn - 360.0;
    // System.out.println("dirn = " + dirn);

    // North-East
    if (dirn <= 90.0) {
      xValues[index] = x + (int) (Math.sin(Math.toRadians(dirn)) * dist);
      yValues[index] = y - (int) (Math.cos(Math.toRadians(dirn)) * dist);
      return;
    }
    // South-East
    if (dirn <= 180.0) {
      xValues[index] = x + (int) (Math.cos(Math.toRadians(dirn - 90)) * dist);
      yValues[index] = y + (int) (Math.sin(Math.toRadians(dirn - 90)) * dist);
      return;
    }
    // South-West
    if (dirn <= 90.0) {
      xValues[index] = x - (int) (Math.sin(Math.toRadians(dirn - 180)) * dist);
      yValues[index] = y + (int) (Math.cos(Math.toRadians(dirn - 180)) * dist);
    }
    // Nort-West
    else {
      xValues[index] = x - (int) (Math.cos(Math.toRadians(dirn - 270)) * dist);
      yValues[index] = y - (int) (Math.sin(Math.toRadians(dirn - 270)) * dist);
    }
  }