Ejemplo n.º 1
0
 void drawRoiLabel(Graphics g, int index, Roi roi) {
   Rectangle r = roi.getBounds();
   int x = screenX(r.x);
   int y = screenY(r.y);
   double mag = getMagnification();
   int width = (int) (r.width * mag);
   int height = (int) (r.height * mag);
   int size = width > 40 && height > 40 ? 12 : 9;
   if (font != null) {
     g.setFont(font);
     size = font.getSize();
   } else if (size == 12) g.setFont(largeFont);
   else g.setFont(smallFont);
   boolean drawingList = index >= LIST_OFFSET;
   if (drawingList) index -= LIST_OFFSET;
   String label = "" + (index + 1);
   if (drawNames && roi.getName() != null) label = roi.getName();
   FontMetrics metrics = g.getFontMetrics();
   int w = metrics.stringWidth(label);
   x = x + width / 2 - w / 2;
   y = y + height / 2 + Math.max(size / 2, 6);
   int h = metrics.getAscent() + metrics.getDescent();
   if (bgColor != null) {
     g.setColor(bgColor);
     g.fillRoundRect(x - 1, y - h + 2, w + 1, h - 3, 5, 5);
   }
   if (!drawingList && labelRects != null && index < labelRects.length)
     labelRects[index] = new Rectangle(x - 1, y - h + 2, w + 1, h);
   g.setColor(labelColor);
   g.drawString(label, x, y - 2);
   g.setColor(defaultColor);
 }
Ejemplo n.º 2
0
  /** This internal method begins a new page and prints the header. */
  protected void newpage() {
    page = job.getGraphics(); // Begin the new page
    linenum = 0;
    charnum = 0; // Reset line and char number
    pagenum++; // Increment page number
    page.setFont(headerfont); // Set the header font.
    page.drawString(jobname, x0, headery); // Print job name left justified

    String s = "- " + pagenum + " -"; // Print the page number centered.
    int w = headermetrics.stringWidth(s);
    page.drawString(s, x0 + (this.width - w) / 2, headery);
    w = headermetrics.stringWidth(time); // Print date right justified
    page.drawString(time, x0 + width - w, headery);

    // Draw a line beneath the header
    int y = headery + headermetrics.getDescent() + 1;
    page.drawLine(x0, y, x0 + width, y);

    // Set the basic monospaced font for the rest of the page.
    page.setFont(font);
  }
Ejemplo n.º 3
0
 public int height() {
   /* XXX: Should leading go into this, when it's mostly
    * supposed to be used for one-liners? */
   return (m.getAscent() + m.getDescent());
 }
