Example #1
0
 public static BufferedImage scaleImage(BufferedImage bi, double scale) {
   int w1 = (int) (Math.round(scale * bi.getWidth()));
   int h1 = (int) (Math.round(scale * bi.getHeight()));
   BufferedImage image = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);
   Graphics2D g2 = image.createGraphics();
   g2.setRenderingHint(
       RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
   g2.setPaint(Color.white);
   g2.fillRect(0, 0, w1, h1);
   g2.drawImage(bi, 0, 0, w1, h1, null); // this);
   g2.dispose();
   return image;
 }
Example #2
0
  /**
   * Конвертация строки типа: 5 Minutes
   *
   * @param str
   * @return
   */
  private static int parseDuration(String str) throws ParseException {
    int idx = str.indexOf(' ');
    float num = DF.parse(str.substring(0, idx)).floatValue();
    switch (str.substring(idx + 1)) {
      case "Seconds":
        return Math.round(num);

      case "Minutes":
        return Math.round(num * 60);

      default:
        throw new ParseException("Неизвестный формат единиц: " + str, idx + 1);
    }
  }
Example #3
0
 private static Color lighter(Color col) {
   int hsl[] = new int[3];
   Utils.rgb2hsl(col.getRed(), col.getGreen(), col.getBlue(), hsl);
   hsl[1] = Math.round(0.7f * hsl[1]);
   hsl[2] = 100;
   int rgb[] = Utils.hsl2rgb(hsl);
   return new Color(rgb[0], rgb[1], rgb[2]);
 }
Example #4
0
  public static FibonacciHeap<NodeInfoHelper> initialStartSet(
      long startNode,
      long endNode,
      HashMap<Long, FibonacciHeapNode<NodeInfoHelper>> nodeHelperCache) {
    FibonacciHeap<NodeInfoHelper> openSet = new FibonacciHeap<NodeInfoHelper>();

    NodeInfo start = OSMData.nodeHashMap.get(startNode);
    NodeInfoHelper initial;
    FibonacciHeapNode<NodeInfoHelper> fInitial;
    // initial start end set
    if (start.isIntersect()) {
      // initial
      initial = new NodeInfoHelper(startNode);
      initial.setCost(0);
      initial.setHeuristic(estimateHeuristic(startNode, endNode));
      fInitial = new FibonacciHeapNode<NodeInfoHelper>(initial);
      openSet.insert(fInitial, initial.getTotalCost()); // push the start node
      nodeHelperCache.put(initial.getNodeId(), fInitial); // add cache
    } else {
      EdgeInfo edge = start.getOnEdgeList().getFirst();
      double speed = edge.getTravelSpeed();
      int travelTime = 1; // second
      int distance;
      if (!edge.isOneway()) {
        // distance from start to middle
        distance = edge.getStartDistance(startNode);
        travelTime = (int) Math.round(distance / speed * OSMParam.MILLI_PER_SECOND);
        initial = new NodeInfoHelper(edge.getStartNode());
        initial.setCost(travelTime);
        initial.setHeuristic(estimateHeuristic(edge.getStartNode(), endNode));
        fInitial = new FibonacciHeapNode<NodeInfoHelper>(initial);
        openSet.insert(fInitial, initial.getTotalCost()); // push the start node
        nodeHelperCache.put(initial.getNodeId(), fInitial); // add cache
      }
      distance = edge.getEndDistance(startNode);
      travelTime = (int) Math.round(distance / speed * OSMParam.MILLI_PER_SECOND);
      initial = new NodeInfoHelper(edge.getEndNode());
      initial.setCost(travelTime);
      initial.setHeuristic(estimateHeuristic(edge.getEndNode(), endNode));
      fInitial = new FibonacciHeapNode<NodeInfoHelper>(initial);
      openSet.insert(fInitial, initial.getTotalCost()); // push the start node
      nodeHelperCache.put(initial.getNodeId(), fInitial); // add cache
    }
    return openSet;
  }
Example #5
0
  /**
   * Called by the paint method to draw the graph and its graph items.
   *
   * @param g the graphics context.
   */
  public void paintComponent(Graphics g) {

    Dimension dim = getSize();
    Insets insets = getInsets();
    dataArea =
        new Rectangle(
            insets.left,
            insets.top,
            dim.width - insets.left - insets.right - 1,
            dim.height - insets.top - insets.bottom - 1);
    // background
    if (isOpaque()) {
      g.setColor(getBackground());
      g.fillRect(0, 0, dim.width, dim.height);
    }
    g.setColor(getForeground());
    // get axis tickmarks
    double xticks[] = xAxis.getTicks();
    double yticks[] = yAxis.getTicks();
    int yb = dataArea.y + dataArea.height;
    // draw grid
    if (showGrid) {
      g.setColor(gridColor != null ? gridColor : getBackground().darker());
      // vertical x grid lines
      for (int i = 0; i < xticks.length; i += 2) {
        int x = dataArea.x + (int) Math.round(xticks[i]);
        g.drawLine(x, dataArea.y, x, dataArea.y + dataArea.height);
      }
      // horizontal y grid lines
      for (int i = 0; i < yticks.length; i += 2) {
        int y = yb - (int) Math.round(yticks[i]);
        g.drawLine(dataArea.x, y, dataArea.x + dataArea.width, y);
      }
    }
    for (int i = 0; i < graphItems.size(); i++) {
      ((GraphItem) graphItems.elementAt(i)).draw(this, g, dataArea, xAxis, yAxis);
    }
    if (sPt != null && ePt != null) {
      g.setColor(getForeground());
      g.drawRect(
          Math.min(sPt.x, ePt.x), Math.min(sPt.y, ePt.y),
          Math.abs(ePt.x - sPt.x), Math.abs(ePt.y - sPt.y));
    }
  }
