Example usage for java.awt Graphics drawString

List of usage examples for java.awt Graphics drawString

Introduction

In this page you can find the example usage for java.awt Graphics drawString.

Prototype

public abstract void drawString(AttributedCharacterIterator iterator, int x, int y);

Source Link

Document

Renders the text of the specified iterator applying its attributes in accordance with the specification of the java.awt.font.TextAttribute TextAttribute class.

Usage

From source file:org.underworldlabs.swing.plaf.base.AcceleratorToolTipUI.java

public void paint(Graphics g, JComponent c) {

    UIUtils.antialias(g);/*from w ww.j av a  2s .c o  m*/

    Font font = c.getFont();
    FontMetrics metrics = c.getFontMetrics(font);

    Dimension size = c.getSize();
    if (c.isOpaque()) {
        g.setColor(c.getBackground());
        g.fillRect(0, 0, size.width + 20, size.height);
    }

    JToolTip toolTip = (JToolTip) c;
    String tipText = getTipText(toolTip);

    if (!MiscUtils.isNull(tipText)) {

        Insets insets = c.getInsets();
        Rectangle paintTextR = new Rectangle(insets.left, insets.top, size.width - (insets.left + insets.right),
                size.height - (insets.top + insets.bottom));

        Color foreground = c.getForeground();
        g.setColor(foreground);
        g.setFont(font);

        g.drawString(tipText, paintTextR.x + 3, paintTextR.y + metrics.getAscent());

        String acceleratorString = getAcceleratorStringForRender(toolTip);
        if (StringUtils.isNotBlank(acceleratorString)) {

            Font acceleratorFont = font.deriveFont(font.getSize() - 1f);
            g.setFont(acceleratorFont);
            g.setColor(GUIUtils.getSlightlyBrighter(foreground, 2.0f));

            g.drawString(acceleratorString, paintTextR.x + 6 + metrics.stringWidth(tipText),
                    paintTextR.y + metrics.getAscent());
        }

    }

}

From source file:org.esa.nest.dat.views.polarview.PolarCanvas.java

private void draw(Graphics g, Dimension size) {
    final int annotationHeight = 100;
    final int x = Math.max((int) (size.height * 0.05), 10);
    final int y = Math.max((int) (size.width * 0.05), 10);
    final int bottom = Math.max((int) (size.height * 0.1) + annotationHeight, 20);
    final int right = Math.max((int) (size.width * 0.1), 20);
    final Rectangle r = positionPlot(size, x, y, bottom, right);
    plotRadius = Math.min(r.width / 2, r.height / 2);
    final Dimension quadrantSize = new Dimension(plotRadius, plotRadius);
    g.translate(0, origin.y + r.height);
    if (data != null) {
        loadColorBar(data.getColorScale());
        drawColorBar(g, colourAxis);/*from   www. j ava  2  s.  c  o  m*/
    }
    g.translate(0, -origin.y - r.height);

    origin.y += r.height / 2;
    origin.x += r.width / 2;
    final Graphics graphics = g.create();
    graphics.translate(origin.x, origin.y);
    radialAxis.setSize(quadrantSize);
    if (data != null)
        data.draw(graphics);
    if (rings != null) {
        int ri = 0;
        for (double ring : rings) {
            final int rad = radialAxis.getScreenPoint(ring);
            final int rad2 = rad + rad;
            graphics.setColor(Color.lightGray);
            graphics.drawOval(-rad, -rad, rad2, rad2);
            if (ringText != null && ringText[ri] != null) {
                graphics.setColor(Color.black);
                graphics.drawString(ringText[ri], 0, -rad);
            }
            ++ri;
        }
    } else {
        radialAxis.draw(graphics);
    }

    // draw wind direction & speed
    if (showWindDirection)
        drawWindDirection(graphics, plotRadius, windDirection - 90);

    graphics.translate(-origin.x, -origin.y);

    drawAxisLabels(graphics);

    graphics.dispose();
}

From source file:Main.java