Ejemplo n.º 4
0
  /**
   * Draw the text and graph.
   *
   * @param g
   */
  @Override
  public void paintComponent(Graphics g) {
    locations.clear();

    // Background
    g.setColor(background_color);
    g.fillRect(0, 0, getWidth(), getHeight());

    // This color is used for everything until drawing the points
    g.setColor(foreground_color);

    // Anti-Aliasing
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Font
    FontMetrics fontMetrics = g.getFontMetrics(FONT);
    int fontHeight = fontMetrics.getHeight();
    g.setFont(FONT);

    int topTextY = fontMetrics.getAscent();

    // Margins
    int vMargin = fontHeight + MARGIN;
    int hMargin = MARGIN;

    // Calculate actual usable size
    double width = getWidth() - hMargin * 2;
    double height = getHeight() - vMargin * 2;

    boolean drawLowerLine = height > -vMargin;

    // If there is any data and no hovered entry is shown, draw current
    // viewercount
    int nowTextX = 0;
    if (history != null && hoverEntry == -1) {
      Integer viewers = history.get(endTime).getViewers();
      long ago = System.currentTimeMillis() - endTime;
      String text;
      if (ago > CONSIDERED_AS_NOW) {
        text = "latest: " + Helper.formatViewerCount(viewers);
      } else {
        text = "now: " + Helper.formatViewerCount(viewers);
      }
      if (viewers == -1) {
        text = "Stream offline";
      }
      nowTextX = getWidth() - fontMetrics.stringWidth(text);
      g.drawString(text, nowTextX, topTextY);
    }

    // Default text when no data is present
    if (history == null || history.size() < 2) {
      String text = "No viewer history yet";
      int textWidth = fontMetrics.stringWidth(text);
      int y = getHeight() / 2 + fontMetrics.getDescent();
      int x = (getWidth() - textWidth) / 2;
      boolean drawInfoText = false;
      if (history != null && y < topTextY + fontHeight + 4 && x + textWidth + 7 > nowTextX) {
        if (drawLowerLine || nowTextX > textWidth + 5) {
          if (drawLowerLine) {
            y = getHeight() - 2;
          } else {
            y = topTextY;
          }
          x = 0;
          drawInfoText = true;
        }
      } else {
        drawInfoText = true;
      }
      if (drawInfoText) {
        g.drawString(text, x, y);
      }
      return;
    }

    // ----------
    // From here only when actual data is to be rendered

    // Show info on hovered entry
    String maxValueText = "max: " + Helper.formatViewerCount(maxValue);
    int maxValueEnd = fontMetrics.stringWidth(maxValueText);
    boolean displayMaxValue = true;

    if (hoverEntry != -1) {
      Integer viewers = history.get(hoverEntry).getViewers();
      Date d = new Date(hoverEntry);
      String text = "Viewers: " + Helper.formatViewerCount(viewers) + " (" + sdf.format(d) + ")";
      if (viewers == -1) {
        text = "Stream offline (" + sdf.format(d) + ")";
      }
      int x = getWidth() - fontMetrics.stringWidth(text);
      if (maxValueEnd > x) {
        displayMaxValue = false;
      }
      g.drawString(text, x, topTextY);
    }

    String minText = "min: " + Helper.formatViewerCount(minValue);
    int minTextWidth = fontMetrics.stringWidth(minText);

    // Draw Times
    if (drawLowerLine) {
      String timeText = makeTimesText(startTime, endTime);
      int timeTextWidth = fontMetrics.stringWidth(timeText);
      int textX = getWidth() - timeTextWidth;
      g.drawString(timeText, textX, getHeight() - 1);

      if (minValue >= 1000 && timeTextWidth + minTextWidth > width) {
        minText = "min: " + minValue / 1000 + "k";
      }
    }

    // Draw min/max if necessary
    if (isShowingInfo()) {
      if (displayMaxValue) {
        g.drawString(maxValueText, 0, topTextY);
      }
      if (drawLowerLine) {
        g.drawString(minText, 0, getHeight() - 1);
      } else if (maxValueEnd + minTextWidth + 29 < nowTextX) {
        g.drawString(minText, maxValueEnd + 10, topTextY);
      }
    }

    // If height available for the graph is too small, don't draw graph
    if (height < 5) {
      return;
    }

    // Calculation factors for calculating the points location
    int range = maxValue - minValue;
    if (showFullRange) {
      range = maxValue;
    }
    if (range == 0) {
      // Prevent division by zero
      range = 1;
    }
    double pixelPerViewer = height / range;
    double pixelPerTime = width / duration;

    // Go through all entries and calculate positions

    int prevX = -1;
    int prevY = -1;
    Iterator<Entry<Long, StreamInfoHistoryItem>> it = history.entrySet().iterator();
    while (it.hasNext()) {
      Entry<Long, StreamInfoHistoryItem> entry = it.next();

      // Get time and value to draw next
      long time = entry.getKey();
      if (time < startTime || time > endTime) {
        continue;
      }
      long offsetTime = time - startTime;

      int viewers = entry.getValue().getViewers();
      if (viewers == -1) {
        viewers = 0;
      }

      // Calculate point location
      int x = (int) (hMargin + offsetTime * pixelPerTime);
      int y;
      if (showFullRange) {
        y = (int) (-vMargin + getHeight() - (viewers) * pixelPerViewer);
      } else {
        y = (int) (-vMargin + getHeight() - (viewers - minValue) * pixelPerViewer);
      }

      // Draw connecting line
      if (prevX != -1) {
        g.drawLine(x, y, prevX, prevY);
      }

      // Save point coordinates to be able to draw the line next loop
      prevX = x;
      prevY = y;

      // Save point locations to draw points and to find entries on hover
      locations.put(new Point(x, y), time);
    }

    // Draw points (after lines, so they are in front)
    for (Point point : locations.keySet()) {
      int x = point.x;
      int y = point.y;
      long seconds = locations.get(point);

      StreamInfoHistoryItem historyObject = history.get(seconds);

      // Highlight hovered entry
      if (seconds == hoverEntry) {
        g.setColor(HOVER_COLOR);
      } else {
        // Draw offline points differently
        if (!historyObject.isOnline()) {
          g.setColor(OFFLINE_COLOR);
        } else {
          g.setColor(colors.get(seconds));
        }
      }
      g.fillOval(x - POINT_SIZE / 2, y - POINT_SIZE / 2, POINT_SIZE, POINT_SIZE);
    }
  }
  public void paintRegion(Graphics2D g, int x1, int y1, int w, int h) {
    int h2 = h / 2;
    int rad = 3;
    int diam = rad * 2;

    Color lg = Color.cyan;
    Color dg = Color.orange;

    lg = new Color(lg.getRed(), lg.getGreen(), lg.getBlue(), 75);
    dg = new Color(dg.getRed(), dg.getGreen(), dg.getBlue(), 75);

    /*
     * Draw the Baseline
     */
    g.setColor(Color.black);
    g.drawLine(x1, y1 + h, x1 + w, y1 + h);

    Stroke oldStroke = g.getStroke();
    g.setStroke(new BasicStroke((float) 2.0));

    /*
     * Draw the datapoints
     */
    for (ExprPoint ep : points) {
      if (ep.strand == '+') {
        g.setColor(lg);
      } else {
        g.setColor(dg);
      }

      g.drawOval(x1 + ep.x - rad, y1 + ep.y - rad, diam, diam);
    }

    g.setStroke(oldStroke);

    /*
     * Paint the hash marks...
     */
    if (!displayOppositeChannel) {
      g.setColor(Color.black);
      boolean flipper = true;
      for (int value = 100;
          value <= scale.getMax();
          value *= (flipper ? 5 : 2), flipper = !flipper) {
        int yoff = getYOffset((double) value);
        String line = String.format("%d", value);
        int uy = y1 + h2 - yoff, ly = y1 + h2 + yoff;

        g.drawLine(x1, uy, x1 + 10, uy);
        g.drawString(line, x1 + 12, uy + 5);

        g.drawLine(x1, ly, x1 + 10, ly);
        g.drawString(line, x1 + 12, ly + 5);
      }
    }

    /*
     * Draw any selections.
     */

    g.setColor(Color.black);
    for (Point p : selections.keySet()) {
      ExprPoint ep = selections.get(p);
      g.drawLine(p.x, p.y, ep.x, ep.y);
      g.drawString(ep.getLabel(), p.x, p.y);
    }

    /*
     * Draw the label in the upper-right hand corner.
     */
    g.setColor(Color.black);
    Font oldFont = g.getFont();
    Font newFont = new Font("Arial", Font.BOLD, 24);
    g.setFont(newFont);
    FontMetrics fm = g.getFontMetrics();
    int lblHeight = fm.getAscent() + fm.getDescent();
    int lblWidth = fm.charsWidth(label.toCharArray(), 0, label.length());

    int padding = 5;
    int lblx = x1 + w - lblWidth - padding;
    int lbly = y1 + lblHeight + padding;

    g.drawString(label, lblx, lbly);

    g.setFont(oldFont);
  }