Example #6
0
    public void updateCPUInfo(Result result) {
      if (prevUpTime > 0L && result.upTime > prevUpTime) {
        // elapsedCpu is in ns and elapsedTime is in ms.
        long elapsedCpu = result.processCpuTime - prevProcessCpuTime;
        long elapsedTime = result.upTime - prevUpTime;
        // cpuUsage could go higher than 100% because elapsedTime
        // and elapsedCpu are not fetched simultaneously. Limit to
        // 99% to avoid Plotter showing a scale from 0% to 200%.
        float cpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 10000F * result.nCPUs));

        getPlotter()
            .addValues(result.timeStamp, Math.round(cpuUsage * Math.pow(10.0, CPU_DECIMALS)));
        getInfoLabel()
            .setText(
                Resources.format(
                    Messages.CPU_USAGE_FORMAT, String.format("%." + CPU_DECIMALS + "f", cpuUsage)));
      }
      this.prevUpTime = result.upTime;
      this.prevProcessCpuTime = result.processCpuTime;
    }
Example #7
0
 public NodeInfoHelper getEndNodeHelper(long endNode) {
   // TODO: not time dependent now, need to modify
   NodeInfo end = OSMData.nodeHashMap.get(endNode);
   NodeInfoHelper endNodeHelper = new NodeInfoHelper(endNode);
   if (getNodeId() == end.getNodeId()) { // no need to do in edge routing
     return this;
   } else {
     EdgeInfo edge = end.getOnEdgeList().getFirst();
     int distance;
     // from start to middle
     if (getNodeId() == edge.getStartNode()) {
       distance = edge.getStartDistance(endNode);
     } else { // from end to middle
       distance = edge.getEndDistance(endNode);
     }
     double speed = edge.getTravelSpeed();
     int travelTime = 1; // second
     travelTime = (int) Math.round(distance / speed);
     endNodeHelper.setCost(getCost() + travelTime);
     endNodeHelper.setParentId(getNodeId());
   }
   return endNodeHelper;
 }
  public void paintProbe(Graphics2D g2, int x1, int y1, int x2, int y2, double tmin, double tmax) {
    g2.setStroke(stroke);
    g2.setColor(color);

    int ppx = -1, ppy = -1;
    for (int i = 0; i < values.size(); i++) {
      Double v = values.get(i);
      if (v != null && tmax - tmin > 0.0) {
        double frac = (v - tmin) / (tmax - tmin);
        int pixY = y2 - (int) Math.round(frac * (y2 - y1));
        int pixX = x1 + (int) Math.floor((double) (i + 1) / (double) (values.size() + 2));

        if (ppx != -1) {
          g2.drawLine(ppx, ppy, pixX, pixY);
        }

        ppx = pixX;
        ppy = pixY;

      } else {
        ppx = ppy = -1;
      }
    }
  }
