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); }
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); } }
public void drawVictory(Graphics g) { g.setColor(new Color(0, 0, 0, 200)); g.fillRect(195, 220, 410, 110); g.setColor(new Color(255, 255, 255, 200)); g.fillRect(200, 225, 400, 100); g.setColor(new Color(0, 0, 0, 200)); g.setFont(new Font("Bell MT", Font.BOLD, 20)); FontMetrics metrics = g.getFontMetrics(new Font("Bell MT", Font.BOLD, 20)); int hgt = metrics.getHeight(); String initialMessage; String followupMessage; if (nextWindow) { initialMessage = "You have gotten stronger."; followupMessage = player.getName() + " is now level " + player.getLevel() + "!"; } else { initialMessage = "You survived!"; followupMessage = "You and your allies gain " + totalExperience + " experience!"; } // Hgt = 26 int adv = metrics.stringWidth(initialMessage); g.drawString(initialMessage, getWidth() / 2 - adv / 2, 234 + hgt); adv = metrics.stringWidth(followupMessage); g.drawString(followupMessage, getWidth() / 2 - adv / 2, 269 + hgt); }
/** * This is called to paint within the EditCanvas * * @param g Graphics * @param c DisplayCanvas to paint on. */ public void paint(Graphics g, DisplayCanvas c) { Rectangle nb = transformOutput(c, getBounds()); if (!getActive()) { Rectangle r = getBounds(); g.setColor(Color.lightGray); g.fillRect(r.x, r.y, r.width, r.height); } draw((Graphics2D) g, nb.x, nb.y, nb.width, nb.height); if ((c instanceof StationModelCanvas) && ((StationModelCanvas) c).getShowParams()) { int xoff = 0; FontMetrics fm = g.getFontMetrics(); g.setColor(Color.black); for (int i = 0; i < paramIds.length; i++) { String s = paramIds[i]; if (i > 0) { s = ", " + s; } g.drawString(s, nb.x + xoff, nb.y + nb.height + fm.getMaxDescent() + fm.getMaxAscent() + 4); xoff += fm.stringWidth(s); } } if ((c instanceof StationModelCanvas) && ((StationModelCanvas) c).shouldShowAlignmentPoints()) { paintRectPoint(g, c); } }
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(); } }
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); }
@Override public Dimension getMinimumSize(int cols) { Font f = xtext.getFont(); FontMetrics fm = xtext.getFontMetrics(f); return new Dimension( fm.charWidth('0') * cols + 10, fm.getMaxDescent() + fm.getMaxAscent() + PADDING); }
public void paintText(Graphics2D g, ButtonInfo info) { ButtonModel model = info.button.getModel(); FontMetrics fm = info.button.getFontMetrics(info.button.getFont()); int mnemonicIndex = info.button.getDisplayedMnemonicIndex(); String text = info.button.getText(); int textShiftOffset = 0; g.setComposite(AlphaComposite.SrcOver); /* Draw the Text */ if (isEnabled(info.button)) { /** * paint the text normally */ g.setColor(info.button.getForeground()); BasicGraphicsUtils.drawStringUnderlineCharAt( g, text, mnemonicIndex, info.textRect.x + textShiftOffset, info.textRect.y + fm.getAscent() + textShiftOffset); } else { /** * paint the text disabled ** */ g.setColor(info.button.getBackground().brighter()); BasicGraphicsUtils.drawStringUnderlineCharAt( g, text, mnemonicIndex, info.textRect.x, info.textRect.y + fm.getAscent()); g.setColor(info.button.getBackground().darker()); BasicGraphicsUtils.drawStringUnderlineCharAt( g, text, mnemonicIndex, info.textRect.x - 1, info.textRect.y + fm.getAscent() - 1); } }
@Override public void setFont(Font font) { FontMetrics fm = getFontMetrics(font); lineHeight = fm.getHeight(); charWidth = fm.charWidth('0'); super.setFont(font); }
@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { textLabel.setFont(table.getFont()); textLabel.setText(Objects.toString(value, "")); textLabel.setBorder(hasFocus ? focusCellHighlightBorder : noFocusBorder); FontMetrics fm = table.getFontMetrics(table.getFont()); Insets i = textLabel.getInsets(); int swidth = iconLabel.getPreferredSize().width + fm.stringWidth(textLabel.getText()) + i.left + i.right; int cwidth = table.getColumnModel().getColumn(column).getWidth(); dim.width = swidth > cwidth ? cwidth : swidth; if (isSelected) { textLabel.setOpaque(true); textLabel.setForeground(table.getSelectionForeground()); textLabel.setBackground(table.getSelectionBackground()); iconLabel.setIcon(sicon); } else { textLabel.setOpaque(false); textLabel.setForeground(table.getForeground()); textLabel.setBackground(table.getBackground()); iconLabel.setIcon(nicon); } return panel; }
@Override protected void paintText( final Graphics g, final JComponent c, final Rectangle textRect, final String text) { final AbstractButton b = (AbstractButton) c; final ButtonModel model = b.getModel(); final FontMetrics fm = SwingUtils.getFontMetrics(c, g); final int mnemonicIndex = b.getDisplayedMnemonicIndex(); // Drawing text if (model.isEnabled()) { // Normal text g.setColor(b.getForeground()); SwingUtils.drawStringUnderlineCharAt( g, text, mnemonicIndex, textRect.x + getTextShiftOffset(), textRect.y + fm.getAscent() + getTextShiftOffset()); } else { // Disabled text g.setColor(b.getBackground().brighter()); SwingUtils.drawStringUnderlineCharAt( g, text, mnemonicIndex, textRect.x, textRect.y + fm.getAscent()); g.setColor(b.getBackground().darker()); SwingUtils.drawStringUnderlineCharAt( g, text, mnemonicIndex, textRect.x - 1, textRect.y + fm.getAscent() - 1); } }
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)); }
@Override public int getBaseline() { FontMetrics fm = b.getFontMetrics(b.getFont()); int border = (b.getPreferredSize().height - fm.getHeight()) / 2; int bestGuess = border + fm.getAscent(); if (PlatformUtils.isMac()) bestGuess -= 1; return bestGuess; }
/** * Write the given text string in the current font, left-aligned at (x, y). * * @param x the x-coordinate of the text * @param y the y-coordinate of the text * @param s the text */ public static void textLeft(double x, double y, String s) { offscreen.setFont(font); FontMetrics metrics = offscreen.getFontMetrics(); double xs = scaleX(x); double ys = scaleY(y); int hs = metrics.getDescent(); offscreen.drawString(s, (float) (xs), (float) (ys + hs)); draw(); }
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()); }
/** * Returns the baseline. * * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public int getBaseline(JComponent c, int width, int height) { super.getBaseline(c, width, height); if (progressBar.isStringPainted() && progressBar.getOrientation() == JProgressBar.HORIZONTAL) { FontMetrics metrics = progressBar.getFontMetrics(progressBar.getFont()); Insets insets = progressBar.getInsets(); int y = insets.top; height = height - insets.top - insets.bottom; return y + (height + metrics.getAscent() - metrics.getLeading() - metrics.getDescent()) / 2; } return -1; }
private void gameOverMessage(Graphics g) // Center the game-over message in the panel. { String msg = "Game Over. Your score: " + score; int x = (PWIDTH - metrics.stringWidth(msg)) / 2; int y = (PHEIGHT - metrics.getHeight()) / 2; g.setColor(Color.black); g.setFont(msgsFont); g.drawString(msg, x, y); } // end of gameOverMessage()
/** * Returns the baseline. * * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public int getBaseline(JComponent c, int width, int height) { super.getBaseline(c, width, height); AbstractButton b = (AbstractButton) c; String text = b.getText(); if (text == null || "".equals(text)) { return -1; } FontMetrics fm = b.getFontMetrics(b.getFont()); layout(b, fm, width, height); return BasicHTML.getBaseline(b, textRect.y, fm.getAscent(), textRect.width, textRect.height); }
/** * 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; }
/** * 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; }
private int computeHeight() { FontMetrics fm = myRootPane.getFontMetrics(getFont()); int fontHeight = fm.getHeight(); fontHeight += 7; int iconHeight = 0; if (getWindowDecorationStyle() == JRootPane.FRAME) { iconHeight = IMAGE_HEIGHT; } return Math.max(Math.max(fontHeight, iconHeight), JBUI.scale(myIdeMenu == null ? 28 : 36)); }
private void adjustCanvasStuff() { Font f = parent.frameFont; setFont(f); fm = getToolkit().getFontMetrics(f); lineSize = fm.getHeight(); rulerHt = 3 * lineSize; zLockStrW = fm.stringWidth("Zoom Lock"); elTimeStrW = fm.stringWidth("Elapsed Time"); fDescent = fm.getDescent(); dpi = getToolkit().getScreenResolution(); }
/** Draws a message centered in the window. */ private void drawMessage(Graphics2D g2, String message) { g2.setColor(Color.WHITE); g2.setFont(new Font("serif", Font.PLAIN, 72)); // Calculate the size of the text so we can center it in the window FontMetrics metrics = g2.getFontMetrics(); Rectangle2D textBounds = metrics.getStringBounds(message, g2); g2.drawString( message, windowWidth / 2 - (int) textBounds.getCenterX(), windowHeight / 2 - (int) textBounds.getCenterY()); }
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); }
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; }
/** * As of Java 2 platform v 1.4 this method should not be used or overriden. Use the paintText * method which takes the AbstractButton argument. */ protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) { AbstractButton b = (AbstractButton) c; ButtonModel model = b.getModel(); FontMetrics fm = SwingUtilities2.getFontMetrics(c, g); int mnemonicIndex = b.getDisplayedMnemonicIndex(); /* Draw the Text */ if (model.isEnabled()) { /** * paint the text normally */ g.setColor(b.getForeground()); SwingUtilities2.drawStringUnderlineCharAt( c, g, text, mnemonicIndex, textRect.x + getTextShiftOffset(), textRect.y + fm.getAscent() + getTextShiftOffset()); } else { /** * paint the text disabled ** */ g.setColor(b.getBackground().brighter()); SwingUtilities2.drawStringUnderlineCharAt( c, g, text, mnemonicIndex, textRect.x, textRect.y + fm.getAscent()); g.setColor(b.getBackground().darker()); SwingUtilities2.drawStringUnderlineCharAt( c, g, text, mnemonicIndex, textRect.x - 1, textRect.y + fm.getAscent() - 1); } }
public void drawDefeat(Graphics g) { g.setColor(new Color(0, 0, 0, 200)); g.fillRect(195, 220, 410, 110); g.setColor(new Color(255, 255, 255, 200)); g.fillRect(200, 225, 400, 100); g.setColor(new Color(0, 0, 0, 200)); g.setFont(new Font("Bell MT", Font.BOLD, 20)); FontMetrics metrics = g.getFontMetrics(new Font("Bell MT", Font.BOLD, 20)); int hgt = metrics.getHeight(); // Hgt = 26 int adv = metrics.stringWidth("You all fainted."); g.drawString("You all fainted.", getWidth() / 2 - adv / 2, 234 + hgt); adv = metrics.stringWidth("Defenseless, you are all eaten."); g.drawString("Defenseless, you are all eaten.", getWidth() / 2 - adv / 2, 269 + hgt); }
public Dimension getPreferredSize(JComponent c) { Dimension size; Insets border = progressBar.getInsets(); FontMetrics fontSizer = progressBar.getFontMetrics(progressBar.getFont()); if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { size = new Dimension(getPreferredInnerHorizontal()); // Ensure that the progress string will fit if (progressBar.isStringPainted()) { // I'm doing this for completeness. String progString = progressBar.getString(); int stringWidth = SwingUtilities2.stringWidth(progressBar, fontSizer, progString); if (stringWidth > size.width) { size.width = stringWidth; } // This uses both Height and Descent to be sure that // there is more than enough room in the progress bar // for everything. // This does have a strange dependency on // getStringPlacememnt() in a funny way. int stringHeight = fontSizer.getHeight() + fontSizer.getDescent(); if (stringHeight > size.height) { size.height = stringHeight; } } } else { size = new Dimension(getPreferredInnerVertical()); // Ensure that the progress string will fit. if (progressBar.isStringPainted()) { String progString = progressBar.getString(); int stringHeight = fontSizer.getHeight() + fontSizer.getDescent(); if (stringHeight > size.width) { size.width = stringHeight; } // This is also for completeness. int stringWidth = SwingUtilities2.stringWidth(progressBar, fontSizer, progString); if (stringWidth > size.height) { size.height = stringWidth; } } } size.width += border.left + border.right; size.height += border.top + border.bottom; return size; }
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); } }
public void paintComponent(Graphics g) { super.paintComponent(g); ((Graphics2D) g) .setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Dimension theSize = getSize(); if (mColorListMode == VisualizationColor.cColorListModeCategories) { int categories = mCategoryColorList.length; if (categories <= cMaxEditableColors) { int fieldWidth = (theSize.width - 2 * cBorder + cSpacing) / categories - cSpacing; mRect = new Rectangle[categories]; for (int i = 0; i < categories; i++) { mRect[i] = new Rectangle( cBorder + i * (fieldWidth + cSpacing), cBorder, fieldWidth, theSize.height - 2 * cBorder); drawColorButton(g, i); } } else { final String message = "<too many colors to edit>"; g.setColor(Color.GRAY); g.setFont(new Font("Arial", Font.BOLD, 13)); FontMetrics m = g.getFontMetrics(); g.drawString( message, (theSize.width - m.stringWidth(message)) / 2, (theSize.height + m.getHeight()) / 2 - m.getDescent()); } } else { int size = theSize.height - 2 * cBorder; int x1 = 2 * cBorder + size; int x2 = theSize.width - 2 * cBorder - size; mRect = new Rectangle[3]; mRect[0] = new Rectangle(cBorder, cBorder, size, size); mRect[1] = new Rectangle( theSize.width - size - cBorder, theSize.height - size - cBorder, size, size); mRect[cColorWedgeButton] = new Rectangle(x1, cBorder, x2 - x1, size); drawColorButton(g, 0); drawColorButton(g, 1); drawColorButton(g, cColorWedgeButton); } }