Ejemplo n.º 6
0
  public void paint(Graphics g) {
    Rectangle r = bounds();

    // paint the box
    int width = r.width - TF_LEFT + TF_RIGHT;
    if (width < 0) width = 0;
    int height = r.height - TF_TOP + TF_BOTTOM;
    if (height < 0) height = 0;

    if (r.width > lastWidth) firstVisibleTab = 0;
    lastWidth = r.width;

    int col = TF_LEFT;
    int row;

    Color c = g.getColor();
    g.setColor(getBackground());
    g.fillRect(0, 0, r.width, r.height);

    if (iTabsPosition == TOP) row = TF_TOP;
    else row = TF_TOP - TF_BTN_HEIGHT;

    // draw border
    g.setColor(Color.white);
    g.drawLine(col, row, (col + width - 1), row);
    g.drawLine(col, row, col, (row + height - 1));

    g.setColor(Color.gray);
    g.drawLine((col + 2), (row + height - 2), (col + width - 2), (row + height - 2));
    g.drawLine((col + width - 2), (row + 2), (col + width - 2), (row + height - 2));

    g.setColor(Color.black);
    g.drawLine((col + 1), (row + height - 1), (col + width - 1), (row + height - 1));
    g.drawLine((col + width - 1), (row + 1), (col + width - 1), (row + height - 1));

    // paint the tabs, and record areas
    int x1;
    int x2 = TF_LEFT + 8;
    int y1;
    int y2;
    int x3 = 0;
    int x4 = TF_LEFT;

    int sze = pages.size();
    String sLabel;

    Font f = g.getFont();
    FontMetrics fm = getFontMetrics(fReg);
    FontMetrics fms = getFontMetrics(fSel);
    int labelWidth = 0;
    Polygon p;

    int w;
    NotebookPage[] npages = new NotebookPage[sze];
    for (w = 0; w < sze; ++w) {
      npages[w] = (NotebookPage) pages.elementAt(w);
      npages[w].poly = nullPoly;
    }

    // make sure there is a polygon for each tab
    if (firstVisibleTab > 0) x4 += 2;
    int xStep = 1;

    for (w = firstVisibleTab; w < sze; w++) {
      int fheight = fm.getHeight() - fms.getDescent();
      if (w == curIndex) fheight = fms.getHeight() - fms.getDescent();

      p = new Polygon();
      if (npages[w].hidden) {
        y1 = TF_TOP - TF_BTN_HEIGHT;
        y2 = TF_TOP - 1;
        x1 = x4 - 1;
        x2 = x1 + 1;
        x3 = x1;
        x4 = x2;
        p.addPoint(x1, y1);
        p.addPoint(x1, y2);
        p.addPoint(x2, y2);
        p.addPoint(x2, y1);
        npages[w].poly = p;
        //          xStep++;
        continue;
      }
      try {
        sLabel = npages[w].label;
        if (w == curIndex) labelWidth = fms.stringWidth(sLabel);
        else labelWidth = fm.stringWidth(sLabel);

        if (npages[w].img != null) labelWidth += IMAGE_SIZE;

        if (iTabsPosition == TOP) {
          y1 = TF_TOP - TF_BTN_HEIGHT;
          y2 = TF_TOP - 1;
        } else {
          y1 = r.height + TF_BOTTOM + 1;
          y2 = r.height + TF_BOTTOM - TF_BTN_HEIGHT;
        }

        if (iTabsStyle == ROUNDED) {
          x1 = x4 + 2;
          x2 = x1 + labelWidth + 13;
        } else {
          x1 = x2 - 7;
          x2 = x1 + labelWidth + 28;
        }

        // check to see if this tab would draw too far
        if ((x2 + 36 - TF_RIGHT) > r.width) break;

        // draw the outside edge of the tab
        if (iTabsPosition == TOP) {
          // if current tab, it extends further
          if (w == curIndex) {
            y1 -= 3;
            x1 -= 2;
          }
          g.setColor(Color.white);
          g.drawLine(x1 + 2, y1, x2, y1);

          // draw the border between tabs if not covered by the current one
          g.drawLine(x1, y1 + 2, x1, y2);
          x3 = x1;

          g.drawLine(x1 + 1, y1 + 1, x1 + 1, y1 + 1);

          g.setColor(Color.gray);
          g.drawLine(x2, y1, x2, y2);
          g.setColor(Color.black);
          g.drawLine(x2 + 1, y1 + 2, x2 + 1, y2);
          x4 = x2;
        } else {
          if (iTabsStyle == SQUARE) {
            g.setColor(Color.gray);
            g.drawLine(x1 + 9, y1, x2 - 9, y1);

            g.setColor(Color.black);
            // left \ slanted line
            if (w == 0 || w == curIndex) {
              g.drawLine(x1, y2, x1 + 9, y1);
              p.addPoint(x1, y2);
            } else {
              g.drawLine(x1 + 4, y1 - 9, x1 + 9, y1);
              p.addPoint(x1 + 9, y2);
              p.addPoint(x1 + 4, y1 - 9);
            }
            p.addPoint(x1 + 9, y1);
            p.addPoint(x2 - 9, y1);

            if ((w + xStep) == curIndex) {
              g.drawLine(x2 - 5, y1 - 9, x2 - 9, y1);
              p.addPoint(x2 - 5, y1);
              p.addPoint(x2 - 9, y2);
            } else {
              g.drawLine(x2, y2, x2 - 9, y1);
              p.addPoint(x2, y2);
            }

            if (w == 1 || w == curIndex) p.addPoint(x1, y2);
            else p.addPoint(x1 + 9, y2);
          } else {
            // if current tab, it extends further
            if (w == curIndex) {
              y1 += 3;
              x1 -= 2;
            }
            g.setColor(Color.white);
            if (curIndex == (w + xStep)) g.drawLine(x1 + 2, y1, x2 - 2, y1);
            else g.drawLine(x1 + 2, y1, x2, y1);

            // draw the border between tabs if not covered by the current one
            if (curIndex != (w - xStep)) {
              g.drawLine(x1, y1 - 2, x1, y2);
              x3 = x1;
            } else x3 = x1 + 1;

            g.drawLine(x1 + 1, y1 - 1, x1 + 1, y1 - 1);

            if (curIndex != (w + xStep)) {
              g.setColor(Color.gray);
              g.drawLine(x2, y1, x2, y2);
              g.setColor(Color.black);
              g.drawLine(x2 + 1, y1 - 2, x2 + 1, y2);
              x4 = x2;
            } else x4 = x2 - 1;
          }
        }

        // draw the inside edge of the tab
        if (w == curIndex) {
          if (iTabsPosition == TOP) ++y2;
          else --y2;
          g.setColor(getBackground());
          g.drawLine(x1 + 1, y2, x2, y2);
          if (iTabsPosition == BOTTOM) g.drawLine(x1 + 1, y2 - 1, x2, y2 - 1);

          g.setFont(fSel);
        } else g.setFont(fReg);

        // if (iTabsPosition == TOP)
        if (iTabsStyle == ROUNDED) {
          p.addPoint(x3 - 1, y2 + 1);
          p.addPoint(x4 + 1, y2 + 1);
          p.addPoint(x4 + 1, y1 - 1);
          p.addPoint(x3 + 2, y1 - 1);
          p.addPoint(x3 - 1, y1 + 2);
          p.addPoint(x3 - 1, y2 + 1);
        }
        npages[w].poly = p;

        g.setColor(npages[w].color);
        Polygon p2 = justPolygon(p, iTabsPosition == TOP);
        g.fillPolygon(p2);

        // Boolean bool = (Boolean) vEnabled.elementAt(w);
        // if (bool.booleanValue())
        g.setColor(caption);
        // else
        //     g.setColor(Color.gray);

        int dx = (npages[w].img == null) ? 0 : IMAGE_SIZE;

        int xx = x1 + 8 + dx;
        int yy = y1 + 15;

        if (iTabsStyle == TOP) {
        } else if (iTabsStyle == ROUNDED) {
          yy = y1 - 6;
        } else {
          xx = x1 + 14 + dx;
          yy = y1 - 4;
        }

        int imgy = yy - fheight / 2;

        if (npages[w].img != null) {
          int imgH = npages[w].img.getHeight(this);
          int imgW = npages[w].img.getWidth(this);
          imgy = imgy - imgH / 2 + 1;
          g.drawImage(npages[w].img, xx - IMAGE_SIZE - 2, imgy, imgW, imgH, this);
        }

        g.drawString(sLabel, xx, yy);
      } catch (ArrayIndexOutOfBoundsException e) {
      }
      xStep = 1;
    }

    // do I need to show arrows because there are too many tabs???
    if ((firstVisibleTab > 0) || (w < sze)) {
      dbLeft.show();
      dbRight.show();
      if (firstVisibleTab > 0) dbLeft.enable();
      else dbLeft.disable();

      if (w < sze) dbRight.enable();
      else dbRight.disable();
    } else {
      dbLeft.hide();
      dbRight.hide();
    }
    g.setFont(f);
    g.setColor(c);

    if (mark && curIndex >= 0) drawMark(g, npages[curIndex].poly);

    npages = null;
  }