Example #9
0
  // public List getUserStoryList(String sessionId,String iterationId,ServletOutputStream out) {
  public List getUserStoryList(String sessionId, String iterationId, PrintWriter out) {

    List<Map> list = new ArrayList<Map>();

    statusMap.put(sessionId, "0");

    try {

      String apiURL =
          rallyApiHost
              + "/hierarchicalrequirement?"
              + "query=(Iteration%20=%20"
              + rallyApiHost
              + "/iteration/"
              + iterationId
              + ")&fetch=true&start=1&pagesize=100";

      log.info("getUserStoryList apiURL=" + apiURL);

      String responseXML = getRallyXML(apiURL);

      org.jdom.input.SAXBuilder bSAX = new org.jdom.input.SAXBuilder();
      org.jdom.Document doc = bSAX.build(new StringReader(responseXML));
      Element root = doc.getRootElement();

      XPath xpath = XPath.newInstance("//Object");
      List xlist = xpath.selectNodes(root);

      int totalSteps = xlist.size() + 1;
      int currentStep = 0;

      List taskRefLink = new ArrayList();

      Iterator iter = xlist.iterator();
      while (iter.hasNext()) {
        double totalTimeSpent = 0.0D;

        Map map = new HashMap();

        Element item = (Element) iter.next();
        String objId = item.getChildText("ObjectID");
        String name = item.getChildText("Name");
        String planEstimate = item.getChildText("PlanEstimate");
        String formattedId = item.getChildText("FormattedID");

        String taskActualTotal = item.getChildText("TaskActualTotal");
        String taskEstimateTotal = item.getChildText("TaskEstimateTotal");
        String taskRemainingTotal = item.getChildText("TaskRemainingTotal");
        String scheduleState = item.getChildText("ScheduleState");

        Element ownerElement = item.getChild("Owner");

        String owner = "";
        String ownerRef = "";

        if (ownerElement != null) {
          owner = ownerElement.getAttributeValue("refObjectName");
        }

        Element taskElements = item.getChild("Tasks");
        // List taskElementList=taskElements.getContent();
        List taskElementList = taskElements.getChildren();

        List taskList = new ArrayList();

        log.info("taskElements.getChildren=" + taskElements);
        log.info("taskList=" + taskElementList);

        for (int i = 0; i < taskElementList.size(); i++) {
          Element taskElement = (Element) taskElementList.get(i);

          String taskRef = taskElement.getAttributeValue("ref");

          String[] objectIdArr = taskRef.split("/");
          String objectId = objectIdArr[objectIdArr.length - 1];

          log.info("objectId=" + objectId);

          // Map taskMap=getTaskMap(taskRef);
          Map taskMap = getTaskMapBatch(objectId);

          double taskTimeSpentTotal =
              Double.parseDouble((String) taskMap.get("taskTimeSpentTotal"));
          totalTimeSpent += taskTimeSpentTotal;
          taskList.add(taskMap);
        }

        map.put("type", "userstory");
        map.put("formattedId", formattedId);
        map.put("name", name);
        map.put("taskStatus", scheduleState);
        map.put("owner", owner);
        map.put("planEstimate", planEstimate);
        map.put("taskEstimateTotal", taskEstimateTotal);
        map.put("taskRemainingTotal", taskRemainingTotal);
        map.put("taskTimeSpentTotal", "" + totalTimeSpent);

        list.add(map);
        list.addAll(taskList);

        ++currentStep;
        double percentage = 100.0D * currentStep / totalSteps;
        String status = "" + Math.round(percentage);
        statusMap.put(sessionId, status);

        out.println("<script>parent.updateProcessStatus('" + status + "%')</script>" + status);
        out.flush();
        log.info("out.flush..." + status);

        // log.info("status="+status+" sessionId="+sessionId);
        // log.info("L1 statusMap="+statusMap+" "+statusMap.hashCode());

      }

      double planEstimate = 0.0D;
      double taskEstimateTotal = 0.0D;
      double taskRemainingTotal = 0.0D;
      double taskTimeSpentTotal = 0.0D;
      Map iterationMap = new HashMap();
      for (Map map : list) {
        String type = (String) map.get("type");

        String planEstimateStr = (String) map.get("planEstimate");

        log.info("planEstimateStr=" + planEstimateStr);

        if ("userstory".equals(type)) {

          if (planEstimateStr != null) {
            planEstimate += Double.parseDouble(planEstimateStr);
          }
          taskEstimateTotal += Double.parseDouble((String) map.get("taskEstimateTotal"));
          taskRemainingTotal += Double.parseDouble((String) map.get("taskRemainingTotal"));
          taskTimeSpentTotal += Double.parseDouble((String) map.get("taskTimeSpentTotal"));
        }
      }

      apiURL = rallyApiHost + "/iteration/" + iterationId + "?fetch=true";
      log.info("iteration apiURL=" + apiURL);
      responseXML = getRallyXML(apiURL);

      bSAX = new org.jdom.input.SAXBuilder();
      doc = bSAX.build(new StringReader(responseXML));
      root = doc.getRootElement();

      xpath = XPath.newInstance("//Iteration");
      xlist = xpath.selectNodes(root);

      String projName = "";
      String iterName = "";
      String iterState = "";

      iter = xlist.iterator();
      while (iter.hasNext()) {
        Element item = (Element) iter.next();

        iterName = item.getChildText("Name");
        iterState = item.getChildText("State");
        Element projElement = item.getChild("Project");
        projName = projElement.getAttributeValue("refObjectName");
      }

      iterationMap.put("type", "iteration");
      iterationMap.put("formattedId", "");
      iterationMap.put("name", projName + " - " + iterName);
      iterationMap.put("taskStatus", iterState);
      iterationMap.put("owner", "");

      iterationMap.put("planEstimate", "" + planEstimate);
      iterationMap.put("taskEstimateTotal", "" + taskEstimateTotal);
      iterationMap.put("taskRemainingTotal", "" + taskRemainingTotal);
      iterationMap.put("taskTimeSpentTotal", "" + taskTimeSpentTotal);

      list.add(0, iterationMap);

      statusMap.put(sessionId, "100");

      log.info("L2 statusMap=" + statusMap);

      log.info("L2 verify=" + getProcessStatus(sessionId));
      log.info("-----------");

      // String jsonData=JsonUtil.encodeObj(list);
      String jsonData = JSONValue.toJSONString(list);

      out.println("<script>parent.tableResult=" + jsonData + "</script>");
      out.println("<script>parent.showTableResult()</script>");

    } catch (Exception ex) {
      log.error("ERROR: ", ex);
    }

    return list;
  }
