Example usage for java.awt Graphics getFontMetrics

List of usage examples for java.awt Graphics getFontMetrics

Introduction

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

Prototype

public FontMetrics getFontMetrics() 

Source Link

Document

Gets the font metrics of the current font.

Usage

From source file:haven.Utils.java

public static int drawtext(Graphics g, String text, Coord c) {
    java.awt.FontMetrics m = g.getFontMetrics();
    g.drawString(text, c.x, c.y + m.getAscent());
    return (m.getHeight());
}

From source file:ExtendedTableCellRenderer.java

public void paint(Graphics g) {
    super.paint(g);

    if (underlined) {
        Insets i = getInsets();//from ww  w  .  j a va 2 s .co  m
        FontMetrics fm = g.getFontMetrics();

        Rectangle textRect = new Rectangle();
        Rectangle viewRect = new Rectangle(i.left, i.top, getWidth() - (i.right + i.left),
                getHeight() - (i.bottom + i.top));

        SwingUtilities.layoutCompoundLabel(this, fm, getText(), getIcon(), getVerticalAlignment(),
                getHorizontalAlignment(), getVerticalTextPosition(), getHorizontalTextPosition(), viewRect,
                new Rectangle(), textRect,
                getText() == null ? 0 : ((Integer) UIManager.get("Button.textIconGap")).intValue());

        int offset = 2;
        if (UIManager.getLookAndFeel().isNativeLookAndFeel()
                && System.getProperty("os.name").startsWith("Windows")) {
            offset = 1;
        }
        g.fillRect(textRect.x + ((Integer) UIManager.get("Button.textShiftOffset")).intValue(), textRect.y
                + fm.getAscent() + ((Integer) UIManager.get("Button.textShiftOffset")).intValue() + offset,
                textRect.width, 1);
    }
}

From source file:haven.Utils.java

static void aligntext(Graphics g, String text, Coord c, double ax, double ay) {
    java.awt.FontMetrics m = g.getFontMetrics();
    java.awt.geom.Rectangle2D ts = m.getStringBounds(text, g);
    g.drawString(text, (int) (c.x - ts.getWidth() * ax), (int) (c.y + m.getAscent() - ts.getHeight() * ay));
}

From source file:JapaneseCalendar.java

public void paintComponent(Graphics g) {
    int width = 400;
    int height = 400;

    Calendar cal = Calendar.getInstance(locale);
    cal.setTime(new Date());

    String header = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, locale);
    header += " " + cal.get(Calendar.YEAR);

    FontMetrics fm = g.getFontMetrics();
    Insets insets = getInsets();/*from w ww .j  a  v a 2 s . c om*/
    g.setColor(Color.black);
    g.drawString(header, (width - fm.stringWidth(header)) / 2, insets.top + fm.getHeight());

    DateFormatSymbols dfs = new DateFormatSymbols(locale);
    String[] weekdayNames = dfs.getShortWeekdays();
    int fieldWidth = (width - insets.left - insets.right) / 7;
    g.drawString(weekdayNames[Calendar.SUNDAY],
            insets.left + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.SUNDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.MONDAY],
            insets.left + fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.MONDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.TUESDAY],
            insets.left + 2 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.TUESDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.WEDNESDAY],
            insets.left + 3 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.WEDNESDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.THURSDAY],
            insets.left + 4 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.THURSDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.FRIDAY],
            insets.left + 5 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.FRIDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.SATURDAY],
            insets.left + 6 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.SATURDAY])) / 2,
            insets.top + 3 * fm.getHeight());

    int dom = cal.get(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    int col = 0;
    switch (cal.get(Calendar.DAY_OF_WEEK)) {
    case Calendar.MONDAY:
        col = 1;
        break;

    case Calendar.TUESDAY:
        col = 2;
        break;

    case Calendar.WEDNESDAY:
        col = 3;
        break;

    case Calendar.THURSDAY:
        col = 4;
        break;

    case Calendar.FRIDAY:
        col = 5;
        break;

    case Calendar.SATURDAY:
        col = 6;
    }
    cal.set(Calendar.DAY_OF_MONTH, dom);

    int row = 5 * fm.getHeight();
    for (int i = 1; i <= cal.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
        g.drawString("" + i, insets.left + fieldWidth * col + (fieldWidth - fm.stringWidth("" + i)) / 2, row);
        if (++col > 6) {
            col = 0;
            row += fm.getHeight();
        }
    }
}

From source file:DialogSeparator.java

