Ejemplo n.º 1
1
  /**
   * Draws as much as possible of a given string into a 2d square region in a graphical space.
   *
   * @param g component onto which to draw
   * @param f font to use in drawing the string
   * @param s string to be drawn
   * @param xPos
   * @param yPos
   * @param width
   * @param height
   */
  public static void drawStringMultiline(
      Graphics2D g, Font f, String s, double xPos, double yPos, double width, double height) {
    FontMetrics fm = g.getFontMetrics(f);
    int w = fm.stringWidth(s);
    int h = fm.getAscent();
    // g.setColor(Color.LIGHT_GRAY);
    g.setColor(Color.BLACK);
    g.setFont(f);

    Scanner lineSplitter = new Scanner(s);
    // draw as much as can fit in each item
    // read all content from scanner, storing in string lists (where each string == 1 line), each
    // string should be as long as possible without overflowing the space
    int maxRows = (int) height / h;
    List<String> textRows = new ArrayList<>();
    while (lineSplitter.hasNextLine() && textRows.size() < maxRows) {
      String line = lineSplitter.nextLine();
      // if line is blank, insert to maintain paragraph seps
      if (line.trim().equals("")) {
        textRows.add("");
      }
      // else, pass to inner loop
      StringBuilder currentBuilder = new StringBuilder();
      int currentStrWidth = 0;
      Scanner splitter = new Scanner(line);
      while (splitter.hasNext() && textRows.size() < maxRows) {
        String token = splitter.next() + " ";
        // TODO incorporate weight detection, formatting for token?
        currentStrWidth += fm.stringWidth(token);
        if (currentStrWidth >= width) {
          // if string length >= glyph width, build row
          textRows.add(currentBuilder.toString());
          currentBuilder = new StringBuilder();
          currentBuilder.append(token);
          currentStrWidth = fm.stringWidth(token);
        } else {
          // if not yet at end of row, append to builder
          currentBuilder.append(token);
        }
      }

      // if we've still space and still have things to write, add them here
      if (textRows.size() < maxRows) {
        textRows.add(currentBuilder.toString());
        currentBuilder = new StringBuilder();
        currentStrWidth = 0;
      }
    }

    // write each line to object
    for (int t = 0; t < textRows.size(); t++) {
      String line = textRows.get(t);
      if (fm.stringWidth(line) <= width) {
        // ensure that string doesn't overflow the box
        //                g.drawString(line, (float) (xPos-(width/2.)), (float) (yPos-(height/2.) +
        // h * (t+1)));
        g.drawString(line, (float) xPos, (float) (yPos + h * (t + 1)));
      }
    }
  }
Ejemplo n.º 2
0
  public void paint(Graphics g) {
    m_fm = g.getFontMetrics();

    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());
    getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());

    g.setColor(getForeground());
    g.setFont(getFont());
    m_insets = getInsets();
    int x = m_insets.left;
    int y = m_insets.top + m_fm.getAscent();

    StringTokenizer st = new StringTokenizer(getText(), "\t");
    while (st.hasMoreTokens()) {
      String sNext = st.nextToken();
      g.drawString(sNext, x, y);
      x += m_fm.stringWidth(sNext);

      if (!st.hasMoreTokens()) break;
      int index = 0;
      while (x >= getTab(index)) index++;
      x = getTab(index);
    }
  }
Ejemplo n.º 3
0
  public static int[] getTextDims(Graphics2D g, Font f, String s) {

    // [0] == max width of all lines
    // [1] == total height
    int[] textDims = new int[2];

    FontMetrics fm = g.getFontMetrics(f);
    int lineH = fm.getAscent();

    Scanner lineSplitter = new Scanner(s);
    int maxW = -1;
    int lineCounter = 0;
    while (lineSplitter.hasNextLine()) {
      String line = lineSplitter.nextLine();
      int w = fm.stringWidth(line);
      if (w > maxW) {
        maxW = w;
      }
      lineCounter++;
    }
    int h = lineH * lineCounter;

    textDims[0] = maxW;
    textDims[1] = h;
    return textDims;
  }
