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:net.sf.webphotos.tools.Thumbnail.java

private static Image estampar(Image im) {
    try {//  w  ww. java 2s .co  m
        Image temp = new ImageIcon(im).getImage();

        BufferedImage buf = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
                BufferedImage.TYPE_INT_RGB);

        Graphics2D g2 = (Graphics2D) buf.getGraphics();

        g2.drawImage(temp, 0, 0, null);
        g2.setBackground(Color.BLUE);

        Dimension dimensaoFoto = new Dimension(im.getWidth(null), im.getHeight(null));

        // aplicar texto
        if (texto != null) {
            Util.out.println("aplicando texto " + texto);

            Font fonte = new Font(txFamilia, txEstilo, txTamanho);
            g2.setFont(fonte);
            FontMetrics fm = g2.getFontMetrics(fonte);
            Dimension dimensaoTexto = new Dimension(fm.stringWidth(texto), fm.getHeight());
            Point posTexto = calculaPosicao(dimensaoFoto, dimensaoTexto, txMargem, txPosicao);

            g2.setPaint(txCorFundo);
            g2.drawString(texto, (int) posTexto.getX() + 1, (int) posTexto.getY() + 1 + fm.getHeight());
            g2.setPaint(txCorFrente);
            g2.drawString(texto, (int) posTexto.getX(), (int) posTexto.getY() + fm.getHeight());
        }

        // aplicar marca dagua
        if (marcadagua != null) {
            Image marca = new ImageIcon(marcadagua).getImage();
            int rule = AlphaComposite.SRC_OVER;
            float alpha = (float) mdTransparencia / 100;
            Point pos = calculaPosicao(dimensaoFoto, new Dimension(marca.getWidth(null), marca.getHeight(null)),
                    mdMargem, mdPosicao);

            g2.setComposite(AlphaComposite.getInstance(rule, alpha));
            g2.drawImage(marca, (int) pos.getX(), (int) pos.getY(), null);
        }

        g2.dispose();
        //return (Image) buf;
        return Toolkit.getDefaultToolkit().createImage(buf.getSource());
    } catch (Exception e) {
        Util.err.println("[Thumbnail.estampar]/ERRO: Inesperado - " + e.getMessage());
        e.printStackTrace(Util.err);
        return im;
    }
}

From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java

private static void drawMaskName(Graphics2D g2, AbstractMask mask, Rectangle2D imageArea, double fontSizeRate) {
    if (mask.getName() == null) {
        return;//from  www  .ja  v a  2 s .co m
    }
    Point2D fontLocation = mask.getTitleLocation(imageArea);
    g2.setPaint(Color.black);
    Font currentFont = g2.getFont();
    g2.setFont(currentFont.deriveFont((float) (maskNameFont.getSize() * fontSizeRate)).deriveFont(Font.ITALIC));
    g2.drawString(mask.getName(), (int) fontLocation.getX(), (int) fontLocation.getY());
    g2.setFont(currentFont);
}

From source file:Main.java

public int print(Graphics g, PageFormat Pf, int pageIndex) throws PrinterException {
    if (pageIndex > 0)
        return NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(sFont);
    g2.setPaint(Color.black);//from   w ww.  j a  va  2s . com
    g2.drawString("Save a tree!", 96, 144);
    return PAGE_EXISTS;
}

From source file:MainClass.java

public int print(Graphics g, PageFormat pf, int pageIndex) {
    if (pageIndex != 0)
        return NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(new Font("Serif", Font.PLAIN, 36));
    g2.setPaint(Color.black);/*  w ww  .j a  va 2  s  .c om*/
    g2.drawString("www.java2s.com", 100, 100);
    Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf.getImageableWidth(),
            pf.getImageableHeight());
    g2.draw(outline);
    return PAGE_EXISTS;
}

From source file:edu.umn.cs.spatialHadoop.nasa.MultiHDFPlot.java

/**
 * Draws a scale used with the heat map/*  w ww.j  a v a  2 s .  c  om*/
 * @param output
 * @param valueRange
 * @param width
 * @param height
 * @throws IOException
 */
