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:JapaneseCalendar.java

public void paintComponent(Graphics g) {
    int width = 400;
    int height = 400;

    Calendar cal = Calendar.getInstance(locale);
    cal.setTime(new Date());

    String header = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, locale);
    header += " " + cal.get(Calendar.YEAR);

    FontMetrics fm = g.getFontMetrics();
    Insets insets = getInsets();//from ww  w .  ja  va2  s  .  com
    g.setColor(Color.black);
    g.drawString(header, (width - fm.stringWidth(header)) / 2, insets.top + fm.getHeight());

    DateFormatSymbols dfs = new DateFormatSymbols(locale);
    String[] weekdayNames = dfs.getShortWeekdays();
    int fieldWidth = (width - insets.left - insets.right) / 7;
    g.drawString(weekdayNames[Calendar.SUNDAY],
            insets.left + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.SUNDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.MONDAY],
            insets.left + fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.MONDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.TUESDAY],
            insets.left + 2 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.TUESDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.WEDNESDAY],
            insets.left + 3 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.WEDNESDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.THURSDAY],
            insets.left + 4 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.THURSDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.FRIDAY],
            insets.left + 5 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.FRIDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.SATURDAY],
            insets.left + 6 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.SATURDAY])) / 2,
            insets.top + 3 * fm.getHeight());

    int dom = cal.get(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    int col = 0;
    switch (cal.get(Calendar.DAY_OF_WEEK)) {
    case Calendar.MONDAY:
        col = 1;
        break;

    case Calendar.TUESDAY:
        col = 2;
        break;

    case Calendar.WEDNESDAY:
        col = 3;
        break;

    case Calendar.THURSDAY:
        col = 4;
        break;

    case Calendar.FRIDAY:
        col = 5;
        break;

    case Calendar.SATURDAY:
        col = 6;
    }
    cal.set(Calendar.DAY_OF_MONTH, dom);

    int row = 5 * fm.getHeight();
    for (int i = 1; i <= cal.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
        g.drawString("" + i, insets.left + fieldWidth * col + (fieldWidth - fm.stringWidth("" + i)) / 2, row);
        if (++col > 6) {
            col = 0;
            row += fm.getHeight();
        }
    }
}

From source file:Main.java

public Component getListCellRendererComponent(final JList list, final Object value, final int index,
        final boolean isSelected, final boolean cellHasFocus) {
    return new JPanel() {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Font font = (Font) value;
            String text = font.getFamily();
            FontMetrics fm = g.getFontMetrics(font);
            g.setColor(isSelected ? list.getSelectionBackground() : list.getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(isSelected ? list.getSelectionForeground() : list.getForeground());
            g.setFont(font);//w w  w .ja  va  2  s.  co m
            g.drawString(text, 0, fm.getAscent());
        }

        public Dimension getPreferredSize() {
            Font font = (Font) value;
            String text = font.getFamily();
            Graphics g = getGraphics();
            FontMetrics fm = g.getFontMetrics(font);
            return new Dimension(fm.stringWidth(text), fm.getHeight());
        }
    };
}

From source file:Main.java

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

    if (page > 0) { /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
    }//  ww w  . j a v  a 2  s. c o  m

    /*
     * User (0,0) is typically outside the imageable area, so we must translate
     * by the X and Y values in the PageFormat to avoid clipping
     */
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    /* Now we perform our rendering */
    g.drawString("Test the print dialog!", 100, 100);

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

From source file:AlphaTest.java

/**
 * Draws a String into the display area for the plot.
 *//*from  w ww. j  a va  2 s  . co  m*/
protected void drawAreaString(Graphics g, int nLen, String szText, double x, double y) {
    if (nLen > 0 && szText.length() > nLen)
        szText = szText.substring(0, nLen);

    g.drawString(szText, (int) (m_nInsetX + x), (int) (m_nMaxHeight + m_nInsetY - y));
}

From source file:AlphaTest.java

/**
 * Draws a String into the graph area for the plot.
 *//*from w ww.j ava2  s .c om*/
protected void drawGraphString(Graphics g, int nLen, String szText, double x, double y) {
    if (nLen > 0 && szText.length() > nLen)
        szText = szText.substring(0, nLen);

    g.drawString(szText, (int) (m_nGraphInsetX + x), (int) (m_nGraphInsetY + m_nGraphMaxHeight - y));
}