Ejemplo n.º 4
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.º 5
0
    public void paintComponent(Graphics g) {
      g.setColor(new Color(96, 96, 96));
      image.paintIcon(this, g, 1, 1);

      FontMetrics fm = g.getFontMetrics();

      String[] args = {jEdit.getVersion()};
      String version = jEdit.getProperty("about.version", args);
      g.drawString(version, (getWidth() - fm.stringWidth(version)) / 2, getHeight() - 5);

      g = g.create((getWidth() - maxWidth) / 2, TOP, maxWidth, getHeight() - TOP - BOTTOM);

      int height = fm.getHeight();
      int firstLine = scrollPosition / height;

      int firstLineOffset = height - scrollPosition % height;
      int lines = (getHeight() - TOP - BOTTOM) / height;

      int y = firstLineOffset;

      for (int i = 0; i <= lines; i++) {
        if (i + firstLine >= 0 && i + firstLine < text.size()) {
          String line = (String) text.get(i + firstLine);
          g.drawString(line, (maxWidth - fm.stringWidth(line)) / 2, y);
        }
        y += fm.getHeight();
      }
    }
Ejemplo n.º 6
0
  private void paintCoords(int xSize, int ySize) {
    float yStep = 1;
    while ((yStep * (ySize - 50) / (threshold - lowThreshold)) < 20) yStep += 1;

    FontMetrics metrics = g.getFontMetrics();
    int height = metrics.getAscent();
    for (float y = lowThreshold; y < threshold; y += yStep) {
      int yCoord = 10 + (int) ((y - lowThreshold) * (ySize - 50) / (threshold - lowThreshold));

      g.setColor(Color.LIGHT_GRAY);
      g.drawLine(45, yCoord, xSize, yCoord);
      g.setColor(Color.BLACK);

      String text = "" + y;
      text = trimStringToLength(text, 5);
      int width = metrics.stringWidth(text);
      g.drawString(text, 40 - width, yCoord + height / 2);
    }
    g.setColor(Color.LIGHT_GRAY);
    g.drawLine(45, ySize - 40, xSize, ySize - 40);
    g.setColor(Color.BLACK);

    String text = "" + threshold;
    text = trimStringToLength(text, 5);
    int width = metrics.stringWidth(text);
    g.drawString(text, 40 - width, ySize - 40 + height / 2);

    g.drawLine(45, 10, 45, ySize - 35);
    g.drawLine(45, ySize - 35, xSize, ySize - 35);
  }
Ejemplo n.º 7
0
  public void paint(java.awt.Graphics g) {
    if (element != null) {
      Rectangle bounds = element.jGetBounds();
      Graphics2D g2 = (Graphics2D) g;

      g2.setFont(font);

      int mitteX = bounds.x + (bounds.width) / 2;
      int mitteY = bounds.y + (bounds.height) / 2;

      int distanceY = 10;

      g2.setColor(new Color(204, 204, 255));
      g2.fillRect(bounds.x, mitteY - distanceY, bounds.width, 2 * distanceY);
      g2.setColor(Color.BLACK);
      g2.drawRect(bounds.x, mitteY - distanceY, bounds.width, 2 * distanceY);

      String caption = "dec(" + variable.getValue() + ")";

      FontMetrics fm = g2.getFontMetrics();
      Rectangle2D r = fm.getStringBounds(caption, g2);

      g2.setColor(Color.BLACK);
      g.drawString(
          caption, mitteX - (int) (r.getWidth() / 2), (int) (mitteY + fm.getHeight() / 2) - 3);
    }
    super.paint(g);
  }
