Example usage for java.awt Graphics setFont

List of usage examples for java.awt Graphics setFont

Introduction

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

Prototype

public abstract void setFont(Font font);

Source Link

Document

Sets this graphics context's font to the specified font.

Usage

From source file:ro.nextreports.designer.ui.sqleditor.syntax.SyntaxStyles.java

/**
 * Set the graphics font and others to the style for the given token
 * @param g/*  w  w  w .ja v a2s. c o  m*/
 * @param type
 */
@Deprecated
public void setGraphicsStyle(Graphics g, TokenType type) {
    SyntaxStyle ss = styles.get(type);
    if (ss != null) {
        g.setFont(g.getFont().deriveFont(ss.getFontStyle()));
        g.setColor(ss.getColor());
    } else {
        g.setFont(g.getFont().deriveFont(Font.PLAIN));
        g.setColor(Color.BLACK);
    }
}

From source file:Main.java

public void paint(Graphics g) {
    m_fm = g.getFontMetrics();//from  w  w w  .j av  a2 s .  co  m

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

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

    StringTokenizer st = new StringTokenizer(getText(), "\t");
    while (st.hasMoreTokens()) {
        String str = st.nextToken();
        g.drawString(str, x, y);
        //insert distance for each tab
        x += m_fm.stringWidth(str) + 50;

        if (!st.hasMoreTokens())
            break;
    }
}

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  . ja v  a  2 s  . 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:org.openlegacy.terminal.render.DefaultTerminalSnapshotImageRenderer.java

@Override
public void render(TerminalSnapshot terminalSnapshot, OutputStream output) {

    BufferedImage buffer;/*from   w ww  . j  ava 2s . com*/

    int width = 885;
    int height = 450;
    if (terminalSnapshot.getSize().getColumns() == 132) {
        width = 1460;
    }
    imageWidth = width;
    imageHeight = height;

    buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    if (logger.isTraceEnabled()) {
        logger.trace("Font set to:" + fontFamily);
    }

    Font font = new Font(fontFamily, fontType, fontSize);
    Graphics graphics = buffer.createGraphics();
    graphics.setFont(font);
    setDefaultColor(graphics);

    markBackgroundAndInputFields(terminalSnapshot, graphics);

    drawText(terminalSnapshot, graphics);

    try {
        ImageIO.write(buffer, "jpg", output);
    } catch (IOException e) {
        throw (new OpenLegacyRuntimeException(e));
    }
}

From source file:yp.tibco.com.yang.lottery.server.LotteryServerIoHandler.java

/**
 * Create an image using the specified request and the text.  
 *
 * @param request/*from   www.j  a va  2 s . co m*/
 *  Determines the height and width of the image
 * @param text
 *  The text that is placed in the image
 * @return
 *  a BufferedImage representing the text.
 */
private BufferedImage createImage(ImageRequest request, String text) {
    BufferedImage image = new BufferedImage(request.getWidth(), request.getHeight(),
            BufferedImage.TYPE_BYTE_INDEXED);
    Graphics graphics = image.createGraphics();
    graphics.setColor(Color.YELLOW);
    graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
    Font serif = new Font("serif", Font.PLAIN, 30);
    graphics.setFont(serif);
    graphics.setColor(Color.BLUE);
    graphics.drawString(text, 10, 50);
    return image;
}

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

public BufferedImage getImage(HttpSession session) {
    //background//from w  ww .  j av a 2  s . c o  m
    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:components.Rule.java

protected void paintComponent(Graphics g) {
    Rectangle drawHere = g.getClipBounds();

    // Fill clipping area with dirty brown/orange.
    g.setColor(new Color(230, 163, 4));
    g.fillRect(drawHere.x, drawHere.y, drawHere.width, drawHere.height);

    // Do the ruler labels in a small font that's black.
    g.setFont(new Font("SansSerif", Font.PLAIN, 10));
    g.setColor(Color.black);/*from  w  w  w.  j a v  a 2 s  .co m*/

    // Some vars we need.
    int end = 0;
    int start = 0;
    int tickLength = 0;
    String text = null;

    // Use clipping bounds to calculate first and last tick locations.
    if (orientation == HORIZONTAL) {
        start = (drawHere.x / increment) * increment;
        end = (((drawHere.x + drawHere.width) / increment) + 1) * increment;
    } else {
        start = (drawHere.y / increment) * increment;
        end = (((drawHere.y + drawHere.height) / increment) + 1) * increment;
    }

    // Make a special case of 0 to display the number
    // within the rule and draw a units label.
    if (start == 0) {
        text = Integer.toString(0) + (isMetric ? " cm" : " in");
        tickLength = 10;
        if (orientation == HORIZONTAL) {
            g.drawLine(0, SIZE - 1, 0, SIZE - tickLength - 1);
            g.drawString(text, 2, 21);
        } else {
            g.drawLine(SIZE - 1, 0, SIZE - tickLength - 1, 0);
            g.drawString(text, 9, 10);
        }
        text = null;
        start = increment;
    }

    // ticks and labels
    for (int i = start; i < end; i += increment) {
        if (i % units == 0) {
            tickLength = 10;
            text = Integer.toString(i / units);
        } else {
            tickLength = 7;
            text = null;
        }

        if (tickLength != 0) {
            if (orientation == HORIZONTAL) {
                g.drawLine(i, SIZE - 1, i, SIZE - tickLength - 1);
                if (text != null)
                    g.drawString(text, i - 3, 21);
            } else {
                g.drawLine(SIZE - 1, i, SIZE - tickLength - 1, i);
                if (text != null)
                    g.drawString(text, 9, i + 3);
            }
        }
    }
}

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);
            g.drawString(text, 0, fm.getAscent());
        }/*www  .  j a  va2 s  .co  m*/

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

public void paint(Graphics g) {

    /*//w  w  w.j a v  a 2s .c o m
     * Draw the image stretched to exactly cover the size of the drawing area.
     */
    Dimension size = getSize();
    g.drawImage(img, 0, 0, size.width, size.height, 0, 0, img.getWidth(null), img.getHeight(null), null);

    /*
     * Fill a rounded rectangle centered in the drawing area. Calculate the size
     * of the rectangle from the size of the text
     */
    g.setFont(font);
    FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
    Rectangle2D bounds = font.getStringBounds(text, frc);

    int wText = (int) bounds.getWidth();
    int hText = (int) bounds.getHeight();

    int rX = (size.width - wText) / 2;
    int rY = (size.height - hText) / 2;
    g.setColor(Color.yellow);
    g.fillRoundRect(rX, rY, wText, hText, hText / 2, hText / 2);

    /*
     * Draw text positioned in the rectangle. Since the rectangle is sized based
     * on the bounds of the String we can position it using those bounds.
     */
    int xText = rX - (int) bounds.getX();
    int yText = rY - (int) bounds.getY();
    g.setColor(Color.black);
    g.setFont(font);
    g.drawString(text, xText, yText);
}

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

@Override
public void paint(Graphics g) {
    try {/*from  ww  w  .j av a  2 s . co m*/
        if (isShowing()) {
            if (opaque)
                fillBackground(g);
            g.setColor(getForeground());
            g.setFont(getFont());
            paintComponents(this, g);
        }
        drawSynchronised(g, getSize());
    } catch (Throwable e) {
        Debug.trace(e);
    }
}