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:com.synnex.saas.platform.core.servlet.CaptchaServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {/*from   w w  w .  j  a va2s .  c  om*/
        int width = 50;
        int height = 18;
        String captchaCode = RandomStringUtils.random(4, true, true);
        HttpSession session = request.getSession(true);
        session.setAttribute("captchaCode", captchaCode);

        response.setContentType("images/jpeg");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        ServletOutputStream out = response.getOutputStream();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        Font mFont = new Font("Times New Roman", Font.BOLD, 18);
        g.setFont(mFont);
        g.setColor(getRandColor(160, 200));
        Random random = new Random();
        for (int i = 0; i < 155; i++) {
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            int x3 = random.nextInt(12);
            int y3 = random.nextInt(12);
            g.drawLine(x2, y2, x2 + x3, y2 + y3);
        }
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        g.drawString(captchaCode, 2, 16);
        g.dispose();
        ImageIO.write((BufferedImage) image, "JPEG", out);
        out.close();
    } catch (Exception e) {
        logger.error("Generate captcha failed.", e);
    }
}

From source file:PictureScaler.java

/**
 * Render all scaled versions 10 times, timing each version and 
 * reporting the results below the appropriate scaled image.
 *//*from  w w w  . j  a v a 2 s .  c  om*/
protected void paintComponent(Graphics g) {
    // Scale with NEAREST_NEIGHBOR
    int xLoc = PADDING, yLoc = PADDING;
    long startTime, endTime;
    float totalTime;
    int iterations = 10;
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        g.drawImage(picture, xLoc, yLoc, scaleW, scaleH, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("NEAREST ", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("NEAREST: " + ((endTime - startTime) / 1000000));

    // Scale with BILINEAR
    xLoc += scaleW + PADDING;
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        g.drawImage(picture, xLoc, yLoc, scaleW, scaleH, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("BILINEAR", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("BILINEAR: " + ((endTime - startTime) / 1000000));

    // Scale with BICUBIC
    xLoc += scaleW + PADDING;
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        g.drawImage(picture, xLoc, yLoc, scaleW, scaleH, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("BICUBIC", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("BICUBIC: " + ((endTime - startTime) / 1000000));

    // Scale with getScaledInstance
    xLoc += scaleW + PADDING;
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        Image scaledPicture = picture.getScaledInstance(scaleW, scaleH, Image.SCALE_AREA_AVERAGING);
        g.drawImage(scaledPicture, xLoc, yLoc, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("getScaled", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("getScaled: " + ((endTime - startTime) / 1000000));

    // Scale with Progressive Bilinear
    xLoc += scaleW + PADDING;
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        Image scaledPicture = getFasterScaledInstance(picture, scaleW, scaleH,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
        g.drawImage(scaledPicture, xLoc, yLoc, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("Progressive", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("Progressive: " + ((endTime - startTime) / 1000000));
}

From source file:com.fengduo.bee.commons.servlet.ValidateCodeServlet.java

private String createCharacter(Graphics g) {
    char[] codeSeq = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U',
            'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };
    String[] fontTypes = { "Arial", "Arial Black", "AvantGarde Bk BT", "Calibri" };
    Random random = new Random();
    StringBuilder s = new StringBuilder();
    for (int i = 0; i < 4; i++) {
        String r = String.valueOf(codeSeq[random.nextInt(codeSeq.length)]);// random.nextInt(10));
        g.setColor(new Color(50 + random.nextInt(100), 50 + random.nextInt(100), 50 + random.nextInt(100)));
        g.setFont(new Font(fontTypes[random.nextInt(fontTypes.length)], Font.BOLD, 26));
        g.drawString(r, 15 * i + 5, 19 + random.nextInt(8));
        // g.drawString(r, i*w/4, h-5);
        s.append(r);/*from   w  w  w  .  j  a  v  a2  s  .  c om*/
    }
    return s.toString();
}

From source file:com.headswilllol.basiclauncher.Launcher.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setFont(font);//from w w  w .  ja va 2  s .c o  m
    if (updateMsg != null) {
        g.drawString("Update Available!", centerText(g, "Update Available!"), 50);
        g.setFont(smallFont);
        g.drawString(updateMsg, centerText(g, updateMsg), 100);
    }

    else if (progress == null)
        g.drawString(NAME + " Launcher", centerText(g, NAME + " Launcher"), 50);

    else {
        g.drawString(progress, centerText(g, progress), height / 2);
        if (fail != null)
            g.drawString(fail, centerText(g, fail), height / 2 + 50);
        else {
            if (aSize != -1 && eSize != -1) {
                String s = (aSize * 8) + "/" + (int) (eSize * 8) + " B";
                if (eSize * 8 >= 1024)
                    if (eSize * 8 >= 1024 * 1024)
                        s = String.format("%.2f", aSize * 8 / 1024 / 1024) + "/"
                                + String.format("%.2f", eSize * 8 / 1024 / 1024) + " MiB";
                    else
                        s = String.format("%.2f", aSize * 8 / 1024) + "/"
                                + String.format("%.2f", eSize * 8 / 1024) + " KiB";
                g.drawString(s, centerText(g, s), height / 2 + 40);
                String sp = "@" + (int) speed + " B/s";
                if (speed >= 1024)
                    if (speed >= 1024 * 1024)
                        sp = "@" + String.format("%.2f", (speed / 1024 / 1024)) + " MiB/s";
                    else
                        sp = "@" + String.format("%.2f", (speed / 1024)) + " KiB/s";
                g.drawString(sp, centerText(g, sp), height / 2 + 80);
                int barWidth = 500;
                int barHeight = 35;
                g.setColor(Color.LIGHT_GRAY);
                g.drawRect(width / 2 - barWidth / 2, height / 2 + 100, barWidth, barHeight);
                g.setColor(Color.GREEN);
                g.fillRect(width / 2 - barWidth / 2 + 1, height / 2 + 100 + 1,
                        (int) ((aSize / eSize) * (double) barWidth - 2), barHeight - 1);
                g.setColor(new Color(.2f, .2f, .2f));
                int percent = (int) (aSize / (double) eSize * 100);
                g.drawString(percent + "%", centerText(g, percent + "%"), height / 2 + 128);
            }
        }
    }
}

From source file:DialogSeparator.java

public void paint(Graphics g) {
    g.setColor(getBackground());/*from www .  j ava 2  s. co  m*/
    g.fillRect(0, 0, getWidth(), getHeight());

    Dimension d = getSize();
    int y = (d.height - 3) / 2;
    g.setColor(Color.white);
    g.drawLine(1, y, d.width - 1, y);
    y++;
    g.drawLine(0, y, 1, y);
    g.setColor(Color.gray);
    g.drawLine(d.width - 1, y, d.width, y);
    y++;
    g.drawLine(1, y, d.width - 1, y);

    String text = getText();
    if (text.length() == 0)
        return;

    g.setFont(getFont());
    FontMetrics fm = g.getFontMetrics();
    y = (d.height + fm.getAscent()) / 2;
    int fontWidth = fm.stringWidth(text);

    g.setColor(getBackground());
    g.fillRect(OFFSET - 5, 0, OFFSET + fontWidth, d.height);

    g.setColor(getForeground());
    g.drawString(text, OFFSET, y);
}

From source file:VASSAL.counters.Labeler.java

public static void drawLabel(Graphics g, String text, int x, int y, Font f, int hAlign, int vAlign,
        Color fgColor, Color bgColor, Color borderColor) {
    g.setFont(f);/*from w  w  w.j ava2  s .c o m*/
    final int width = g.getFontMetrics().stringWidth(text + "  ");
    final int height = g.getFontMetrics().getHeight();
    int x0 = x;
    int y0 = y;
    switch (hAlign) {
    case CENTER:
        x0 = x - width / 2;
        break;
    case LEFT:
        x0 = x - width;
        break;
    }
    switch (vAlign) {
    case CENTER:
        y0 = y - height / 2;
        break;
    case BOTTOM:
        y0 = y - height;
        break;
    }
    if (bgColor != null) {
        g.setColor(bgColor);
        g.fillRect(x0, y0, width, height);
    }
    if (borderColor != null) {
        g.setColor(borderColor);
        g.drawRect(x0, y0, width, height);
    }
    g.setColor(fgColor);
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.drawString(" " + text + " ", x0, y0 + g.getFontMetrics().getHeight() - g.getFontMetrics().getDescent());
}

From source file:CalIcon.java

/** paintIcon: draw the calendar page. */
public void paintIcon(Component c, Graphics g, int x, int y) {

    // Allow clock to get painted (voodoo magic)
    if (showTime)
        super.paint(g);

    // Outline it.
    g.setColor(Color.black);/*from w w w .j a  v  a  2s .c  o m*/
    g.draw3DRect(x, y, d.width - 2, d.height - 2, true);

    // Show the date: First, a white page with a drop shadow.
    g.setColor(Color.gray);
    g.fillRect(x + RBX + 3, y + RBY + 3, RBW, RBH);
    g.setColor(Color.white);
    g.fillRect(x + RBX, y + RBY, RBW, RBH);

    // g.setColor(getForeground());
    g.setColor(Color.black);

    String s = days[myCal.get(Calendar.DAY_OF_WEEK) - 1];
    g.setFont(dayNameFont);
    int w = dayNameFM.stringWidth(s);
    g.drawString(s, x + RBX + ((RBW - w) / 2), y + RBY + 10);

    s = Integer.toString(myCal.get(Calendar.DAY_OF_MONTH));
    g.setFont(dayNumbFont);
    w = dayNumbFM.stringWidth(s);
    g.drawString(s, x + RBX + ((RBW - w) / 2), y + RBY + 25);

    s = mons[myCal.get(Calendar.MONTH)];
    g.setFont(monNameFont);
    w = monNameFM.stringWidth(s);
    g.drawString(s, x + RBX + ((RBW - w) / 2), y + RBY + 35);
}

From source file:com.tinypace.mobistore.servlet.ValidateCodeServlet.java

private String createCharacter(Graphics g) {
    char[] codeSeq = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
            'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };
    String[] fontTypes = { "\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53",
            "\u96b6\u4e66" };
    Random random = new Random();
    StringBuilder s = new StringBuilder();
    for (int i = 0; i < 4; i++) {
        String r = String.valueOf(codeSeq[random.nextInt(codeSeq.length)]);//random.nextInt(10));
        g.setColor(new Color(50 + random.nextInt(100), 50 + random.nextInt(100), 50 + random.nextInt(100)));
        g.setFont(new Font(fontTypes[random.nextInt(fontTypes.length)], Font.BOLD, 26));
        g.drawString(r, 15 * i + 5, 19 + random.nextInt(8));
        s.append(r);/*from w w  w.  j  a va 2 s  .c  om*/
    }
    return s.toString();
}

From source file:apm.common.servlet.ValidateCodeServlet.java

private String createCharacter(Graphics g) {
    char[] codeSeq = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
            'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };
    String[] fontTypes = { "\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53",
            "\u96b6\u4e66" };
    Random random = new Random();
    StringBuilder s = new StringBuilder();
    for (int i = 0; i < 4; i++) {
        String r = String.valueOf(codeSeq[random.nextInt(codeSeq.length)]);//random.nextInt(10));
        g.setColor(new Color(50 + random.nextInt(100), 50 + random.nextInt(100), 50 + random.nextInt(100)));
        g.setFont(new Font(fontTypes[random.nextInt(fontTypes.length)], Font.BOLD, 26));
        g.drawString(r, 15 * i + 5, 19 + random.nextInt(8));
        //         g.drawString(r, i*w/4, h-5);
        s.append(r);//from   ww  w  .  ja  v  a  2  s.c o  m
    }
    return s.toString();
}

From source file:com.aistor.common.servlet.ValidateCodeServlet.java

private String createCharacter(Graphics g) {
    char[] codeSeq = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S',
            'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    String[] fontTypes = { "\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53",
            "\u96b6\u4e66" };
    Random random = new Random();
    StringBuilder s = new StringBuilder();
    for (int i = 0; i < 4; i++) {
        String r = String.valueOf(codeSeq[random.nextInt(codeSeq.length)]);//random.nextInt(10));
        g.setColor(new Color(50 + random.nextInt(100), 50 + random.nextInt(100), 50 + random.nextInt(100)));
        g.setFont(new Font(fontTypes[random.nextInt(fontTypes.length)], Font.BOLD, 26));
        g.drawString(r, 15 * i + 5, 19 + random.nextInt(8));
        //         g.drawString(r, i*w/4, h-5);
        s.append(r);/*w w w  . ja va2 s  .c om*/
    }
    return s.toString();
}