public void paint(Graphics g) {

    int fontSize = 20;

    g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));

    FontMetrics fm = g.getFontMetrics();

    String s = "www.java2s.com";

    int stringWidth = fm.stringWidth(s);

    int w = 200;/*from  ww w.j  a v a 2 s  .co  m*/
    int h = 200;

    int x = (w - stringWidth) / 2;
    int baseline = fm.getMaxAscent() + (h - (fm.getAscent() + fm.getMaxDecent())) / 2;
    int ascent = fm.getMaxAscent();
    int descent = fm.getMaxDecent();
    int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();

    g.setColor(Color.white);
    g.fillRect(x, baseline - ascent, stringWidth, fontHeight);

    g.setColor(Color.gray);
    g.drawLine(x, baseline, x + stringWidth, baseline);

    g.setColor(Color.red);
    g.drawLine(x, baseline + descent, x + stringWidth, baseline + descent);

    g.setColor(Color.blue);
    g.drawLine(x, baseline - ascent, x + stringWidth, baseline - ascent);
    g.setColor(Color.black);
    g.drawString(s, x, baseline);

}

From source file:MainClass.java

public void paint(Graphics g) {
    int fontSize = 20;

    g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));

    FontMetrics fm = g.getFontMetrics();

    String s = "www.java2s.com";

    int stringWidth = fm.stringWidth(s);

    int w = 200;//www . ja  v  a 2  s  . co  m
    int h = 200;

    int x = (w - stringWidth) / 2;
    int baseline = fm.getMaxAscent() + (h - (fm.getAscent() + fm.getMaxDecent())) / 2;
    int ascent = fm.getMaxAscent();
    int descent = fm.getMaxDecent();
    int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();

    g.setColor(Color.white);
    g.fillRect(x, baseline - ascent, stringWidth, fontHeight);

    g.setColor(Color.gray);
    g.drawLine(x, baseline, x + stringWidth, baseline);

    g.setColor(Color.red);
    g.drawLine(x, baseline + descent, x + stringWidth, baseline + descent);

    g.setColor(Color.blue);
    g.drawLine(x, baseline - ascent, x + stringWidth, baseline - ascent);
    g.setColor(Color.black);
    g.drawString(s, x, baseline);
}

From source file:edu.ku.brc.specify.plugins.imgproc.ImageProcessorPanel.java

@Override
public void paint(Graphics g) {
    ((Graphics2D) g).addRenderingHints(hints);

    super.paint(g);

    if (font == null) {
        font = (new JLabel()).getFont();
        font = font.deriveFont(18.0f);//from w w  w . j ava2  s  .c  om
        font = font.deriveFont(Font.BOLD);
    }
    String title = "Specify Image Processor";
    g.setColor(new Color(32, 131, 155));
    g.setFont(font);
    int w = g.getFontMetrics().stringWidth(title);
    int y = g.getFontMetrics().getHeight() - 5;
    int x = (getSize().width - w) / 2;

    g.drawString(title, x, y);
}

From source file:edu.ku.brc.stats.StatGroupTable.java

/**
 * Overrides paint to draw in name at top with separator AND the Label
 *//* www  .j  a  v  a 2s .co  m*/
public void paint(Graphics g) {
    super.paint(g);

    if (!useSeparator) {
        Dimension dim = getSize();

        FontMetrics fm = g.getFontMetrics();
        int strW = fm.stringWidth(name);

        int x = (dim.width - strW) / 2;
        Insets ins = getBorder().getBorderInsets(this);
        int y = 2 + fm.getAscent();

        int lineW = dim.width - ins.left - ins.right;
        g.setColor(Color.BLUE.darker());
        g.drawString(name, x, y);
        x = ins.left;
        y += fm.getDescent() + fm.getLeading();

        g.setColor(Color.LIGHT_GRAY.brighter());
        g.drawLine(x, y, x + lineW, y);
        y++;
        x++;
        g.setColor(Color.LIGHT_GRAY);
        g.drawLine(x, y, x + lineW, y);
    }
}

From source file:net.rptools.tokentool.ui.TokenCompositionPanel.java

@Override
protected void paintComponent(Graphics g) {

    Dimension size = getSize();/*from w  w w  . jav  a2s  . co  m*/
    g.setColor(Color.black);
    g.fillRect(0, 0, size.width, size.height);

    int messageY = 15;
    int messageX = 5;

    // BASE
    if (AppState.compositionProperties.isBase()) {
        paintOverlay(g, size);
    }

    // TOKEN
    if (tokenImage != null) {
        int width = (int) (tokenImage.getWidth() * tokenScale);
        int height = (int) (tokenImage.getHeight() * tokenScale);
        g.drawImage(tokenImage, tokenOffsetX, tokenOffsetY, width, height, this);
    } else {
        g.setColor(Color.white);
        g.drawString("Drag an image onto this pane", messageX, messageY);
        messageY += 15;
    }

    if (!AppState.compositionProperties.isBase()) {
        paintOverlay(g, size);
    }
}