Ejemplo n.º 8
0
 private SelectionDialog(
     RunnerAndConfigurationSettings selectedSettings,
     @NotNull List<RunnerAndConfigurationSettings> settings) {
   super(myProject);
   setTitle(ExecutionBundle.message("before.launch.run.another.configuration.choose"));
   mySelectedSettings = selectedSettings;
   mySettings = settings;
   init();
   myJBList.setSelectedValue(mySelectedSettings, true);
   myJBList.addMouseListener(
       new MouseAdapter() {
         @Override
         public void mouseClicked(MouseEvent e) {
           if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
             doOKAction();
           }
         }
       });
   FontMetrics fontMetrics = myJBList.getFontMetrics(myJBList.getFont());
   int maxWidth = fontMetrics.stringWidth("m") * 30;
   for (RunnerAndConfigurationSettings setting : settings) {
     maxWidth =
         Math.max(fontMetrics.stringWidth(setting.getConfiguration().getName()), maxWidth);
   }
   maxWidth += 24; // icon and gap
   myJBList.setMinimumSize(new Dimension(maxWidth, myJBList.getPreferredSize().height));
 }
Ejemplo n.º 9
0
    /** @see prefuse.render.AbstractShapeRenderer#getRawShape(prefuse.visual.VisualItem) */
    @Override
    protected Shape getRawShape(VisualItem item) {
      double x1 = item.getDouble(VisualItem.X);
      double y1 = item.getDouble(VisualItem.Y);
      double x2 = item.getDouble(VisualItem.X2);
      double y2 = item.getDouble(VisualItem.Y2);
      boolean isX = item.getBoolean(DocumentGridAxisLayout.IS_X);
      double midPoint = item.getDouble(DocumentGridAxisLayout.MID_POINT);
      // horizontal or vertical coords should be manually held constant so that fisheye works
      // properly
      if (isX) {
        // vertical line
        m_line.setLine(x1, y1, x1, y2);
      } else {
        // horizontal line
        m_line.setLine(x1, y1, x2, y1);
      }

      if (!item.canGetString(VisualItem.LABEL)) {
        return m_line;
      }

      String label = item.getString(VisualItem.LABEL);
      if (label == null) {
        return m_line;
      }

      FontMetrics fm = DEFAULT_GRAPHICS.getFontMetrics(item.getFont());
      m_ascent = fm.getAscent();
      int h = fm.getHeight();
      int w = fm.stringWidth(label);

      double tx, ty;

      int labelOffset = 10;
      if (isX) {
        // vertical axis
        // get text x-coord, center at midPoint
        //            tx = x1 + (x2-x1)/2 - w/2;
        //            tx = midPoint + (x1+midPoint)/2 - w/2;
        //            tx = x1 + midPoint/2 - w/2;
        // simpler approach: just add a fixed distance
        tx = x1 + labelOffset;
        // get text y-coord
        ty = y2 - h;
      } else {
        // horiz axis
        // get text x-coord
        tx = x1 - w - 2;
        // get text y-coord, center at midPoint
        //            ty = y1 + (y2-y1)/2 - h/2;
        //            ty = y1 + midPoint/2 - h/2;
        // simpler approach: just add a fixed distance
        ty = y1 + labelOffset;
      }

      m_box.setFrame(tx, ty, w, h);
      return m_box;
    }
Ejemplo n.º 10
0
 public ColorPane() {
   super();
   Font font = new Font("Monospaced", Font.PLAIN, 12);
   FontMetrics fm = getFontMetrics(font);
   lineHeight = fm.getHeight();
   setFont(font);
   setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
 }
Ejemplo n.º 11
0
  /**
   * @param g Graphics context.
   * @param ch The character.
   * @return the width of the character.
   */
  public int charWidth(Graphics g, char ch) {
    FontMetrics fm;
    if (g == null) return 0;

    if (font == null) fm = g.getFontMetrics();
    else fm = g.getFontMetrics(font);

    return fm.charWidth(ch);
  }
 private void setFixedColumnWidth(final int columnIndex, String sampleText) {
   final TableColumn column =
       myEntryTable.getTableHeader().getColumnModel().getColumn(columnIndex);
   final FontMetrics fontMetrics = myEntryTable.getFontMetrics(myEntryTable.getFont());
   final int width = fontMetrics.stringWidth(" " + sampleText + " ") + JBUI.scale(4);
   column.setPreferredWidth(width);
   column.setMinWidth(width);
   column.setResizable(false);
 }