Example #10
0
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.WHITE);
    // Draws a white arrow and the principal axis
    g.drawLine(0, 200, 700, 200);
    g.drawLine(arrow_x, 200, arrow_x, arrow_y2);

    // Show coordinates of arrow tip
    arrowCoordinate_x = arrow_x - startingPosition;
    arrowCoordinate_x /= 10;
    arrowCoordinate_y = 200 - arrow_y2;
    arrowCoordinate_y /= 10;

    // Coordinates
    Optics.lbl_arrowCoordinates.setText(
        "<html>(d<sub>o</sub>, h<sub>o</sub>) = ("
            + arrowCoordinate_x
            + ", "
            + arrowCoordinate_y
            + ")</html>");

    if (arrow_y2 < 200) // if arrow is above principal axis
    {
      g.drawLine(arrow_x, arrow_y2, arrow_x - 7, arrow_y2 + 7);
      g.drawLine(arrow_x, arrow_y2, arrow_x + 7, arrow_y2 + 7);
    } else if (arrow_y2 > 200) // if arrow is below principal axis
    {
      g.drawLine(arrow_x, arrow_y2, arrow_x - 7, arrow_y2 - 7);
      g.drawLine(arrow_x, arrow_y2, arrow_x + 7, arrow_y2 - 7);
    }
    // Draws lines for the grid
    if (lenses) startingPosition = 350;
    else {
      radiusOfCurvature = 20 * focalLength;
      if (type == 0) startingPosition = 500;
      else startingPosition = 350;
    }
    {
      for (int i = startingPosition; i <= 700; i += 10) {
        if ((i - startingPosition) % (10 * focalLength) == 0) {
          g.setColor(Color.ORANGE);
          g.drawLine(i, 195, i, 205);
        } else {
          g.setColor(Color.WHITE);
          g.drawLine(i, 197, i, 203);
        }
      }
      for (int i = startingPosition; i >= 0; i -= 10) {
        if ((i - startingPosition) % (10 * focalLength) == 0 && i != 0) {
          g.setColor(Color.ORANGE);
          g.drawLine(i, 195, i, 205);
        } else {
          g.setColor(Color.WHITE);
          g.drawLine(i, 197, i, 203);
        }
      }
    }
    g.setColor(Color.WHITE);

    if (lenses) {
      if (type == 0) // If Converging
      {
        // Draws a converging lens
        g.drawArc(340, 50, 40, 300, 120, 120);
        g.drawArc(320, 50, 40, 300, 60, -120);
        // draws horizontal line from the tip of the arrow to the lens (line 1/3)
        g.setColor(Color.RED);
        g.drawLine(arrow_x, arrow_y2, 350, arrow_y2);
        // calculates necessary information to form equation of line from lens to focal point (line
        // 2/3)

        dy_1 = 200 - arrow_y2;

        if (arrow_x > 350) dx_1 = -10 * focalLength;
        else dx_1 = 10 * focalLength;
        slope_1 = dy_1 / dx_1;

        if (arrow_x > 350) y_intercept_1 = 200 - slope_1 * (350 - 10 * focalLength);
        else y_intercept_1 = 200 - slope_1 * (10 * focalLength + 350);
        // Calculates coordinates of points on the edge of screen (endpoints)
        if (arrow_x <= 350)
          y_screenIntersection_1 = (int) (Math.round(slope_1 * 700 + y_intercept_1));
        else y_screenIntersection_1 = (int) (Math.round(y_intercept_1));
        if (slope_1 != 0)
          if (arrow_y2 <= 200)
            x_screenIntersection_1 = (int) (Math.round((400 - y_intercept_1) / slope_1));
          else x_screenIntersection_1 = (int) (Math.round(-y_intercept_1 / slope_1));
        if (x_screenIntersection_1 >= 0
            && x_screenIntersection_1 <= 700) // If endpoint is on the x-edge
        if (arrow_y2 <= 200) g.drawLine(350, arrow_y2, x_screenIntersection_1, 400);
          else g.drawLine(350, arrow_y2, x_screenIntersection_1, 0);
        else if (arrow_x > 350) g.drawLine(350, arrow_y2, 0, y_screenIntersection_1);
        else
          g.drawLine(350, arrow_y2, 700, y_screenIntersection_1); // Else: endpoint is on the y-edge
      } else // Else: Diverging
      {
        // Draws a diverging lens
        g.drawArc(360, 50, 40, 300, 120, 120);
        g.drawArc(300, 50, 40, 300, 60, -120);
        g.drawLine(330, 68, 370, 68);
        g.drawLine(330, 330, 370, 330);

        // draws horizontal line from the tip of the arrow to the lens (line 1/3)
        g.setColor(Color.RED);
        g.drawLine(arrow_x, arrow_y2, 350, arrow_y2);

        // calculates necessary information to form equation of line from lens to focal point (line
        // 2/3)

        dy_1 = arrow_y2 - 200;

        if (arrow_x > 350) dx_1 = -10 * focalLength;
        else dx_1 = 10 * focalLength;
        slope_1 = dy_1 / dx_1;

        if (arrow_x > 350) y_intercept_1 = 200 - slope_1 * (10 * focalLength + 350);
        else y_intercept_1 = 200 - slope_1 * (350 - 10 * focalLength);
        // Calculates coordinates of points on the edge of screen (endpoints)
        if (arrow_x <= 350)
          y_screenIntersection_1 = (int) (Math.round(slope_1 * 700 + y_intercept_1));
        else y_screenIntersection_1 = (int) (Math.round(y_intercept_1));
        if (slope_1 != 0)
          if (arrow_y2 <= 200)
            x_screenIntersection_1 = (int) (Math.round(-y_intercept_1 / slope_1));
          else x_screenIntersection_1 = (int) (Math.round((400 - y_intercept_1) / slope_1));
        if (x_screenIntersection_1 >= 0
            && x_screenIntersection_1 <= 700) // If endpoint is on the x-edge
        if (arrow_y2 <= 200) g.drawLine(350, arrow_y2, x_screenIntersection_1, 0);
          else g.drawLine(350, arrow_y2, x_screenIntersection_1, 400);
        else // Else: endpoint is on the y-edge
        if (arrow_x > 350) g.drawLine(350, arrow_y2, 0, y_screenIntersection_1);
        else g.drawLine(350, arrow_y2, 700, y_screenIntersection_1);
      }
      // Line 3/3
      dy_2 = 200 - arrow_y2;
      dx_2 = 350 - arrow_x;
      slope_2 = dy_2 / dx_2;
      y_intercept_2 = 200 - slope_2 * 350;
      if (arrow_x <= 350)
        y_screenIntersection_2 = (int) (Math.round(slope_2 * 700 + y_intercept_2));
      else y_screenIntersection_2 = (int) (Math.round(y_intercept_2));
      if (slope_2 != 0)
        if (arrow_y2 <= 200)
          x_screenIntersection_2 = (int) (Math.round((400 - y_intercept_2) / slope_2));
        else x_screenIntersection_2 = (int) (Math.round(-y_intercept_2 / slope_2));

      if (x_screenIntersection_2 >= 0
          && x_screenIntersection_2 <= 700) // If endpoint is on the x-edge
      if (arrow_y2 <= 200) g.drawLine(arrow_x, arrow_y2, x_screenIntersection_2, 400);
        else g.drawLine(arrow_x, arrow_y2, x_screenIntersection_2, 0);
      else if (arrow_x <= 350)
        g.drawLine(
            arrow_x, arrow_y2, 700, y_screenIntersection_2); // Else: endpoint is on the y-edge
      else g.drawLine(arrow_x, arrow_y2, 0, y_screenIntersection_2);

      // POI between Line 2 & Line 3
      x_pointOfIntersection = (int) ((y_intercept_2 - y_intercept_1) / (slope_1 - slope_2));
      y_pointOfIntersection = (int) (slope_1 * x_pointOfIntersection + y_intercept_1);
      // Draw image
      g.setColor(Color.ORANGE);
      g.drawLine(x_pointOfIntersection, 200, x_pointOfIntersection, y_pointOfIntersection);
      if (y_pointOfIntersection < 200) {
        g.drawLine(
            x_pointOfIntersection,
            y_pointOfIntersection,
            x_pointOfIntersection - 7,
            y_pointOfIntersection + 7);
        g.drawLine(
            x_pointOfIntersection,
            y_pointOfIntersection,
            x_pointOfIntersection + 7,
            y_pointOfIntersection + 7);
      } else {
        g.drawLine(
            x_pointOfIntersection,
            y_pointOfIntersection,
            x_pointOfIntersection - 7,
            y_pointOfIntersection - 7);
        g.drawLine(
            x_pointOfIntersection,
            y_pointOfIntersection,
            x_pointOfIntersection + 7,
            y_pointOfIntersection - 7);
      }
      // Same side image line continuation
      if (((x_pointOfIntersection > 350 && arrow_x > 350)
              || (x_pointOfIntersection < 350 && arrow_x < 350))
          && (arrow_x != 350 - 10 * focalLength && arrow_x != 350 + 10 * focalLength
              || type == 1)) {
        g.setColor(Color.YELLOW);
        g.drawLine(x_pointOfIntersection, y_pointOfIntersection, 350, arrow_y2);
        if (type == 0) g.drawLine(x_pointOfIntersection, y_pointOfIntersection, arrow_x, arrow_y2);
      }

      // Mag calculations
      height_image = 200 - y_pointOfIntersection;
      height_object = 200 - arrow_y2;
      if (height_object != 0) magnification = height_image / height_object;

      if (magnification <= 9999 && magnification >= -9999)
        Optics.txt_magnification.setText("" + roundTwoDecimals(magnification));
      else if (magnification > 9999) {
        magnification = Double.POSITIVE_INFINITY;
        Optics.txt_magnification.setText("N/A");
      } else {
        magnification = Double.NEGATIVE_INFINITY;
        Optics.txt_magnification.setText("N/A");
      }
      // Characteristics
      g.setColor(Color.ORANGE);
      g.drawString("Image Characteristics:", 20, 300);
      if (type == 0) {
        if ((Math.abs(magnification) > 1 && Math.abs(magnification) < 9999))
          g.drawString("Magnification:  Enlarged", 20, 320);
        else if (arrow_x == 350 - 20 * focalLength
            || arrow_x == 350 + 20 * focalLength
            || (int) (Math.abs(magnification)) == 1) g.drawString("Magnification:  None", 20, 320);
        else if (Math.abs(magnification) < 1 && Math.abs(magnification) > 0)
          g.drawString("Magnification:  Diminished", 20, 320);
        else g.drawString("Magnification:  N/A", 20, 320);
        if (arrow_x == 350 - 10 * focalLength || arrow_x == 350 + 10 * focalLength)
          g.drawString("Orientation:      N/A", 20, 335);
        else if ((arrow_y2 < 200 && y_pointOfIntersection < 200)
            || (arrow_y2 > 200 && y_pointOfIntersection > 200))
          g.drawString("Orientation:      Upright", 20, 335);
        else g.drawString("Orientation:      Inverted", 20, 335);
        if (arrow_x == 350 - 10 * focalLength || arrow_x == 350 + 10 * focalLength)
          g.drawString("Type:                 N/A", 20, 350);
        else if ((x_pointOfIntersection < 350 && arrow_x < 350)
            || (x_pointOfIntersection > 350 && arrow_x > 350))
          g.drawString("Type:                 Virtual", 20, 350);
        else g.drawString("Type:                 Real", 20, 350);
      } else {
        g.drawString("Magnification:  Diminished", 20, 320);
        g.drawString("Orientation:      Upright", 20, 335);
        g.drawString("Type:                 Virtual", 20, 350);
      }

      height_image /= 10;

      if (height_image > 9999 || height_image < -9999)
        Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= N/A</html>");
      else Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= " + height_image + "</html>");

      distance_image = x_pointOfIntersection - 350;
      distance_image /= 10;
      if (distance_image > 9999 || distance_image < -9999)
        Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= N/A</html>");
      else Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= " + distance_image + "</html>");
    } else // Else: mirrors
    {

      if (type == 0) // If converging
      {
        // draws converging mirror
        g.drawArc(
            500 - 2 * radiusOfCurvature,
            200 - radiusOfCurvature,
            2 * radiusOfCurvature,
            2 * radiusOfCurvature,
            60,
            -120);
        // draws horizontal line from the tip of the arrow to the lens (line 1/4)
        g.setColor(Color.RED);
        x_arcIntersection_1 =
            (int)
                ((Math.sqrt(Math.abs(Math.pow(radiusOfCurvature, 2) - Math.pow(arrow_y2 - 200, 2))))
                    + (500 - radiusOfCurvature));
        g.drawLine(arrow_x, arrow_y2, x_arcIntersection_1, arrow_y2);

        // line 2/4
        dy_1 = arrow_y2 - 200;
        dx_1 = x_arcIntersection_1 - (500 - focalLength * 10);
        slope_1 = dy_1 / dx_1;
        y_intercept_1 = 200 - slope_1 * (500 - focalLength * 10);

        // Calculates coordinates of points on the edge of screen (endpoints)
        y_screenIntersection_1 = (int) (Math.round(y_intercept_1));
        if (slope_1 != 0)
          if (arrow_y2 <= 200)
            x_screenIntersection_1 = (int) (Math.round((400 - y_intercept_1) / slope_1));
          else x_screenIntersection_1 = (int) (Math.round(-y_intercept_1 / slope_1));
        if (x_screenIntersection_1 >= 0
            && x_screenIntersection_1 <= 700) // If endpoint is on the x-edge
        if (arrow_y2 <= 200) g.drawLine(x_arcIntersection_1, arrow_y2, x_screenIntersection_1, 400);
          else g.drawLine(x_arcIntersection_1, arrow_y2, x_screenIntersection_1, 0);
        else
          g.drawLine(
              x_arcIntersection_1,
              arrow_y2,
              0,
              y_screenIntersection_1); // Else: endpoint is on the y-edge
        // line 3/4
        if (!(arrow_x > 495 - focalLength * 10 && arrow_x < 505 - focalLength * 10)) {
          dy_2 = 200 - arrow_y2;
          dx_2 = (500 - 10 * focalLength) - arrow_x;
          slope_2 = dy_2 / dx_2;
          y_intercept_2 = arrow_y2 - slope_2 * arrow_x;
          quadratic_a = (float) (Math.pow(slope_2, 2) + 1);
          quadratic_b =
              (float)
                  (((2 * slope_2 * y_intercept_2)
                      - (400 * slope_2)
                      + ((radiusOfCurvature - 500) * 2)));
          quadratic_c =
              (float)
                  ((Math.pow(y_intercept_2, 2)
                      - Math.pow(radiusOfCurvature, 2)
                      - (400 * y_intercept_2)
                      + 40000
                      + Math.pow((radiusOfCurvature - 500), 2)));
          discriminant = (float) (Math.pow(quadratic_b, 2) - (4 * quadratic_a * quadratic_c));
          if (discriminant >= 0)
            x_arcIntersection_2 =
                (int)
                    (Math.max(
                        ((-quadratic_b + Math.sqrt(discriminant)) / (2 * quadratic_a)),
                        ((-quadratic_b - Math.sqrt(discriminant)) / (2 * quadratic_a))));
          else System.out.println("Error, imaginary root!");
          y_arcIntersection_2 = (int) (slope_2 * x_arcIntersection_2 + y_intercept_2);
          g.drawLine(arrow_x, arrow_y2, x_arcIntersection_2, y_arcIntersection_2);
          // System.out.println ("slope: " + slope_2 + "\n yintercept: " + y_intercept_2 + "\n
          // quadratic-a: " + quadratic_a + "\n quadratic-b: " + quadratic_b + "\n quadratic_c: " +
          // quadratic_c + "\n discriminant: " + discriminant + "\n xarcintersection2: " +
          // x_arcIntersection_2 + "\n yarcintersection2: " + y_arcIntersection_2);
          // line 4/4
          g.drawLine(x_arcIntersection_2, y_arcIntersection_2, 0, y_arcIntersection_2);

          // POI between line 2 and line 4
          x_pointOfIntersection = (int) ((y_arcIntersection_2 - y_intercept_1) / slope_1);
          y_pointOfIntersection = y_arcIntersection_2;
          g.setColor(Color.ORANGE);
          g.drawLine(x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection, 200);

          if (y_pointOfIntersection < 200) {
            g.drawLine(
                x_pointOfIntersection,
                y_pointOfIntersection,
                x_pointOfIntersection - 7,
                y_pointOfIntersection + 7);
            g.drawLine(
                x_pointOfIntersection,
                y_pointOfIntersection,
                x_pointOfIntersection + 7,
                y_pointOfIntersection + 7);
          } else {
            g.drawLine(
                x_pointOfIntersection,
                y_pointOfIntersection,
                x_pointOfIntersection - 7,
                y_pointOfIntersection - 7);
            g.drawLine(
                x_pointOfIntersection,
                y_pointOfIntersection,
                x_pointOfIntersection + 7,
                y_pointOfIntersection - 7);
          }
          // Same side image line continuation
          if (arrow_x > 500 - 10 * focalLength) {
            g.setColor(Color.YELLOW);
            g.drawLine(x_pointOfIntersection, y_pointOfIntersection, x_arcIntersection_1, arrow_y2);
            g.drawLine(
                x_pointOfIntersection,
                y_pointOfIntersection,
                x_arcIntersection_2,
                y_arcIntersection_2);
          }
        }
      } else // Diverging
      {
        // draws converging mirror
        g.drawArc(
            350, 200 - radiusOfCurvature, 2 * radiusOfCurvature, 2 * radiusOfCurvature, 120, 120);
        // draws horizontal line from the tip of the arrow to the lens (line 1/4)
        g.setColor(Color.RED);
        x_arcIntersection_1 =
            (int)
                (-(Math.sqrt(Math.pow(radiusOfCurvature, 2) - Math.pow(arrow_y2 - 200, 2)))
                    + (350 + radiusOfCurvature));
        g.drawLine(arrow_x, arrow_y2, x_arcIntersection_1, arrow_y2);

        // line 2/4
        dy_1 = arrow_y2 - 200;
        dx_1 = x_arcIntersection_1 - (350 + focalLength * 10);
        slope_1 = dy_1 / dx_1;
        y_intercept_1 = 200 - slope_1 * (350 + focalLength * 10);

        // Calculates coordinates of points on the edge of screen (endpoints)
        y_screenIntersection_1 = (int) (Math.round(y_intercept_1));
        if (slope_1 != 0)
          if (arrow_y2 <= 200)
            x_screenIntersection_1 = (int) (Math.round(-y_intercept_1 / slope_1));
          else if (arrow_y2 > 200)
            x_screenIntersection_1 = (int) (Math.round(400 - y_intercept_1 / slope_1));
        if (x_screenIntersection_1 >= 0
            && x_screenIntersection_1 <= 700) // If endpoint is on the x-edge
        if (arrow_y2 <= 200) g.drawLine(x_arcIntersection_1, arrow_y2, x_screenIntersection_1, 0);
          else g.drawLine(x_arcIntersection_1, arrow_y2, x_screenIntersection_1, 400);
        else
          g.drawLine(
              x_arcIntersection_1,
              arrow_y2,
              0,
              y_screenIntersection_1); // Else: endpoint is on the y-edge
        // line 3/4

        dy_2 = 200 - arrow_y2;
        dx_2 = (350 + 10 * focalLength) - arrow_x;
        slope_2 = dy_2 / dx_2;
        y_intercept_2 = arrow_y2 - slope_2 * arrow_x;
        quadratic_a = (float) (Math.pow(slope_2, 2) + 1);
        quadratic_b =
            (float)
                ((2 * slope_2 * y_intercept_2) - (400 * slope_2) - (2 * radiusOfCurvature + 700));
        quadratic_c =
            (float)
                ((Math.pow(y_intercept_2, 2)
                    - Math.pow(radiusOfCurvature, 2)
                    - (400 * y_intercept_2)
                    + 40000
                    + Math.pow((radiusOfCurvature + 350), 2)));
        discriminant = (float) (Math.pow(quadratic_b, 2) - (4 * quadratic_a * quadratic_c));
        if (discriminant >= 0)
          x_arcIntersection_2 =
              (int)
                  (Math.min(
                      ((-quadratic_b + Math.sqrt(discriminant)) / (2 * quadratic_a)),
                      ((-quadratic_b - Math.sqrt(discriminant)) / (2 * quadratic_a))));
        else System.out.println("Error, imaginary root!");
        y_arcIntersection_2 = (int) (slope_2 * x_arcIntersection_2 + y_intercept_2);
        g.drawLine(arrow_x, arrow_y2, x_arcIntersection_2, y_arcIntersection_2);
        // System.out.println ("slope: " + slope_2 + "\n yintercept: " + y_intercept_2 + "\n
        // quadratic-a: " + quadratic_a + "\n quadratic-b: " + quadratic_b + "\n quadratic_c: " +
        // quadratic_c + "\n discriminant: " + discriminant + "\n xarcintersection2: " +
        // x_arcIntersection_2 + "\n yarcintersection2: " + y_arcIntersection_2);
        // line 4/4
        g.drawLine(x_arcIntersection_2, y_arcIntersection_2, 0, y_arcIntersection_2);

        // POI between line 2 and line 4
        x_pointOfIntersection = (int) ((y_arcIntersection_2 - y_intercept_1) / slope_1);
        y_pointOfIntersection = y_arcIntersection_2;
        g.setColor(Color.ORANGE);
        g.drawLine(x_pointOfIntersection, y_pointOfIntersection, x_pointOfIntersection, 200);

        if (y_pointOfIntersection < 200) {
          g.drawLine(
              x_pointOfIntersection,
              y_pointOfIntersection,
              x_pointOfIntersection - 7,
              y_pointOfIntersection + 7);
          g.drawLine(
              x_pointOfIntersection,
              y_pointOfIntersection,
              x_pointOfIntersection + 7,
              y_pointOfIntersection + 7);
        } else {
          g.drawLine(
              x_pointOfIntersection,
              y_pointOfIntersection,
              x_pointOfIntersection - 7,
              y_pointOfIntersection - 7);
          g.drawLine(
              x_pointOfIntersection,
              y_pointOfIntersection,
              x_pointOfIntersection + 7,
              y_pointOfIntersection - 7);
        }
        // Same side image line continuation
        g.setColor(Color.YELLOW);
        g.drawLine(x_pointOfIntersection, y_pointOfIntersection, x_arcIntersection_1, arrow_y2);
        g.drawLine(
            x_pointOfIntersection, y_pointOfIntersection, x_arcIntersection_2, y_arcIntersection_2);
      }

      // Mag calculations
      height_image = 200 - y_pointOfIntersection;
      height_object = 200 - arrow_y2;
      if (height_object != 0) magnification = height_image / height_object;

      if (magnification <= 9999 && magnification >= -9999)
        Optics.txt_magnification.setText("" + roundTwoDecimals(magnification));
      else if (magnification > 9999) {
        magnification = Double.POSITIVE_INFINITY;
        Optics.txt_magnification.setText("N/A");
      } else {
        magnification = Double.NEGATIVE_INFINITY;
        Optics.txt_magnification.setText("N/A");
      }
      // Characteristics
      g.setColor(Color.ORANGE);
      g.drawString("Image Characteristics:", 20, 300);
      if (type == 0) {

        if ((Math.abs(magnification) > 1 && Math.abs(magnification) < 9999)
            && arrow_x != 500 - 10 * focalLength) g.drawString("Magnification:  Enlarged", 20, 320);
        else if ((int) (Math.abs(magnification)) == 1)
          g.drawString("Magnification:  None", 20, 320);
        else if (Math.abs(magnification) < 1 && Math.abs(magnification) > 0)
          g.drawString("Magnification:  Diminished", 20, 320);
        else {
          g.drawString("Magnification:  N/A", 20, 320);
          Optics.txt_magnification.setText("N/A");
          Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= N/A</html>");
          Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= N/A</html>");
        }
        if (arrow_x == 500 - 10 * focalLength) g.drawString("Orientation:      N/A", 20, 335);
        else if ((arrow_y2 < 200 && y_pointOfIntersection < 200)
            || (arrow_y2 > 200 && y_pointOfIntersection > 200))
          g.drawString("Orientation:      Upright", 20, 335);
        else g.drawString("Orientation:      Inverted", 20, 335);
        if (arrow_x == 500 - 10 * focalLength) g.drawString("Type:                 N/A", 20, 350);
        else if (x_pointOfIntersection < 500 && arrow_x < 500)
          g.drawString("Type:                 Real", 20, 350);
        else if (x_pointOfIntersection > 500 && arrow_x < 500)
          g.drawString("Type:                 Virtual", 20, 350);
      } else {
        g.drawString("Magnification:  Diminished", 20, 320);
        g.drawString("Orientation:      Upright", 20, 335);
        g.drawString("Type:                 Virtual", 20, 350);
      }

      height_image /= 10;

      if (height_image > 9999 || height_image < -9999 || arrow_x == 500 - 10 * focalLength)
        Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= N/A</html>");
      else Optics.lbl_heightImage.setText("<html>h<sub>i</sub>= " + height_image + "</html>");
      if (type == 0) distance_image = x_pointOfIntersection - 500;
      else distance_image = x_pointOfIntersection - 350;
      distance_image /= 10;
      if (distance_image > 9999 || distance_image < -9999 || arrow_x == 500 - 10 * focalLength)
        Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= N/A</html>");
      else Optics.lbl_distanceImage.setText("<html>d<sub>i</sub>= " + distance_image + "</html>");
    }
  }
