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

public void paint(Graphics g, JComponent c) {
    AbstractButton b = (AbstractButton) c;
    Dimension d = b.getSize();//w w w .j  av a  2s .  com

    g.setFont(c.getFont());
    FontMetrics fm = g.getFontMetrics();

    g.setColor(b.getForeground());
    String caption = b.getText();
    int x = (d.width - fm.stringWidth(caption)) / 2;
    int y = (d.height + fm.getAscent()) / 2;
    g.drawString(caption, x, y);

}

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

@Override
public void render(String source, OutputStream output) {

    BufferedImage buffer;//w  ww .j  a  v a  2  s. c o m

    int width = 885;
    int height = 550;

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

    if (logger.isDebugEnabled()) {
        logger.debug("Font set to:" + fontFamily);
    }

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

    drawText(source, graphics);

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

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

public void render(String source, OutputStream output) {

    BufferedImage buffer;//  w  w w  .ja v a  2 s. c o  m

    int width = 885;
    int height = 550;

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

    if (logger.isDebugEnabled()) {
        logger.debug("Font set to:" + fontFamily);
    }

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

    drawText(source, graphics);

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

From source file:PaintAllFontsFromGraphicEvironment.java

public void paint(Graphics g) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    Font[] allFonts = ge.getAllFonts();

    for (int i = 0; i < allFonts.length; i++) {
        Font f = allFonts[i].deriveFont(10.0f);
        g.setFont(f);

        g.setColor(Color.black);/* w w w  . j av a  2 s  . co m*/
        g.drawString("Hello!", 10, 20 * i);

    }
}

From source file:FontDemo.java

/**
 * Draws the font names in its font. Called by AWT when painting is needed
 * Does lazy evaluation of Font creation, caching the results (without this,
 * scrolling performance suffers even on a P3-750).
 *///from  w  ww  .j  av a2  s  .  com
public void paint(Graphics g) {
    for (int i = 0; i < fontNames.length; i += 1) {
        if (fonts[i] == null) {
            fonts[i] = new Font(fontNames[i], Font.BOLD, 14);
        }
        g.setFont(fonts[i]);
        int x = 20;
        int y = 20 + (YINCR * i);
        g.drawString(fontNames[i], x, y);
    }
}

From source file:FontList.java

public void paint(Graphics g) {
    for (int f = 0; f < families.length; f++) { // for each family
        for (int s = 0; s < styles.length; s++) { // for each style
            Font font = new Font(families[f], styles[s], 18); // create font
            g.setFont(font); // set font
            String name = families[f] + " " + stylenames[s]; // create name
            g.drawString(name, 20, (f * 4 + s + 1) * 20); // display name
        }/*from w ww .ja  va  2s .com*/
    }
}

From source file:org.opensha.commons.util.FileUtils.java

/**
 * Prints a Text file// w  w w . j  a  va 2 s .  com
 * @param pjob PrintJob  created using getToolkit().getPrintJob(JFrame,String,Properties);
 * @param pg Graphics
 * @param textToPrint String
 */
public static void print(PrintJob pjob, Graphics pg, String textToPrint) {

    int margin = 60;

    int pageNum = 1;
    int linesForThisPage = 0;
    int linesForThisJob = 0;
    // Note: String is immutable so won't change while printing.
    if (!(pg instanceof PrintGraphics)) {
        throw new IllegalArgumentException("Graphics context not PrintGraphics");
    }
    StringReader sr = new StringReader(textToPrint);
    LineNumberReader lnr = new LineNumberReader(sr);
    String nextLine;
    int pageHeight = pjob.getPageDimension().height - margin;
    Font helv = new Font("Monaco", Font.PLAIN, 12);
    //have to set the font to get any output
    pg.setFont(helv);
    FontMetrics fm = pg.getFontMetrics(helv);
    int fontHeight = fm.getHeight();
    int fontDescent = fm.getDescent();
    int curHeight = margin;
    try {
        do {
            nextLine = lnr.readLine();
            if (nextLine != null) {
                if ((curHeight + fontHeight) > pageHeight) {
                    // New Page
                    if (linesForThisPage == 0)
                        break;

                    pageNum++;
                    linesForThisPage = 0;
                    pg.dispose();
                    pg = pjob.getGraphics();
                    if (pg != null) {
                        pg.setFont(helv);
                    }
                    curHeight = 0;
                }
                curHeight += fontHeight;
                if (pg != null) {
                    pg.drawString(nextLine, margin, curHeight - fontDescent);
                    linesForThisPage++;

                    linesForThisJob++;
                }
            }
        } while (nextLine != null);
    } catch (EOFException eof) {
        // Fine, ignore
    } catch (Throwable t) { // Anything else
        t.printStackTrace();
    }
}

From source file:org.accada.reader.hal.impl.sim.graphic.Antenna.java

/**
 * paints the rfid antenna/*from w ww . ja v  a  2s.c o m*/
 * 
 * @param the graphic representation of the component
 */
protected void paintComponent(Graphics g) {
    icon.paintIcon(this, g, 0, 0);
    g.setColor(Color.BLACK);
    g.setFont(new Font(simulator.getProperties().getProperty("AntennaLabelFont"), 0,
            simulator.getProperty("AntennaLabelSize")));
    g.drawString(id, (15 - id.length()) * 7 / 2, simulator.getProperty("AntennaHeight"));
}

From source file:org.accada.hal.impl.sim.graphic.Antenna.java

/**
 * paints the rfid antenna/*ww w  . j  a v a2s . co m*/
 * 
 * @param g the graphic representation of the component
 */
protected void paintComponent(Graphics g) {
    icon.paintIcon(this, g, 0, 0);
    g.setColor(Color.BLACK);
    g.setFont(new Font(simulator.getProperties().getString("AntennaLabelFont"), 0,
            simulator.getProperty("AntennaLabelSize")));
    g.drawString(id, (15 - id.length()) * 7 / 2, simulator.getProperty("AntennaHeight"));
}

From source file:org.accada.hal.impl.sim.graphic.Reader.java

/**
 * paints the reader//from www . jav a 2  s.  com
 * 
 * @param g the graphic representation of the component
 */
protected void paintComponent(Graphics g) {
    icon.paintIcon(this, g, 0, 0);
    g.setColor(Color.BLACK);
    g.setFont(new Font(simulator.getProperties().getString("ReaderLabelFont"), 0,
            simulator.getProperty("ReaderLabelSize")));
    g.drawString(id, (18 - id.length()) * 9 / 2, simulator.getProperty("ReaderHeight"));
}