Ejemplo n.º 13
0
  /**
   * Query the font height used in calculating line spacing.
   *
   * @return fontHeight to use to calculate line spacing.
   */
  public int getFontHeight() {
    int retval = fontHeight;

    if (retval < 0) {
      final FontMetrics fontMetrics = getFontMetrics(getFont());
      retval = fontMetrics.getHeight();
      setFontHeight(retval);
    }

    return retval;
  }
Ejemplo n.º 14
0
 void setPosition(int newX, int newY) {
   Insets insets = eNode.getContext().getInsets();
   FontMetrics metrics = getFontMetrics(getFont());
   int stringWidth = metrics.stringWidth(getText());
   int stringHeight = metrics.getHeight();
   setBounds(
       newX + insets.left + eNode.getWidth() + SEPARATION,
       newY + insets.top + eNode.getInsets().top,
       metrics.stringWidth(getText()),
       metrics.getHeight());
 }
Ejemplo n.º 15
0
  /**
   * Query the font width used in calculating line indenting.
   *
   * @return fontWidth to use to calculate line indenting.
   */
  public int getFontWidth() {
    int retval = fontWidth;

    if (fontWidth < 0) {
      final FontMetrics fontMetrics = getFontMetrics(getFont());
      retval = fontMetrics.stringWidth("_");
      setFontWidth(retval);
    }

    return retval;
  }
Ejemplo n.º 16
0
 /**
  * Draws this process as a colored box with a process ID inside.
  *
  * @param g The graphics context.
  * @param x The leftmost x-coordinate of the box.
  * @param y The topmost y-coordinate of the box.
  * @param w The width of the box.
  * @param h The height of the box.
  */
 public void draw(Graphics g, int x, int y, int w, int h) {
   g.setColor(color);
   g.fillRect(x, y, w, h);
   g.setColor(Color.black);
   g.drawRect(x, y, w, h);
   g.setFont(font);
   FontMetrics fm = g.getFontMetrics(font);
   g.drawString(
       "" + processId,
       x + w / 2 - fm.stringWidth("" + processId) / 2,
       y + h / 2 + fm.getHeight() / 2);
 }
Ejemplo n.º 17
0
 private void measure() {
   FontMetrics fontmetrics = getFontMetrics(getFont());
   if (fontmetrics == null) return;
   line_height = fontmetrics.getHeight();
   line_ascent = fontmetrics.getAscent();
   max_width = 0;
   for (int i = 0; i < num_lines; i++) {
     line_widths[i] = fontmetrics.stringWidth(lines[i]);
     if (line_widths[i] > max_width) max_width = line_widths[i];
   }
   max_width += 2 * btnMarginWidth;
   max_height = num_lines * line_height + 2 * btnMarginHeight;
 }
Ejemplo n.º 18
0
 void setPosition(int newX, int newY) {
   Insets insets = getContext().getInsets();
   insets = getContext().getInsets();
   FontMetrics metrics = getFontMetrics(getFont());
   int stringWidth = metrics.stringWidth(getText());
   int stringHeight = metrics.getHeight();
   setBounds(
       newX + insets.left,
       newY + insets.top,
       stringWidth + getInsets().left + getInsets().right,
       stringHeight + insets.top + insets.bottom + VERTICAL_PAD);
   if (rightLabel != null) rightLabel.setPosition(newX, newY);
 }