Example #11
0
  ///////////////////////////////////
  ////                          ////
  //// Valor por Extenso       ////
  ////                        ////
  ///////////////////////////////
  public static String valorPorExtenso(double vlr) {
    if (vlr == 0) {
      return ("zero");
    }

    long inteiro = (long) Math.abs(vlr); // parte inteira do valor
    double resto = vlr - inteiro; // parte fracionária do valor

    String vlrS = String.valueOf(inteiro);
    if (vlrS.length() > 15) {
      return ("Erro: valor superior a 999 trilhões.");
    }

    String s = "", saux, vlrP;
    String centavos = String.valueOf((int) Math.round(resto * 100));

    String[] unidade = {
      "",
      "um",
      "dois",
      "três",
      "quatro",
      "cinco",
      "seis",
      "sete",
      "oito",
      "nove",
      "dez",
      "onze",
      "doze",
      "treze",
      "quatorze",
      "quinze",
      "dezesseis",
      "dezessete",
      "dezoito",
      "dezenove"
    };
    String[] centena = {
      "",
      "cento",
      "duzentos",
      "trezentos",
      "quatrocentos",
      "quinhentos",
      "seiscentos",
      "setecentos",
      "oitocentos",
      "novecentos"
    };
    String[] dezena = {
      "",
      "",
      "vinte",
      "trinta",
      "quarenta",
      "cinquenta",
      "sessenta",
      "setenta",
      "oitenta",
      "noventa"
    };
    String[] qualificaS = {"", "mil", "milhão", "bilhão", "trilhão"};
    String[] qualificaP = {"", "mil", "milhões", "bilhões", "trilhões"};

    // definindo o extenso da parte inteira do valor
    int n, unid, dez, cent, tam, i = 0;
    boolean umReal = false, tem = false;
    while (!vlrS.equals("0")) {
      tam = vlrS.length();
      if (tam > 3) {
        vlrP = vlrS.substring(tam - 3, tam);
        vlrS = vlrS.substring(0, tam - 3);
      } else { // última parte do valor
        vlrP = vlrS;
        vlrS = "0";
      }
      if (!vlrP.equals("000")) {
        saux = "";
        if (vlrP.equals("100")) {
          saux = "cem";
        } else {
          n = Integer.parseInt(vlrP, 10); // para n = 371, tem-se:
          cent = n / 100; // cent = 3 (centena trezentos)
          dez = (n % 100) / 10; // dez  = 7 (dezena setenta)
          unid = (n % 100) % 10; // unid = 1 (unidade um)
          if (cent != 0) {
            saux = centena[cent];
          }
          if ((dez != 0) || (unid != 0)) {
            if ((n % 100) <= 19) {
              if (saux.length() != 0) {
                saux = saux + " e " + unidade[n % 100];
              } else {
                saux = unidade[n % 100];
              }
            } else {
              if (saux.length() != 0) {
                saux = saux + " e " + dezena[dez];
              } else {
                saux = dezena[dez];
              }
              if (unid != 0) {
                if (saux.length() != 0) {
                  saux = saux + " e " + unidade[unid];
                } else {
                  saux = unidade[unid];
                }
              }
            }
          }
        }
        if (vlrP.equals("1") || vlrP.equals("001")) {
          if (i == 0) // 1a. parte do valor (um real)
          {
            umReal = true;
          } else {
            saux = saux + " " + qualificaS[i];
          }
        } else if (i != 0) {
          saux = saux + " " + qualificaP[i];
        }
        if (s.length() != 0) {
          s = saux + ", " + s;
        } else {
          s = saux;
        }
      }
      if (((i == 0) || (i == 1)) && s.length() != 0) {
        tem = true; // tem centena ou mil no valor
      }
      i = i + 1; // próximo qualificador: 1- mil, 2- milhão, 3- bilhão, ...
    }

    if (s.length() != 0) {
      if (umReal) {
        s = s + " real";
      } else if (tem) {
        s = s + " reais";
      } else {
        s = s + " de reais";
      }
    }

    // definindo o extenso dos centavos do valor
    if (!centavos.equals("0")) { // valor com centavos
      if (s.length() != 0) // se não é valor somente com centavos
      {
        s = s + " e ";
      }
      if (centavos.equals("1")) {
        s = s + "um centavo";
      } else {
        n = Integer.parseInt(centavos, 10);
        if (n <= 19) {
          s = s + unidade[n];
        } else { // para n = 37, tem-se:
          unid = n % 10; // unid = 37 % 10 = 7 (unidade sete)
          dez = n / 10; // dez  = 37 / 10 = 3 (dezena trinta)
          s = s + dezena[dez];
          if (unid != 0) {
            s = s + " e " + unidade[unid];
          }
        }
        s = s + " centavos";
      }
    }
    return (s);
  }