private static void drawVerticalScale(Path output, double min, double max, int width, int height,
        OperationsParams params) throws IOException {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = image.createGraphics();
    g.setBackground(Color.BLACK);
    g.clearRect(0, 0, width, height);

    // fix this part to work according to color1, color2 and gradient type
    HDFPlot.HDFRasterizer gradient = new HDFPlot.HDFRasterizer();
    gradient.configure(params);
    HDFRasterLayer gradientLayer = (HDFRasterLayer) gradient.createCanvas(0, 0, new Rectangle());
    for (int y = 0; y < height; y++) {
        Color color = gradientLayer.calculateColor(height - y, 0, height);
        g.setColor(color);
        g.drawRect(width * 3 / 4, y, width / 4, 1);
    }

    int fontSize = 24;
    g.setFont(new Font("Arial", Font.BOLD, fontSize));
    double step = (max - min) * fontSize * 5 / height;
    step = (int) (Math.pow(10.0, Math.round(Math.log10(step))));
    double min_value = Math.floor(min / step) * step;
    double max_value = Math.floor(max / step) * step;

    g.setColor(Color.WHITE);
    for (double value = min_value; value <= max_value; value += step) {
        double y = ((value - min) + (max - value) * (height - fontSize)) / (max - min);
        g.drawString(String.valueOf((int) value), 5, (int) y);
    }

    g.dispose();

    FileSystem fs = output.getFileSystem(new Configuration());
    FSDataOutputStream outStream = fs.create(output, true);
    ImageIO.write(image, "png", outStream);
    outStream.close();
}

From source file:edu.umn.cs.spatialHadoop.nasa.MultiHDFPlot.java

/**
 * Draws a scale used with the heat map//from  w w  w . j  a va2  s .c o m
 * @param output
 * @param valueRange
 * @param width
 * @param height
 * @throws IOException
 */
private static void drawHorizontalScale(Path output, double min, double max, int width, int height,
        OperationsParams params) throws IOException {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = image.createGraphics();
    g.setBackground(Color.BLACK);
    g.clearRect(0, 0, width, height);

    int fontSize = 24;
    // fix this part to work according to color1, color2 and gradient type
    HDFPlot.HDFRasterizer gradient = new HDFPlot.HDFRasterizer();
    gradient.configure(params);
    HDFRasterLayer gradientLayer = (HDFRasterLayer) gradient.createCanvas(0, 0, new Rectangle());
    for (int x = 0; x < width; x++) {
        Color color = gradientLayer.calculateColor(x, 0, width);
        g.setColor(color);
        g.drawRect(x, height - (fontSize - 5), 1, fontSize - 5);
    }

    g.setFont(new Font("Arial", Font.BOLD, fontSize));
    double step = (max - min) * fontSize * 5 / width;
    step = (int) (Math.pow(10.0, Math.round(Math.log10(step))));
    double min_value = Math.floor(min / step) * step;
    double max_value = Math.floor(max / step) * step;

    g.setColor(Color.WHITE);
    for (double value = min_value; value <= max_value; value += step) {
        double x = ((value - min) * (width - fontSize) + (max - value)) / (max - min);
        g.drawString(String.valueOf((int) value), (int) x, fontSize);
    }

    g.dispose();

    FileSystem fs = output.getFileSystem(new Configuration());
    FSDataOutputStream outStream = fs.create(output, true);
    ImageIO.write(image, "png", outStream);
    outStream.close();
}

From source file:DrawStringI18N.java

public void paint(Graphics g) {
    Graphics2D graphics2D = (Graphics2D) g;
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    graphics2D.setFont(new Font("LucidaSans", Font.PLAIN, 40));
    graphics2D.drawString("\u05E9\u05DC\u05D5\u05DD \u05E2\u05D5\u05DC\u05DD", 50, 75);
}

From source file:Main.java

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

    Font f = new Font("Arial", Font.PLAIN, 48);
    g2.setFont(f);

    g2.drawString("www.java2s.com", 10, 60);

    RenderingHints rh = g2.getRenderingHints();
    rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHints(rh);/*w  ww.jav a 2s  .c  om*/

    g2.drawString("Antialiased text", 10, 120);
}

From source file:MainClass.java

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

    Font f = new Font("Arial", Font.PLAIN, 48);
    g2.setFont(f);

    g2.drawString("www.java2s.com", 10, 60);

    RenderingHints rh = g2.getRenderingHints();
    rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHints(rh);//from   w w w .ja v  a  2s  . c  om

    g2.drawString("Antialiased text", 10, 120);

}

From source file:StringGraidentPaint.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    int w = getSize().width;
    int h = getSize().height;

    g2D.setFont(font);
    GradientPaint gp = new GradientPaint(30.0f, 50.0f, Color.blue, fontMetrics.stringWidth("Hello!"),
            fontMetrics.getHeight(), Color.red);
    g2D.setPaint(gp);/* www.j a v a 2 s .c  o m*/
    g2D.drawString("Hello!", 20, 200);

}