Ejemplo n.º 19
0
 public Line render(String text, Color c) {
   text = Translate.get(text);
   Coord sz = strsize(text);
   if (sz.x < 1) sz = sz.add(1, 0);
   BufferedImage img = TexI.mkbuf(sz);
   Graphics g = img.createGraphics();
   if (aa) Utils.AA(g);
   g.setFont(font);
   g.setColor(c);
   FontMetrics m = g.getFontMetrics();
   g.drawString(text, 0, m.getAscent());
   g.dispose();
   return (new Line(text, img, m));
 }
Ejemplo n.º 20
0
 /* paint() - get current time and draw (centered) in Component. */
 public void paint(Graphics g) {
   Calendar myCal = Calendar.getInstance();
   StringBuffer sb = new StringBuffer();
   sb.append(tf.format(myCal.get(Calendar.HOUR)));
   sb.append(':');
   sb.append(tflz.format(myCal.get(Calendar.MINUTE)));
   sb.append(':');
   sb.append(tflz.format(myCal.get(Calendar.SECOND)));
   String s = sb.toString();
   FontMetrics fm = getFontMetrics(getFont());
   int x = (getSize().width - fm.stringWidth(s)) / 2;
   // System.out.println("Size is " + getSize());
   g.drawString(s, x, 10);
 }
Ejemplo n.º 21
0
  private void recalculateDimension() {
    FontMetrics fontmetrics = getFontMetrics(getFont());

    lineHeight = fontmetrics.getHeight();
    lineAscent = fontmetrics.getAscent();

    maxWidth = 0;
    for (int i = 0; i < numLines; i++) {
      lineWidths[i] = fontmetrics.stringWidth(lines[i]);

      maxWidth = Math.max(maxWidth, lineWidths[i]);
    }

    maxWidth += 2 * btnMarginWidth;
    textHeight = numLines * lineHeight;
  }
Ejemplo n.º 22
0
  // draws the tree, starting from the given node, in the region with x values
  // ranging
  // from minX to maxX, with y value beginning at y, and next level at y +
  // yIncr.
  private void drawTree(Graphics2D g2, TreeNode<E> t, int minX, int maxX, int y, int yIncr) {
    // skip if empty
    if (t == null) return;

    // compute useful coordinates
    int x = (minX + maxX) / 2;
    int nextY = y + yIncr;

    // draw black lines
    g2.setPaint(Color.black);
    if (t.left != null) {
      int nextX = (minX + x) / 2;
      g2.draw(new Line2D.Double(x, y, nextX, nextY));
    }
    if (t.right != null) {
      int nextX = (x + maxX) / 2;
      g2.draw(new Line2D.Double(x, y, nextX, nextY));
    }

    // measure text
    FontMetrics font = g2.getFontMetrics();
    String text = t.data + "";
    int textHeight = font.getHeight();
    int textWidth = font.stringWidth(text);

    // draw the box around the node
    Rectangle2D.Double box =
        new Rectangle2D.Double(
            x - textWidth / 2 - ARC_PAD,
            y - textHeight / 2 - ARC_PAD,
            textWidth + 2 * ARC_PAD,
            textHeight + 2 * ARC_PAD);
    Color c = new Color(187, 224, 227);
    g2.setPaint(c);
    g2.fill(box);
    // draw black border
    g2.setPaint(Color.black);
    g2.draw(box);

    // draw text
    g2.drawString(text, x - textWidth / 2, y + textHeight / 2);

    // draw children
    drawTree(g2, t.left, minX, x, nextY, yIncr);
    drawTree(g2, t.right, x, maxX, nextY, yIncr);
  }
Ejemplo n.º 23
0
      public void run() {
        FontMetrics fm = getFontMetrics(getFont());
        int max = (text.size() * fm.getHeight());

        while (running) {
          scrollPosition += 2;

          if (scrollPosition > max) scrollPosition = -250;

          try {
            Thread.sleep(100);
          } catch (Exception e) {
          }

          repaint(getWidth() / 2 - maxWidth, TOP, maxWidth * 2, getHeight() - TOP - BOTTOM);
        }
      }
