List of usage examples for java.awt FontMetrics getStringBounds
public Rectangle2D getStringBounds(String str, Graphics context)
From source file:PaintUtils.java
/** * Returns the bounds that the text of a label will be drawn into. * Takes into account the current font metrics. *///from w ww . j av a 2s. c om public static Rectangle getTextBounds(Graphics g, JLabel label) { FontMetrics fm = g.getFontMetrics(); Rectangle2D r2d = fm.getStringBounds(label.getText(), g); Rectangle rect = r2d.getBounds(); int xOffset = 0; switch (label.getHorizontalAlignment()) { case SwingConstants.RIGHT: case SwingConstants.TRAILING: xOffset = label.getBounds().width - rect.width; break; case SwingConstants.CENTER: xOffset = (label.getBounds().width - rect.width) / 2; break; default: case SwingConstants.LEFT: case SwingConstants.LEADING: xOffset = 0; break; } int yOffset = 0; switch (label.getVerticalAlignment()) { case SwingConstants.TOP: yOffset = 0; break; case SwingConstants.CENTER: yOffset = (label.getBounds().height - rect.height) / 2; break; case SwingConstants.BOTTOM: yOffset = label.getBounds().height - rect.height; break; } return new Rectangle(xOffset, yOffset, rect.width, rect.height); }
From source file:org.esa.snap.graphbuilder.rcp.dialogs.support.GraphPanel.java
private static void drawHelp(final Graphics g) { final int x = (int) (g.getClipBounds().getWidth() / 2); final int y = (int) (g.getClipBounds().getHeight() / 2); final FontMetrics metrics = g.getFontMetrics(); final String name = "Right click here to add an operator"; final Rectangle2D rect = metrics.getStringBounds(name, g); final int stringWidth = (int) rect.getWidth(); g.setColor(Color.black);//from w w w . j a v a 2s . c o m g.drawString(name, x - stringWidth / 2, y); }
From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFBoolColumnCellRenderer.java
public void paint(Graphics g) { if (g == null) { return;//from w ww. java 2s. c o m } Rectangle bounds = getBounds(); g.setColor(getBackground()); g.fill3DRect(0, 0, bounds.width, bounds.height, true); g.setColor(getForeground()); int xoffbox = (bounds.width - 16) / 2; int yoffbox = (bounds.height - 16) / 2; g.drawRect(xoffbox, yoffbox, 16, 16); Boolean useval; if (value instanceof Boolean) { useval = (Boolean) value; } else if (value instanceof String) { String s = (String) value; if (s.equals("true")) { useval = new Boolean(true); } else if (s.equals("false")) { useval = new Boolean(false); } else { useval = null; } } else { useval = null; } String str; if (useval == null) { str = "?"; } else if (useval.booleanValue()) { str = "X"; } else { str = null; } if (str != null) { FontMetrics fm = g.getFontMetrics(); Rectangle sb = fm.getStringBounds(str, g).getBounds(); int ascent = fm.getAscent(); int leading = fm.getLeading(); int sxoff = xoffbox + ((16 - sb.width) / 2); int syoff = yoffbox + ((16 - (leading + ascent)) / 2) + leading + ascent; g.drawString(str, sxoff, syoff); } }
From source file:pl.edu.icm.visnow.system.main.VisNow.java
private static void renderSplashFrame(float progress, String loadText, String bottomTextUpperLine, String bottomTextLowerLine) { if (splash == null) { return;//www . jav a2 s. c o m } try { Graphics2D g = splash.createGraphics(); if (g == null) { return; } if (!splash.isVisible()) return; Rectangle bounds = splash.getBounds(); Font f = g.getFont(); FontMetrics fm = g.getFontMetrics(f); java.awt.geom.Rectangle2D rect = fm.getStringBounds(loadText, g); int texth = (int) Math.ceil(rect.getHeight()); g.setComposite(AlphaComposite.Clear); //g.setColor(Color.RED); g.fillRect(PROGRESS_TEXT_X_POSITION, bounds.height - PROGRESS_TEXT_Y_MARGIN - texth - 5, bounds.width - PROGRESS_TEXT_X_POSITION, texth + 10); g.setFont(lowerLineFont); fm = g.getFontMetrics(g.getFont()); rect = fm.getStringBounds(bottomTextLowerLine, g); int lowerLineTextHeight = (int) Math.ceil(rect.getHeight()); g.fillRect(BOTTOM_TEXT_X_MARGIN, bounds.height - BOTTOM_TEXT_Y_MARGIN - lowerLineTextHeight - 5, bounds.width - BOTTOM_TEXT_X_MARGIN, lowerLineTextHeight + 10); g.setFont(f); fm = g.getFontMetrics(g.getFont()); rect = fm.getStringBounds(bottomTextUpperLine, g); texth = (int) Math.ceil(rect.getHeight()); g.fillRect(BOTTOM_TEXT_X_MARGIN, bounds.height - lowerLineTextHeight - BOTTOM_TEXT_Y_MARGIN - texth - 5, bounds.width - BOTTOM_TEXT_X_MARGIN, lowerLineTextHeight + 10); g.setPaintMode(); // g.setColor(Color.BLACK); g.setColor(new Color(0, 75, 50)); g.drawString(loadText, PROGRESS_TEXT_X_POSITION, bounds.height - PROGRESS_TEXT_Y_MARGIN); g.drawString(bottomTextUpperLine, BOTTOM_TEXT_X_MARGIN, bounds.height - lowerLineTextHeight - BOTTOM_TEXT_Y_MARGIN); g.setFont(lowerLineFont); g.drawString(bottomTextLowerLine, BOTTOM_TEXT_X_MARGIN, bounds.height - BOTTOM_TEXT_Y_MARGIN); g.setFont(f); // g.setColor(Color.BLACK); g.setColor(new Color(0, 150, 100)); g.drawRect(PROGRESS_BAR_X_MARGIN, bounds.height - PROGRESS_BAR_Y_MARGIN, bounds.width - PROGRESS_BAR_X_MARGIN, PROGRESS_BAR_HEIGHT); int progressWidth = bounds.width - 2 * PROGRESS_BAR_X_MARGIN; int done = (int) (progressWidth * progress); g.fillRect(PROGRESS_BAR_X_MARGIN, bounds.height - PROGRESS_BAR_Y_MARGIN, PROGRESS_BAR_X_MARGIN + done, PROGRESS_BAR_HEIGHT); if (progress >= 1.0f) { g.fillRect(PROGRESS_BAR_X_MARGIN, bounds.height - PROGRESS_BAR_Y_MARGIN, bounds.width - PROGRESS_BAR_X_MARGIN, PROGRESS_BAR_HEIGHT); } splash.update(); } catch (IllegalStateException ex) { } }
From source file:haven.Utils.java
static Coord textsz(Graphics g, String text) { java.awt.FontMetrics m = g.getFontMetrics(); java.awt.geom.Rectangle2D ts = m.getStringBounds(text, g); return (new Coord((int) ts.getWidth(), (int) ts.getHeight())); }
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:nl.b3p.kaartenbalie.core.server.b3pLayering.ConfigLayer.java
protected void drawTitledMessageBox(Graphics2D g2d, String title, String message, int x, int y, int w, int h) { /* Do some calculations and init variables. */ g2d.setFont(KBConfiguration.OHD_messageBoxFont); FontMetrics fm = g2d.getFontMetrics(); int labelHeight = KBConfiguration.OHD_messageBoxFont.getSize() + (KBConfiguration.OHD_padding * 2); int angling = labelHeight; Rectangle2D testRectangle = fm.getStringBounds(title, g2d); int labelWidth = (int) testRectangle.getWidth(); if (w < labelWidth + (2 * angling)) { w = labelWidth + (2 * angling);/* w w w .j ava2 s .c o m*/ } y += labelHeight; /* Now draw the box... */ drawMessageBox(g2d, message, x, y, w, h); /* Draw the label background */ g2d.setColor(KBConfiguration.OHD_labelBoxColor); GeneralPath label = new GeneralPath(); label.moveTo(x, y); label.lineTo(x + angling, y - labelHeight); label.lineTo(x + angling + labelWidth, y - labelHeight); label.lineTo(x + (angling * 2) + labelWidth, y); label.closePath(); g2d.fill(label); /* Draw the label Lines.. */ g2d.setColor(KBConfiguration.OHD_borderBoxTopLeft); g2d.drawLine(x, y, x + angling, y - labelHeight); g2d.drawLine(x + angling, y - labelHeight, x + angling + labelWidth, y - labelHeight); g2d.setColor(KBConfiguration.OHD_borderBoxBottomRight); g2d.drawLine(x + angling + labelWidth, y - labelHeight, x + (angling * 2) + labelWidth, y); g2d.setColor(KBConfiguration.OHD_borderBoxBackground); g2d.drawLine(x + (angling * 2) + labelWidth, y, x, y); /*Then add the title... */ g2d.setColor(KBConfiguration.OHD_labelFontBoxColor); g2d.drawString(title, x + angling, y - KBConfiguration.OHD_padding); }
From source file:savant.view.tracks.TrackRenderer.java
public void drawFeatureLabel(Graphics2D g2, String geneName, double startXPos, double y) { FontMetrics fm = g2.getFontMetrics(); double stringstartx = startXPos - fm.stringWidth(geneName) - 5; if (stringstartx <= 0) { Rectangle2D r = fm.getStringBounds(geneName, g2); int b = 2; Color textColor = g2.getColor(); g2.setColor(new Color(255, 255, 255, 200)); g2.fill(new RoundRectangle2D.Double(3.0, y - (fm.getHeight() - fm.getDescent()) - b, r.getWidth() + 2 * b, r.getHeight() + 2 * b, 8.0, 8.0)); g2.setColor(textColor);/* ww w . j a v a2 s. c o m*/ g2.drawString(geneName, 5.0F, (float) y); } else { g2.drawString(geneName, (float) stringstartx, (float) y); } }
From source file:org.fcrepo.localservices.imagemanip.ImageManipulation.java
/** * Method automatically called by browser to handle image manipulations. * // w w w. j av a2 s . c om * @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.esa.snap.graphbuilder.rcp.dialogs.support.GraphNode.java
/** * Draw a GraphNode as a rectangle with a name * * @param g The Java2D Graphics/*from w ww. j av a2s. com*/ * @param col The color to draw */ public void drawNode(final Graphics2D g, final Color col) { final int x = displayPosition.x; final int y = displayPosition.y; g.setFont(g.getFont().deriveFont(Font.BOLD, 11)); final FontMetrics metrics = g.getFontMetrics(); final String name = node.getId(); final Rectangle2D rect = metrics.getStringBounds(name, g); final int stringWidth = (int) rect.getWidth(); setSize(Math.max(stringWidth, 50) + 10, 25); int step = 4; int alpha = 96; for (int i = 0; i < step; ++i) { g.setColor(new Color(0, 0, 0, alpha - (32 * i))); g.drawLine(x + i + 1, y + nodeHeight + i, x + nodeWidth + i - 1, y + nodeHeight + i); g.drawLine(x + nodeWidth + i, y + i, x + nodeWidth + i, y + nodeHeight + i); } Shape clipShape = new Rectangle(x, y, nodeWidth, nodeHeight); g.setComposite(AlphaComposite.SrcAtop); g.setPaint(new GradientPaint(x, y, col, x + nodeWidth, y + nodeHeight, col.darker())); g.fill(clipShape); g.setColor(Color.blue); g.draw3DRect(x, y, nodeWidth - 1, nodeHeight - 1, true); g.setColor(Color.BLACK); g.drawString(name, x + (nodeWidth - stringWidth) / 2, y + 15); }