From source file:Main.java

public void paint(Graphics g) {

    int fontSize = 20;

    Font font = new Font("TimesRoman", Font.PLAIN, fontSize);
    g.setFont(font);/* www. ja  v  a2s.  c o m*/

    FontMetrics fm = g.getFontMetrics(font);

    String s = "www.java2s.com";

    int stringWidth = fm.stringWidth(s);

    int w = 200;
    int h = 200;

    int x = (w - stringWidth) / 2;
    int baseline = fm.getMaxAscent() + (h - (fm.getAscent() + fm.getMaxDecent())) / 2;
    int ascent = fm.getMaxAscent();
    int descent = fm.getMaxDecent();
    int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();

    g.setColor(Color.white);
    g.fillRect(x, baseline - ascent, stringWidth, fontHeight);

    g.setColor(Color.gray);
    g.drawLine(x, baseline, x + stringWidth, baseline);

    g.setColor(Color.red);
    g.drawLine(x, baseline + descent, x + stringWidth, baseline + descent);

    g.setColor(Color.blue);
    g.drawLine(x, baseline - ascent, x + stringWidth, baseline - ascent);
    g.setColor(Color.black);
    g.drawString(s, x, baseline);

}

From source file:AppletMenuBarDemo.java

/**
 * This method is called to draw tell the component to redraw itself. If we
 * were implementing a Swing component, we'd override paintComponent()
 * instead/*  w ww. ja  va  2  s . co  m*/
 */
public void paint(Graphics g) {
    if (remeasure)
        measure(); // Remeasure everything first, if needed

    // Figure out Y coordinate to draw at
    Dimension size = getSize();
    int baseline = size.height - margins.bottom - descent;
    // Set the font to draw with
    g.setFont(getFont());
    // Loop through the labels
    int nummenus = labels.size();
    for (int i = 0; i < nummenus; i++) {
        // Set the drawing color. Highlight the current item
        if ((i == highlightedItem) && (highlightColor != null))
            g.setColor(getHighlightColor());
        else
            g.setColor(getForeground());

        // Draw the menu label at the position computed in measure()
        g.drawString((String) labels.elementAt(i), startPositions[i], baseline);
    }

    // Now draw a groove at the bottom of the menubar.
    Color bg = getBackground();
    g.setColor(bg.darker());
    g.drawLine(0, size.height - 2, size.width, size.height - 2);
    g.setColor(bg.brighter());
    g.drawLine(0, size.height - 1, size.width, size.height - 1);
}

From source file:ChartPanel.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (values == null || values.length == 0)
        return;//from  w w w . j av  a2  s .  c  o  m
    double minValue = 0;
    double maxValue = 0;
    for (int i = 0; i < values.length; i++) {
        if (minValue > values[i])
            minValue = values[i];
        if (maxValue < values[i])
            maxValue = values[i];
    }

    Dimension d = getSize();
    int clientWidth = d.width;
    int clientHeight = d.height;
    int barWidth = clientWidth / values.length;

    Font titleFont = new Font("SansSerif", Font.BOLD, 20);
    FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
    Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
    FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);

    int titleWidth = titleFontMetrics.stringWidth(title);
    int y = titleFontMetrics.getAscent();
    int x = (clientWidth - titleWidth) / 2;
    g.setFont(titleFont);
    g.drawString(title, x, y);

    int top = titleFontMetrics.getHeight();
    int bottom = labelFontMetrics.getHeight();
    if (maxValue == minValue)
        return;
    double scale = (clientHeight - top - bottom) / (maxValue - minValue);
    y = clientHeight - labelFontMetrics.getDescent();
    g.setFont(labelFont);

    for (int i = 0; i < values.length; i++) {
        int valueX = i * barWidth + 1;
        int valueY = top;
        int height = (int) (values[i] * scale);
        if (values[i] >= 0)
            valueY += (int) ((maxValue - values[i]) * scale);
        else {
            valueY += (int) (maxValue * scale);
            height = -height;
        }

        g.setColor(Color.red);
        g.fillRect(valueX, valueY, barWidth - 2, height);
        g.setColor(Color.black);
        g.drawRect(valueX, valueY, barWidth - 2, height);
        int labelWidth = labelFontMetrics.stringWidth(names[i]);
        x = i * barWidth + (barWidth - labelWidth) / 2;
        g.drawString(names[i], x, y);
    }
}