Ejemplo n.º 24
0
    /** @see prefuse.render.Renderer#render(java.awt.Graphics2D, prefuse.visual.VisualItem) */
    @Override
    public void render(Graphics2D g, VisualItem item) {
      Shape s = getShape(item);
      GraphicsLib.paint(g, item, m_line, getStroke(item), getRenderType(item));

      // check if we have a text label, if so, render it
      String str;
      if (item.canGetString(VisualItem.LABEL)) {
        str = (String) item.getString(VisualItem.LABEL);
        if (str != null && !str.equals("")) {
          float x = (float) m_box.getMinX();
          float y = (float) m_box.getMinY() + m_ascent;

          // draw label background
          GraphicsLib.paint(g, item, s, null, RENDER_TYPE_FILL);

          AffineTransform origTransform = g.getTransform();
          AffineTransform transform = this.getTransform(item);
          if (transform != null) {
            g.setTransform(transform);
          }

          g.setFont(item.getFont());
          g.setColor(ColorLib.getColor(item.getTextColor()));

          if (!(str.length() > 5
              && str.substring(str.length() - 5, str.length()).equals("_last"))) {

            g.setColor(Color.WHITE);
            // TODO properly hunt down source of null str! for now, triage
            if (str != null) {
              // bump y down by appropriate amount
              FontMetrics fm = g.getFontMetrics(item.getFont());
              int strHeight = fm.getAscent();
              //                        g.drawString(str, x, y);
              g.drawString(str, x, y + strHeight);
            }

            if (transform != null) {
              g.setTransform(origTransform);
            }
          }
        }
      }
    }
Ejemplo n.º 25
0
 static void paintDropShadowText(
     final Graphics g,
     final JComponent c,
     final Font font,
     final FontMetrics metrics,
     final int x,
     final int y,
     final int offsetX,
     final int offsetY,
     final Color textColor,
     final Color shadowColor,
     final String text) {
   g.setFont(font);
   g.setColor(shadowColor);
   SwingUtilities2.drawString(c, g, text, x + offsetX, y + offsetY + metrics.getAscent());
   g.setColor(textColor);
   SwingUtilities2.drawString(c, g, text, x, y + metrics.getAscent());
 }
