Example usage for java.awt Graphics2D setFont

List of usage examples for java.awt Graphics2D setFont

Introduction

In this page you can find the example usage for java.awt Graphics2D 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:TransparentText.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    // the rendering quality.
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // a red rectangle.
    Rectangle2D r = new Rectangle2D.Double(50, 50, 550, 100);
    g2.setPaint(Color.red);/*from   www  . ja  va  2s  . co  m*/
    g2.fill(r);
    // a composite with transparency.
    Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f);
    g2.setComposite(c);
    // Draw some blue text.
    g2.setPaint(Color.blue);
    g2.setFont(new Font("Times New Roman", Font.PLAIN, 72));
    g2.drawString("Java Source and Support", 25, 130);
}

From source file:TrackerPanel.java

public void paintComponent(Graphics g)
// Draw the depth image with coloured users, skeletons, and statistics info
{
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    drawUserDepths(g2d);//from   w w w .j  a  va 2 s.c  om
    g2d.drawImage(cameraImage, 641, 0, this);

    g2d.setFont(msgFont); // for user status and stats
    skels.draw(g2d);
    writeStats(g2d);
}

From source file:com.us.servlet.AuthCode.java

protected void service(HttpServletRequest request, HttpServletResponse response) {
    final CodeAuth bean = AppHelper.CODE_AUTH;
    int width = NumberUtils.toInt(request.getParameter("width"), bean.getWidth());
    int height = NumberUtils.toInt(request.getParameter("height"), bean.getHeight());
    int x = width / (bean.getLength() + 1);
    int codeY = height - 4;
    int fontHeight = height - 2;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D graphics = image.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    if (StringUtil.hasText(request.getParameter("bgcolor"))) {
        graphics.setBackground(ColorHelper.hex2RGB(request.getParameter("bgcolor")));
    }//ww w.  j  a v a  2  s . c om
    graphics.fillRect(0, 0, width, height);
    graphics.setFont(new Font(bean.getFont(), Font.BOLD, fontHeight));
    graphics.drawRect(0, 0, width - 1, height - 1);
    // 
    if (bean.isBreakLine()) {
        for (int i = 0; i < 15; i++) {
            int x1 = RandomUtils.nextInt(width);
            int y1 = RandomUtils.nextInt(height);
            int x2 = RandomUtils.nextInt(12);
            int y2 = RandomUtils.nextInt(12);
            graphics.drawLine(x1, y1, x + x2, y1 + y2);
        }
    }
    char[] CHARSET_AREA = null;
    if (bean.getType().charAt(0) == '1') {
        CHARSET_AREA = ArrayUtils.addAll(CHARSET_AREA, BIG_LETTERS);
    }
    if (bean.getType().charAt(1) == '1') {
        CHARSET_AREA = ArrayUtils.addAll(CHARSET_AREA, SMALL_LETTER);
    }
    if (bean.getType().charAt(2) == '1') {
        CHARSET_AREA = ArrayUtils.addAll(CHARSET_AREA, NUMBERS);
    }
    StringBuilder randomCode = new StringBuilder();
    for (int i = 0; i < bean.getLength(); i++) {
        String rand = String.valueOf(CHARSET_AREA[RandomUtils.nextInt(CHARSET_AREA.length)]);
        graphics.setColor(ColorHelper.color(RandomUtils.nextInt(255), RandomUtils.nextInt(255),
                RandomUtils.nextInt(255)));
        graphics.drawString(rand, (i + 1) * x, codeY);
        randomCode.append(rand);
    }
    HttpSession session = request.getSession();
    session.setAttribute(bean.getSessionKey(), randomCode.toString());
    // ?
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/png");
    try {
        // Servlet?
        ServletOutputStream sos = response.getOutputStream();
        ImageIO.write(image, "png", sos);
        sos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.squidy.designer.util.DrawableString.java

/**
 * @param g/*from ww w .  j av a 2 s.  c  o m*/
 */
public void draw(Graphics2D g, double viewScale) {
    if (value == null)
        return;
    if (color != null)
        g.setColor(color);
    if (font != null)
        g.setFont(font);
    update(g, viewScale);
    if (drawValue != null)
        g.drawString(drawValue, positionX + offsetX, positionY + offsetY);
}

From source file:org.squidy.designer.shape.VisualShape.java

@Override
protected final void paint(PPaintContext paintContext) {
    super.paint(paintContext);

    Graphics2D g = paintContext.getGraphics();

    // Set default font.
    g.setFont(internalFont);

    //      if (!renderingHintsSet) {
    if (isRenderPrimitive()) {
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
        g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
        g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    } else {/*from   w  ww . j  av  a  2s . co m*/
        // Use anti aliasing -> May slow down performance.
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DEFAULT);
        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    }
    //         renderingHintsSet = true;
    //      }

    // Paint the shapes visual representation.
    paintShape(paintContext);

    // Allows visual debugging if enabled.
    if (DebugConstants.ENABLED) {
        paintDebug(paintContext);
    }
}

From source file:org.squidy.designer.zoom.impl.InformationShape.java

@Override
protected void paintShapeZoomedIn(PPaintContext paintContext) {
    super.paintShapeZoomedIn(paintContext);

    if (informationSource != null) {
        Graphics2D g = paintContext.getGraphics();

        PBounds bounds = getBoundsReference();
        double x = bounds.getX();
        double width = bounds.getWidth();

        String source = "Source: " + informationSource;

        g.setFont(fontName);
        g.drawString(name, (int) (x + 50), 230);

        g.setFont(fontSource);/*from   www. j av  a  2 s .  c o  m*/
        source = FontUtils.createCroppedLabelIfNecessary(g.getFontMetrics(), source, (int) width);
        g.drawString(source, (int) (x + width - FontUtils.getWidthOfText(g.getFontMetrics(), source)) - 20,
                130);
    }
}

From source file:savant.view.tracks.TrackRenderer.java

/**
 * Draw a legend which consists of the given bases arranged horizontally
 *//* w w w . ja  v a 2 s. co  m*/
protected void drawBaseLegend(Graphics2D g2, int x, int y, ColourKey... keys) {
    ColourScheme cs = (ColourScheme) instructions.get(DrawingInstruction.COLOUR_SCHEME);
    g2.setFont(LEGEND_FONT);

    for (ColourKey k : keys) {
        g2.setColor(cs.getColor(k));
        g2.fillRect(x, y - SWATCH_SIZE.height + 2, SWATCH_SIZE.width, SWATCH_SIZE.height);
        g2.setColor(Color.BLACK);
        g2.drawString(k.getName(), x + SWATCH_SIZE.width + 3, y);
        x += 27;
    }
}

From source file:AntiAlias.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;
    BufferedImage image = // Create an off-screen image
            new BufferedImage(65, 35, BufferedImage.TYPE_INT_RGB);
    Graphics2D ig = image.createGraphics(); // Get its Graphics for drawing

    // Set the background to a gradient fill. The varying color of
    // the background helps to demonstrate the anti-aliasing effect
    ig.setPaint(new GradientPaint(0, 0, Color.black, 65, 35, Color.white));
    ig.fillRect(0, 0, 65, 35);/*  w  ww  .ja  v  a 2 s  . co  m*/

    // Set drawing attributes for the foreground.
    // Most importantly, turn on anti-aliasing.
    ig.setStroke(new BasicStroke(2.0f)); // 2-pixel lines
    ig.setFont(new Font("Serif", Font.BOLD, 18)); // 18-point font
    ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
            RenderingHints.VALUE_ANTIALIAS_ON);

    // Now draw pure blue text and a pure red oval
    ig.setColor(Color.blue);
    ig.drawString("Java", 9, 22);
    ig.setColor(Color.red);
    ig.drawOval(1, 1, 62, 32);

    // Finally, scale the image by a factor of 10 and display it
    // in the window. This will allow us to see the anti-aliased pixels
    g.drawImage(image, AffineTransform.getScaleInstance(10, 10), this);

    // Draw the image one more time at its original size, for comparison
    g.drawImage(image, 0, 0, this);
}