public void paint(Graphics g) {
    g.setColor(getBackground());//from w w  w . j  a  va2s .  c om
    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:Clock.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Color colorRetainer = g.getColor();

    g.setColor(getBackground());/*from w  w  w .  j  a v a 2s  .com*/
    g.fillRect(0, 0, getWidth(), getHeight());
    getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());

    calendar.setTime(new Date()); // get current time
    int hrs = calendar.get(Calendar.HOUR_OF_DAY);
    int min = calendar.get(Calendar.MINUTE);

    g.setColor(getForeground());
    if (isDigital) {
        String time = "" + hrs + ":" + min;
        g.setFont(getFont());
        FontMetrics fm = g.getFontMetrics();
        int y = (getHeight() + fm.getAscent()) / 2;
        int x = (getWidth() - fm.stringWidth(time)) / 2;
        g.drawString(time, x, y);
    } else {
        int x = getWidth() / 2;
        int y = getHeight() / 2;
        int rh = getHeight() / 4;
        int rm = getHeight() / 3;

        double ah = ((double) hrs + min / 60.0) / 6.0 * Math.PI;
        double am = min / 30.0 * Math.PI;

        g.drawLine(x, y, (int) (x + rh * Math.sin(ah)), (int) (y - rh * Math.cos(ah)));
        g.drawLine(x, y, (int) (x + rm * Math.sin(am)), (int) (y - rm * Math.cos(am)));
    }

    g.setColor(colorRetainer);
}

From source file:edu.ku.brc.stats.StatGroupTable.java

/**
 * Overrides paint to draw in name at top with separator AND the Label
 *//* w w  w  .  ja  v  a2 s.  c  o m*/
public void paint(Graphics g) {
    super.paint(g);

    if (!useSeparator) {
        Dimension dim = getSize();

        FontMetrics fm = g.getFontMetrics();
        int strW = fm.stringWidth(name);

        int x = (dim.width - strW) / 2;
        Insets ins = getBorder().getBorderInsets(this);
        int y = 2 + fm.getAscent();

        int lineW = dim.width - ins.left - ins.right;
        g.setColor(Color.BLUE.darker());
        g.drawString(name, x, y);
        x = ins.left;
        y += fm.getDescent() + fm.getLeading();

        g.setColor(Color.LIGHT_GRAY.brighter());
        g.drawLine(x, y, x + lineW, y);
        y++;
        x++;
        g.setColor(Color.LIGHT_GRAY);
        g.drawLine(x, y, x + lineW, y);
    }
}

From source file:edu.ku.brc.af.core.NavBox.java

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Dimension dim = getSize();/*from w  w w  .  j  a v a 2  s  .  c  om*/

    FontMetrics fm = g.getFontMetrics();
    int strW = fm.stringWidth(name);

    int x = (dim.width - strW) / 2;
    Insets ins = getBorder().getBorderInsets(this);
    int y = 2 + fm.getAscent();

    int lineW = dim.width - ins.left - ins.right;
    x = ins.left;
    int txtY = y;
    y += 3;

    g.setColor(Color.LIGHT_GRAY);
    g.drawLine(x, y, x + lineW, y);
    y++;
    minHeight = y;

    x++;
    g.setColor(Color.LIGHT_GRAY.darker());
    g.drawLine(x, y, x + lineW, y);

    ((Graphics2D) g).setRenderingHints(UIHelper.createTextRenderingHints());
    g.setColor(Color.BLUE.darker());
    g.drawString(name, x, txtY);

    if (collapsableIconOpen != null) {
        if (iconRect == null) {
            iconRect = getBounds();
            iconRect.x = iconRect.width - collapsableIconOpen.getIconWidth();
            iconRect.y = 0;
        }
        g.drawImage(icon.getImage(), iconRect.x, iconRect.y, null);
    }
}

From source file:org.fcrepo.localservices.imagemanip.ImageManipulation.java