Ejemplo n.º 26
0
  private void drawTile(Graphics g2, Tile tile, int x, int y) {
    Graphics2D g = ((Graphics2D) g2);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    int value = tile.value;
    int xOffset = offsetCoors(x);
    int yOffset = offsetCoors(y);
    g.setColor(tile.getBackground());
    g.fillRoundRect(xOffset, yOffset, TILE_SIZE, TILE_SIZE, 14, 14);
    g.setColor(tile.getForeground());
    final int size = value < 100 ? 36 : value < 1000 ? 32 : 24;
    final Font font = new Font(FONT_NAME, Font.BOLD, size);
    g.setFont(font);

    String s = String.valueOf(value);
    final FontMetrics fm = getFontMetrics(font);

    final int w = fm.stringWidth(s);
    final int h = -(int) fm.getLineMetrics(s, g).getBaselineOffsets()[2];

    if (value != 0)
      g.drawString(s, xOffset + (TILE_SIZE - w) / 2, yOffset + TILE_SIZE - (TILE_SIZE - h) / 2 - 2);

    if (myWin || myLose) {
      g.setColor(new Color(255, 255, 255, 30));
      g.fillRect(0, 0, getWidth(), getHeight());
      g.setColor(new Color(78, 139, 202));
      g.setFont(new Font(FONT_NAME, Font.BOLD, 48));
      if (myWin) {
        g.drawString("You won!", 68, 150);
      }
      if (myLose) {
        g.drawString("Game over!", 50, 130);
        g.drawString("You lose!", 64, 200);
      }
      if (myWin || myLose) {
        g.setFont(new Font(FONT_NAME, Font.PLAIN, 16));
        g.setColor(new Color(128, 128, 128, 128));
        g.drawString("Press ESC to play again", 80, getHeight() - 40);
      }
    }
    g.setFont(new Font(FONT_NAME, Font.PLAIN, 18));
    g.drawString("Score: " + myScore, 200, 365);
  }
  private static void setColumnWidths(JTable table, MetricTableSpecification tableSpecification) {
    final TableModel model = table.getModel();
    final TableColumnModel columnModel = table.getColumnModel();

    final List<Integer> columnWidths = tableSpecification.getColumnWidths();
    final List<String> columnOrder = tableSpecification.getColumnOrder();
    if (columnWidths != null && !columnWidths.isEmpty()) {

      final int columnCount = model.getColumnCount();
      for (int i = 0; i < columnCount; i++) {
        final String columnName = model.getColumnName(i);
        final int index = columnOrder.indexOf(columnName);
        if (index != -1) {
          final Integer width = columnWidths.get(index);
          final TableColumn column = columnModel.getColumn(i);
          column.setPreferredWidth(width.intValue());
        }
      }
    } else {
      final Graphics graphics = table.getGraphics();
      final Font font = table.getFont();
      final FontMetrics fontMetrics = table.getFontMetrics(font);

      final int rowCount = model.getRowCount();
      int maxFirstColumnWidth = 100;
      for (int i = 0; i < rowCount; i++) {
        final String name = (String) model.getValueAt(i, 0);
        if (name != null) {
          final Rectangle2D stringBounds = fontMetrics.getStringBounds(name, graphics);
          final double stringWidth = stringBounds.getWidth();
          if (stringWidth > maxFirstColumnWidth) {
            maxFirstColumnWidth = (int) stringWidth;
          }
        }
      }

      final int allocatedFirstColumnWidth = Math.min(300, maxFirstColumnWidth + 5);
      final TableColumn column = columnModel.getColumn(0);
      column.setPreferredWidth(allocatedFirstColumnWidth);
    }
  }
Ejemplo n.º 28
0
  /**
   * Called to paint the outline.
   *
   * @param g graphics object to paint.
   */
  public void paint(final Graphics g) {
    final FontMetrics fontMetrics = getFontMetrics(getFont());
    setFontWidth(fontMetrics.stringWidth("_"));
    setFontHeight(fontMetrics.getHeight());

    if (firstTime) {
      firstTime = false;
      setVisible("Outline".equalsIgnoreCase(djvuBean.properties.getProperty("navpane")));
    }

    if (!isVisible()) {
      getParent().setVisible(false);
      invalidate();
      djvuBean.recursiveRevalidate();
    } else {
      synchronized (activeVector) {
        paintItem(0, g, getBookmark(0));
        paintCheckbox(0, g, getBookmark(0));
      }
    }
  }
Ejemplo n.º 29
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.º 30
0
  // called whenever the TreeDisplay must be drawn on the screen
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Dimension d = getSize();

    // draw white background
    g2.setPaint(Color.white);
    g2.fill(new Rectangle2D.Double(0, 0, d.width, d.height));

    int depth = h();

    if (root == null)
      // no tree to draw
      return;

    // hack to avoid division by zero, if only one level in tree
    if (depth == 1) depth = 2;

    // compute the size of the text
    FontMetrics font = g2.getFontMetrics();
    TreeNode<E> leftmost = root;
    while (leftmost.left != null) leftmost = leftmost.left;
    TreeNode<E> rightmost = root;
    while (rightmost.right != null) rightmost = rightmost.right;
    int leftPad = font.stringWidth(leftmost.data + "") / 2;
    int rightPad = font.stringWidth(rightmost.data + "") / 2;
    int textHeight = font.getHeight();

    // draw the actual tree
    drawTree(
        g2,
        root,
        leftPad + ARC_PAD,
        d.width - rightPad - ARC_PAD,
        textHeight / 2 + ARC_PAD,
        (d.height - textHeight - 2 * ARC_PAD) / (depth - 1));
  }