From source file:HelloWorldPrinter.java

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

    if (page > 0) { /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
    }/*from  w w w.j  av  a 2  s .  co  m*/

    /*
     * User (0,0) is typically outside the imageable area, so we must translate
     * by the X and Y values in the PageFormat to avoid clipping
     */
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    /* Now we perform our rendering */
    g.drawString("Hello world!", 100, 100);

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

From source file:Main.java

public void paint(Graphics g) {
    Font f = g.getFont();/* ww w .  ja  va2 s  .c  om*/
    String fontName = f.getName();
    String fontFamily = f.getFamily();
    int fontSize = f.getSize();
    int fontStyle = f.getStyle();

    String msg = "Family: " + fontName;
    msg += ", Font: " + fontFamily;
    msg += ", Size: " + fontSize + ", Style: ";
    if ((fontStyle & Font.BOLD) == Font.BOLD)
        msg += "Bold ";
    if ((fontStyle & Font.ITALIC) == Font.ITALIC)
        msg += "Italic ";
    if ((fontStyle & Font.PLAIN) == Font.PLAIN)
        msg += "Plain ";

    g.drawString(msg, 4, 16);
}

From source file:com.lixiaocong.service.ImageCodeService.java

public BufferedImage getImage(HttpSession session) {
    //background/*from w  ww . ja va2 s .c om*/
    BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics graphics = bi.getGraphics();
    graphics.setColor(randomColor());
    graphics.fillRect(0, 0, WIDTH, HEIGHT);

    //content
    StringBuilder sb = new StringBuilder();
    int len = ch.length;
    Random random = new Random();
    for (int i = 0; i < 4; i++) {
        int index = random.nextInt(len);
        graphics.setColor(randomColor());
        graphics.setFont(new Font(null, Font.BOLD + Font.ITALIC, 30));
        graphics.drawString(ch[index] + "", i * 15 + 5, 25);
        sb.append(ch[index]);
    }
    session.setAttribute("imagecode", sb.toString());

    //lines
    for (int i = 0; i < 4; i++) {
        graphics.setColor(randomColor());
        graphics.drawLine(random.nextInt(WIDTH), random.nextInt(HEIGHT), random.nextInt(WIDTH),
                random.nextInt(HEIGHT));
    }

    return bi;
}

From source file:uk.co.modularaudio.mads.base.oscilloscope.ui.OscilloscopeDisplayUiJComponent.java

private void drawScale(final Graphics g, final int height, final float maxMag, final int xOffset) {
    final FontMetrics fm = g.getFontMetrics();
    // Do zero, and plus and minus maximum
    final int middle = height / 2;
    final int halfFontHeight = fm.getAscent() / 2;
    g.drawString("0.0", xOffset, middle + halfFontHeight);
    final int top = 0 + fm.getHeight();
    // Only re-create the string when we need to
    if (previousPositiveMagStr == null || previousPositiveMag != maxMag) {
        previousPositiveMagStr = MathFormatter.fastFloatPrint(maxMag, 2, true);
        previousPositiveMag = maxMag;// ww w  . j  a va2  s .c  o m
    }
    g.drawString(previousPositiveMagStr, xOffset, top);

    if (previousNegativeMagStr == null || previousNegativeMag != -maxMag) {
        previousNegativeMagStr = MathFormatter.fastFloatPrint(-maxMag, 2, true);
        previousNegativeMag = -maxMag;
    }
    final int bottom = height - halfFontHeight;
    g.drawString(previousNegativeMagStr, xOffset, bottom);
}

From source file:org.openlegacy.rpc.render.DefaultRpcImageRenderer.java

private void drawText(String source, Graphics graphics) {
    source = source.replaceAll("\r", "");
    String[] lines = source.split("\n");
    int rowNumber = 1;
    graphics.setColor(imageBackgroundColor);
    for (String line : lines) {
        int startY = toHeight(rowNumber);

        if (drawLineNumbers) {
            // draw row number
            graphics.setColor(imageSorroundingTextColor);
            graphics.drawString(String.valueOf(String.format("%2d", rowNumber)), 0, startY);
        }/*from  w w  w .  j a  va 2s  .c  o  m*/

        line = line.replaceAll("\t", "  ");
        graphics.drawString(line, 100, startY);
        rowNumber++;
    }
}