List of usage examples for java.awt Graphics2D getFontMetrics
public FontMetrics getFontMetrics()
From source file:savant.view.swing.GraphPane.java
private void drawMessageHelper(Graphics2D g2, String message, Font font, int w, int h, int offset) { g2.setFont(font);/*www. j ava2 s. com*/ FontMetrics metrics = g2.getFontMetrics(); Rectangle2D stringBounds = font.getStringBounds(message, g2.getFontRenderContext()); int preferredWidth = (int) stringBounds.getWidth() + metrics.getHeight(); int preferredHeight = (int) stringBounds.getHeight() + metrics.getHeight(); w = Math.min(preferredWidth, w); h = Math.min(preferredHeight, h); int x = (getWidth() - (int) stringBounds.getWidth()) / 2; int y = (getHeight() / 2) + ((metrics.getAscent() - metrics.getDescent()) / 2) + offset; g2.drawString(message, x, y); }
From source file:edu.ku.brc.ui.dnd.SimpleGlassPane.java
@Override protected void paintComponent(Graphics graphics) { Graphics2D g = (Graphics2D) graphics; Rectangle rect = getInternalBounds(); int width = rect.width; int height = rect.height; if (useBGImage) { // Create a translucent intermediate image in which we can perform // the soft clipping GraphicsConfiguration gc = g.getDeviceConfiguration(); if (img == null || img.getWidth() != width || img.getHeight() != height) { img = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT); }/*from w w w .ja v a 2 s . c o m*/ Graphics2D g2 = img.createGraphics(); // Clear the image so all pixels have zero alpha g2.setComposite(AlphaComposite.Clear); g2.fillRect(0, 0, width, height); g2.setComposite(AlphaComposite.Src); g2.setColor(new Color(0, 0, 0, 85)); g2.fillRect(0, 0, width, height); if (delegateRenderer != null) { delegateRenderer.render(g, g2, img); } g2.dispose(); // Copy our intermediate image to the screen g.drawImage(img, rect.x, rect.y, null); } super.paintComponent(graphics); if (StringUtils.isNotEmpty(text)) { Graphics2D g2 = (Graphics2D) graphics; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(fillColor); g2.fillRect(margin.left, margin.top, rect.width, rect.height); g2.setFont(new Font((new JLabel()).getFont().getName(), Font.BOLD, pointSize)); FontMetrics fm = g2.getFontMetrics(); int tw = fm.stringWidth(text); int th = fm.getHeight(); int tx = (rect.width - tw) / 2; int ty = (rect.height - th) / 2; if (yPos != null) { ty = yPos; } int expand = 20; int arc = expand * 2; g2.setColor(new Color(0, 0, 0, 50)); int x = margin.left + tx - (expand / 2); int y = margin.top + ty - fm.getAscent() - (expand / 2); drawBGContainer(g2, true, x + 4, y + 6, tw + expand, th + expand, arc, arc); g2.setColor(new Color(255, 255, 255, 220)); drawBGContainer(g2, true, x, y, tw + expand, th + expand, arc, arc); g2.setColor(Color.DARK_GRAY); drawBGContainer(g2, false, x, y, tw + expand, th + expand, arc, arc); g2.setColor(textColor == null ? Color.BLACK : textColor); g2.drawString(text, tx, ty); } }
From source file:org.cruk.mga.CreateReport.java
/** * Draws the x-axis for the number of sequences and the legend. * * @param g2/*w ww . ja va 2 s . c om*/ * @param x0 * @param y * @param tickIntervals * @param maxSequenceCount * @return */ private int drawAxisAndLegend(Graphics2D g2, int x0, int y, int tickIntervals, long maxSequenceCount) { g2.setColor(Color.BLACK); g2.setFont(axisFont); boolean millions = maxSequenceCount / tickIntervals >= 1000000; long largestTickValue = maxSequenceCount; if (millions) largestTickValue /= 1000000; int w = g2.getFontMetrics().stringWidth(Long.toString(largestTickValue)); int x1 = plotWidth - (w / 2) - gapSize; g2.drawLine(x0, y, x1, y); int tickFontHeight = g2.getFontMetrics().getAscent(); int tickHeight = tickFontHeight / 2; for (int i = 0; i <= tickIntervals; i++) { int x = x0 + i * (x1 - x0) / tickIntervals; g2.drawLine(x, y, x, y + tickHeight); long tickValue = i * maxSequenceCount / tickIntervals; if (millions) tickValue /= 1000000; String s = Long.toString(tickValue); int xs = x - g2.getFontMetrics().stringWidth(s) / 2 + 1; int ys = y + tickHeight + tickFontHeight + 1; g2.drawString(s, xs, ys); } g2.setFont(font); int fontHeight = g2.getFontMetrics().getAscent(); String s = "Number of sequences"; if (millions) s += " (millions)"; int xs = x0 + (x1 - x0 - g2.getFontMetrics().stringWidth(s)) / 2; int ys = y + tickHeight + tickFontHeight + fontHeight + fontHeight / 3; g2.drawString(s, xs, ys); int yl = ys + fontHeight * 2; int xl = x0; int barHeight = (int) (fontHeight * 0.7f); int barWidth = 3 * barHeight; int yb = yl + (int) (fontHeight * 0.3f); int gap = (int) (fontHeight * 0.4f); g2.setColor(Color.GREEN); g2.fillRect(xl, yb, barWidth, barHeight); g2.setColor(Color.BLACK); g2.drawRect(xl, yb, barWidth, barHeight); g2.setFont(axisFont); String label = "Sequenced species/genome"; xl += barWidth + gap; g2.drawString(label, xl, yl + fontHeight); xl += g2.getFontMetrics().stringWidth(label) + gap * 3; g2.setColor(Color.ORANGE); g2.fillRect(xl, yb, barWidth, barHeight); g2.setColor(Color.BLACK); g2.drawRect(xl, yb, barWidth, barHeight); label = "Control"; xl += barWidth + gap; g2.drawString(label, xl, yl + fontHeight); xl += g2.getFontMetrics().stringWidth(label) + gap * 3; g2.setColor(Color.RED); g2.fillRect(xl, yb, barWidth, barHeight); g2.setColor(Color.BLACK); g2.drawRect(xl, yb, barWidth, barHeight); label = "Contaminant"; xl += barWidth + gap; g2.drawString(label, xl, yl + fontHeight); xl += g2.getFontMetrics().stringWidth(label) + gap * 3; g2.setColor(ADAPTER_COLOR); g2.fillRect(xl, yb, barWidth, barHeight); g2.setColor(Color.BLACK); g2.drawRect(xl, yb, barWidth, barHeight); label = "Adapter"; xl += barWidth + gap; g2.drawString(label, xl, yl + fontHeight); xl += g2.getFontMetrics().stringWidth(label) + gap * 3; g2.setColor(Color.BLACK); g2.drawRect(xl, yb, barWidth, barHeight); label = "Unmapped"; xl += barWidth + gap; g2.drawString(label, xl, yl + fontHeight); xl += g2.getFontMetrics().stringWidth(label) + gap * 3; g2.setColor(Color.GRAY); g2.fillRect(xl, yb, barWidth, barHeight); g2.setColor(Color.BLACK); g2.drawRect(xl, yb, barWidth, barHeight); label = "Unknown"; xl += barWidth + gap; g2.drawString(label, xl, yl + fontHeight); return x1; }
From source file:coolmap.canvas.datarenderer.renderer.impl.NumberToColor.java
private void updateGradient() { // System.out.println("Gradient updated.."); _gradient.reset();//from ww w.j a va2 s.c om for (int i = 0; i < editor.getNumPoints(); i++) { Color c = editor.getColorAt(i); float p = editor.getColorPositionAt(i); if (c == null || p < 0 || p > 1) { continue; } _gradient.addColor(c, p); } _gradientColors = _gradient.generateGradient(CImageGradient.InterType.Linear); int width = DEFAULT_LEGEND_WIDTH; int height = DEFAULT_LEGENT_HEIGHT; legend = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); Graphics2D g = (Graphics2D) legend.createGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); LinearGradientPaint paint = editor.getLinearGradientPaint(0, 0, width, 0); g.setPaint(paint); g.fillRoundRect(0, 0, width, height - 12, 5, 5); g.setColor(UI.colorBlack2); g.setFont(UI.fontMono.deriveFont(10f)); DecimalFormat format = new DecimalFormat("#.##"); g.drawString(format.format(_minValue), 2, 23); String maxString = format.format(_maxValue); int swidth = g.getFontMetrics().stringWidth(maxString); g.drawString(maxString, width - 2 - swidth, 23); g.dispose(); // System.out.println("===Gradient updated===" + _gradientColors + " " + this); }
From source file:com.fluidops.iwb.deepzoom.ImageLoader.java
private void generateIDCard(URI uri, Map<URI, Set<Value>> facets, String url, File file) { int width = 200; int height = 200; BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D ig2 = bi.createGraphics(); ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ig2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); /* Special ID card handling for certain entity types */ /* TODO: special images based on type if(facets.containsKey(RDF.TYPE)) {/*from ww w . jav a2 s.c om*/ Set<Value> facet = facets.get(RDF.TYPE); for(Value v : facet) { if(v.equals(Vocabulary.DCAT_DATASET)) { Image img = null; try { img = ImageIO.read( new File( "webapps/ROOT/images/rdf.jpg" ) ); } catch (MalformedURLException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error("Could not get image"); } ig2.drawImage( img, 0, 0, null ); break; } } } */ String label = EndpointImpl.api().getDataManager().getLabel(uri); Font font = new Font(Font.SANS_SERIF, Font.BOLD, 20); ig2.setFont(font); FontMetrics fontMetrics = ig2.getFontMetrics(); int labelwidth = fontMetrics.stringWidth(label); if (labelwidth >= width) { int fontsize = 20 * width / labelwidth; font = new Font(Font.SANS_SERIF, Font.BOLD, fontsize); ig2.setFont(font); fontMetrics = ig2.getFontMetrics(); } int x = (width - fontMetrics.stringWidth(label)) / 2; int y = (fontMetrics.getAscent() + (height - (fontMetrics.getAscent() + fontMetrics.getDescent())) / 2); ig2.setPaint(Color.black); ig2.drawString(label, x, y); BufferedOutputStream out; try { out = new BufferedOutputStream(new FileOutputStream(file)); ImageIO.write(bi, "PNG", out); out.flush(); out.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } }
From source file:coolmap.canvas.datarenderer.renderer.impl.NumberToBoxPlot.java
private void updateLegend() { int width = DEFAULT_LEGEND_WIDTH; int height = DEFAULT_LEGENT_HEIGHT; legend = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); Graphics2D g = (Graphics2D) legend.createGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setPaint(UI.colorBlack2);//from w ww.j av a 2 s .c o m g.fillRoundRect(0, 0, width, height - 12, 5, 5); g.setColor(barColorBelow); int boxNum = 10; g.setStroke(UI.stroke1_5); double value; for (int i = 0; i < boxNum; i++) { value = _minValue + (_maxValue - _minValue) / boxNum * i; if (value > disectBound) { g.setColor(barColorNormal); } int h = (height - 12) / boxNum * i; g.drawLine(i * width / boxNum, height - 12 - h, (i + 1) * width / boxNum, height - 12 - h); } g.setColor(Color.BLACK); g.setFont(UI.fontMono.deriveFont(10f)); DecimalFormat format = new DecimalFormat("#.##"); g.drawString(format.format(_minValue), 2, 23); g.setColor(Color.BLACK); String maxString = format.format(_maxValue); int swidth = g.getFontMetrics().stringWidth(maxString); g.drawString(maxString, width - 2 - swidth, 23); g.dispose(); }
From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java
private void renderDayMarkers(long pStart, long pEnd, Graphics2D pG2D) { Date d = new Date(pStart); d = DateUtils.setHours(d, 0);/*from w w w .jav a2 s . com*/ d = DateUtils.setMinutes(d, 0); d = DateUtils.setSeconds(d, 0); d = DateUtils.setMilliseconds(d, 0); for (long mark = d.getTime(); mark <= pEnd; mark += DateUtils.MILLIS_PER_HOUR) { int markerPos = Math.round((mark - pStart) / DateUtils.MILLIS_PER_MINUTE); pG2D.setColor(Color.YELLOW); pG2D.fillRect(markerPos, 20, 2, 10); pG2D.setColor(Color.WHITE); pG2D.setFont(pG2D.getFont().deriveFont(Font.BOLD, 10.0f)); pG2D.fillRect(markerPos - 5, 15, 12, 10); pG2D.setColor(Color.BLACK); Calendar cal = Calendar.getInstance(); cal.setTime(new Date(mark)); NumberFormat f = NumberFormat.getNumberInstance(); f.setMinimumIntegerDigits(2); f.setMaximumFractionDigits(2); pG2D.drawString(f.format(cal.get(Calendar.HOUR_OF_DAY)), markerPos - 5, 23); } long currentDay = d.getTime(); while (currentDay < pEnd) { currentDay += DateUtils.MILLIS_PER_DAY; int markerPos = Math.round((currentDay - pStart) / DateUtils.MILLIS_PER_MINUTE); pG2D.setColor(new Color(123, 123, 123)); pG2D.fillRect(markerPos, 15, 3, 30); SimpleDateFormat f = new SimpleDateFormat("dd.MM.yy"); String dayLabel = f.format(currentDay); Rectangle2D labelBounds = pG2D.getFontMetrics().getStringBounds(dayLabel, pG2D); pG2D.setColor(Color.YELLOW); int labelWidth = (int) labelBounds.getWidth() + 5; pG2D.fillRect(markerPos - (int) Math.rint(labelWidth / 2), 15, labelWidth, 10); pG2D.setColor(Color.BLACK); pG2D.setFont(pG2D.getFont().deriveFont(Font.BOLD, 10.0f)); pG2D.drawString(dayLabel, markerPos - (int) Math.rint(labelWidth / 2) + 2, 23); } }
From source file:com.moviejukebox.plugin.DefaultImagePlugin.java
private BufferedImage drawText(BufferedImage bi, String outputText, boolean verticalAlign) { Graphics2D g2d = bi.createGraphics(); g2d.setFont(new Font(textFont, Font.BOLD, textFontSize)); FontMetrics fm = g2d.getFontMetrics(); int textWidth = fm.stringWidth(outputText); int imageWidth = bi.getWidth(); int imageHeight = bi.getHeight(); int leftAlignment; int topAlignment; if (textAlignment.equalsIgnoreCase(LEFT)) { leftAlignment = textOffset;//from www. j a va 2s. c o m } else if (textAlignment.equalsIgnoreCase(RIGHT)) { leftAlignment = imageWidth - textWidth - textOffset; } else { leftAlignment = (imageWidth / 2) - (textWidth / 2); } if (verticalAlign) { // Align the text to the top topAlignment = fm.getHeight() - 10; } else { // Align the text to the bottom topAlignment = imageHeight - 10; } // Create the drop shadow if (StringUtils.isNotBlank(textFontShadow)) { g2d.setColor(getColor(textFontShadow, Color.DARK_GRAY)); g2d.drawString(outputText, leftAlignment + 2, topAlignment + 2); } // Create the text itself g2d.setColor(getColor(textFontColor, Color.LIGHT_GRAY)); g2d.drawString(outputText, leftAlignment, topAlignment); g2d.dispose(); return bi; }
From source file:net.geoprism.dashboard.DashboardMap.java
private BufferedImage getLegendTitleImage(DashboardLayer layer) { FontMetrics fm;/*from w w w . ja v a 2s. co m*/ int textWidth; int textHeight; int textBoxHorizontalPadding = 4; int textBoxVerticalPadding = 4; int borderWidth = 2; int paddedTitleHeight; int paddedTitleWidth; int titleLeftPadding = textBoxHorizontalPadding; BufferedImage newLegendTitleBase; Graphics2D newLegendTitleBaseGraphic = null; try { // Build the Font object Font titleFont = new Font(layer.getName(), Font.BOLD, 14); // Build variables for base legend graphic construction try { newLegendTitleBase = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); newLegendTitleBaseGraphic = newLegendTitleBase.createGraphics(); newLegendTitleBaseGraphic.setFont(titleFont); fm = newLegendTitleBaseGraphic.getFontMetrics(); textHeight = fm.getHeight(); textWidth = fm.stringWidth(layer.getName()); paddedTitleWidth = textWidth + (textBoxHorizontalPadding * 2) + (borderWidth * 2); paddedTitleHeight = textHeight + (textBoxVerticalPadding * 2) + (borderWidth * 2); } finally { // dispose of temporary graphics context if (newLegendTitleBaseGraphic != null) { newLegendTitleBaseGraphic.dispose(); } } titleLeftPadding = ((paddedTitleWidth / 2) - ((textWidth + (textBoxHorizontalPadding * 2) + (borderWidth * 2)) / 2)) + textBoxHorizontalPadding; newLegendTitleBase = new BufferedImage(paddedTitleWidth, paddedTitleHeight, BufferedImage.TYPE_INT_ARGB); newLegendTitleBaseGraphic = newLegendTitleBase.createGraphics(); newLegendTitleBaseGraphic.drawImage(newLegendTitleBase, 0, 0, null); newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); newLegendTitleBaseGraphic.setFont(titleFont); // draw title text fm = newLegendTitleBaseGraphic.getFontMetrics(); newLegendTitleBaseGraphic.setColor(Color.WHITE); newLegendTitleBaseGraphic.drawString(layer.getName(), titleLeftPadding, fm.getAscent() + textBoxVerticalPadding); newLegendTitleBaseGraphic.drawImage(newLegendTitleBase, 0, 0, null); } finally { if (newLegendTitleBaseGraphic != null) { newLegendTitleBaseGraphic.dispose(); } } return newLegendTitleBase; }
From source file:edu.ku.brc.ui.UIRegistry.java
/** * Writes a string message into the BufferedImage on GlassPane and sets the main component's visibility to false and * shows the GlassPane.//from w w w . j a v a 2 s. c o m * @param msg the message * @param pointSize the Font point size for the message to be writen in */ public static GhostGlassPane writeGlassPaneMsg(final String msg, final int pointSize) { GhostGlassPane glassPane = getGlassPane(); if (glassPane != null) { glassPane.finishDnD(); } glassPane.setMaskingEvents(true); Component mainComp = get(MAINPANE); if (mainComp != null && glassPane != null) { JFrame frame = (JFrame) get(FRAME); frameRect = frame.getBounds(); int y = 0; JMenuBar menuBar = null; Dimension size = mainComp.getSize(); if (UIHelper.getOSType() != UIHelper.OSTYPE.MacOSX) { menuBar = frame.getJMenuBar(); size.height += menuBar.getSize().height; y += menuBar.getSize().height; } BufferedImage buffer = getGlassPaneBufferedImage(size.width, size.height); Graphics2D g2 = buffer.createGraphics(); if (menuBar != null) { menuBar.paint(g2); } g2.translate(0, y); mainComp.paint(g2); g2.translate(0, -y); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(new Color(255, 255, 255, 128)); g2.fillRect(0, 0, size.width, size.height); g2.setFont(new Font((new JLabel()).getFont().getName(), Font.BOLD, pointSize)); FontMetrics fm = g2.getFontMetrics(); int tw = fm.stringWidth(msg); int th = fm.getHeight(); int tx = (size.width - tw) / 2; int ty = (size.height - th) / 2; int expand = 20; int arc = expand * 2; g2.setColor(Color.WHITE); g2.fillRoundRect(tx - (expand / 2), ty - fm.getAscent() - (expand / 2), tw + expand, th + expand, arc, arc); g2.setColor(Color.DARK_GRAY); g2.drawRoundRect(tx - (expand / 2), ty - fm.getAscent() - (expand / 2), tw + expand, th + expand, arc, arc); g2.setColor(Color.BLACK); g2.drawString(msg, tx, ty); g2.dispose(); glassPane.setImage(buffer); glassPane.setPoint(new Point(0, 0), GhostGlassPane.ImagePaintMode.ABSOLUTE); glassPane.setOffset(new Point(0, 0)); glassPane.setVisible(true); mainComp.setVisible(false); //Using paintImmediately fixes problems with glass pane not showing, such as for workbench saves initialed //during workbench or app shutdown. Don't know if there is a better way to fix it. //glassPane.repaint(); glassPane.paintImmediately(glassPane.getBounds()); showingGlassPane = true; } return glassPane; }