/**
 * Method automatically called by browser to handle image manipulations.
 * //ww w. ja v a  2 s  .  co m
 * @param req
 *        Browser request to servlet res Response sent back to browser after
 *        image manipulation
 * @throws IOException
 *         If an input or output exception occurred ServletException If a
 *         servlet exception occurred
 */
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    System.setProperty("java.awt.headless", "true");
    // collect all possible parameters for servlet
    String url = req.getParameter("url");
    String op = req.getParameter("op");
    String newWidth = req.getParameter("newWidth");
    String brightAmt = req.getParameter("brightAmt");
    String zoomAmt = req.getParameter("zoomAmt");
    String wmText = req.getParameter("wmText");
    String cropX = req.getParameter("cropX");
    String cropY = req.getParameter("cropY");
    String cropWidth = req.getParameter("cropWidth");
    String cropHeight = req.getParameter("cropHeight");
    String convertTo = req.getParameter("convertTo");
    if (convertTo != null) {
        convertTo = convertTo.toLowerCase();
    }
    try {
        if (op == null) {
            throw new ServletException("op parameter not specified.");
        }
        String outputMimeType;
        // get the image via url and put it into the ImagePlus processor.
        BufferedImage img = getImage(url);
        // do watermarking stuff
        if (op.equals("watermark")) {
            if (wmText == null) {
                throw new ServletException("Must specify wmText.");
            }
            Graphics g = img.getGraphics();
            int fontSize = img.getWidth() * 3 / 100;
            if (fontSize < 10) {
                fontSize = 10;
            }
            g.setFont(new Font("Lucida Sans", Font.BOLD, fontSize));
            FontMetrics fm = g.getFontMetrics();
            int stringWidth = (int) fm.getStringBounds(wmText, g).getWidth();
            int x = img.getWidth() / 2 - stringWidth / 2;
            int y = img.getHeight() - fm.getHeight();
            g.setColor(new Color(180, 180, 180));
            g.fill3DRect(x - 10, y - fm.getHeight() - 4, stringWidth + 20, fm.getHeight() + 12, true);
            g.setColor(new Color(100, 100, 100));
            g.drawString(wmText, x + 2, y + 2);
            g.setColor(new Color(240, 240, 240));
            g.drawString(wmText, x, y);
        }
        ImageProcessor ip = new ImagePlus("temp", img).getProcessor();
        // if the inputMimeType is image/gif, need to convert to RGB in any case
        if (inputMimeType.equals("image/gif")) {
            ip = ip.convertToRGB();
            alreadyConvertedToRGB = true;
        }
        // causes scale() and resize() to do bilinear interpolation
        ip.setInterpolate(true);
        if (!op.equals("convert")) {
            if (op.equals("resize")) {
                ip = resize(ip, newWidth);
            } else if (op.equals("zoom")) {
                ip = zoom(ip, zoomAmt);
            } else if (op.equals("brightness")) {
                ip = brightness(ip, brightAmt);
            } else if (op.equals("watermark")) {
                // this is now taken care of beforehand (see above)
            } else if (op.equals("grayscale")) {
                ip = grayscale(ip);
            } else if (op.equals("crop")) {
                ip = crop(ip, cropX, cropY, cropWidth, cropHeight);
            } else {
                throw new ServletException("Invalid operation: " + op);
            }
            outputMimeType = inputMimeType;
        } else {
            if (convertTo == null) {
                throw new ServletException("Neither op nor convertTo was specified.");
            }
            if (convertTo.equals("jpg") || convertTo.equals("jpeg")) {
                outputMimeType = "image/jpeg";
            } else if (convertTo.equals("gif")) {
                outputMimeType = "image/gif";
            } else if (convertTo.equals("tiff")) {
                outputMimeType = "image/tiff";
            } else if (convertTo.equals("bmp")) {
                outputMimeType = "image/bmp";
            } else if (convertTo.equals("png")) {
                outputMimeType = "image/png";
            } else {
                throw new ServletException("Invalid format: " + convertTo);
            }
        }
        res.setContentType(outputMimeType);
        BufferedOutputStream out = new BufferedOutputStream(res.getOutputStream());
        outputImage(ip, out, outputMimeType);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
        res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:org.deegree.portal.standard.wms.control.DynLegendListener.java

/**
 * In case the legend can not be obtained from the OGCLayer, this method is called to create a dummy legend image
 * plus the legend title//from w  w w. j a  va 2 s .  co m
 * 
 * @param layerName
 * @return BufferedImage holding the created dummy legend
 */
private BufferedImage createMissingLegend(String layerName) {
    LOG.logDebug("URL is null. Drawing the image from a missingImage variable in init params");
    BufferedImage missingLegend = new BufferedImage(80, 15, BufferedImage.TYPE_INT_RGB);
    Graphics g = missingLegend.getGraphics();
    Rectangle2D rect = g.getFontMetrics().getStringBounds(layerName, g);
    g.dispose();
    missingLegend = new BufferedImage(rect.getBounds().width + 80, missingImg.getHeight() + 15,
            BufferedImage.TYPE_INT_ARGB);
    g = missingLegend.getGraphics();
    g.drawImage(missingImg, 0, 0, null);
    g.setColor(Color.RED);
    if (useLayerTitle) {
        g.drawString(layerName, missingImg.getWidth() + 5,
                missingImg.getHeight() / 2 + g.getFont().getSize() / 2);
    }
    g.dispose();

    return missingLegend;
}