From source file:org.colombbus.tangara.AboutWindow.java

private BufferedImage createBackgroundImage(Image baseBackgroundImg) {
    BufferedImage newImg = new BufferedImage(baseBackgroundImg.getWidth(null),
            baseBackgroundImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
    newImg.getGraphics().drawImage(baseBackgroundImg, 0, 0, null);
    Graphics2D drawingGraphics = (Graphics2D) newImg.getGraphics();
    Color titleColor = Configuration.instance().getColor("tangara.title.color");
    String titleText = Configuration.instance().getString("tangara.title");
    Font titleFont = Configuration.instance().getFont("tangara.title.font");
    drawingGraphics.setFont(titleFont);
    drawingGraphics.setColor(titleColor);
    drawingGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    drawingGraphics.drawString(titleText, marginLeft, windowHeight / 2 - marginText);
    return newImg;
}

From source file:savant.view.tracks.TrackRenderer.java

/**
 * Simplest kind of legend is just a list of coloured lines with names next to them.
 *//*from  w w w.j  a va  2  s . c  o  m*/
protected void drawSimpleLegend(Graphics2D g2, int x, int y, ColourKey... keys) {
    ColourScheme cs = (ColourScheme) instructions.get(DrawingInstruction.COLOUR_SCHEME);

    g2.setFont(LEGEND_FONT);

    for (ColourKey k : keys) {
        String legendString = k.getName();
        g2.setColor(cs.getColor(k));
        g2.setStroke(TWO_STROKE);
        Rectangle2D stringRect = LEGEND_FONT.getStringBounds(legendString, g2.getFontRenderContext());
        g2.drawLine(x - 25, y - (int) stringRect.getHeight() / 2, x - 5, y - (int) stringRect.getHeight() / 2);
        g2.setColor(cs.getColor(ColourKey.INTERVAL_LINE));
        g2.setStroke(ONE_STROKE);
        g2.drawString(legendString, x, y);

        y += LEGEND_LINE_HEIGHT;
    }
}