List of usage examples for java.awt Font getLineMetrics
public LineMetrics getLineMetrics(String str, FontRenderContext frc)
From source file:FontShow.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Dialog", Font.PLAIN, 96); g2.setFont(font);/*from ww w. j av a 2 s. co m*/ int width = getSize().width; int height = getSize().height; String message = "Java2s"; FontRenderContext frc = g2.getFontRenderContext(); LineMetrics metrics = font.getLineMetrics(message, frc); float messageWidth = (float) font.getStringBounds(message, frc).getWidth(); // center text float ascent = metrics.getAscent(); float descent = metrics.getDescent(); float x = (width - messageWidth) / 2; float y = (height + metrics.getHeight()) / 2 - descent; int PAD = 25; g2.setPaint(getBackground()); g2.fillRect(0, 0, width, height); g2.setPaint(getForeground()); g2.drawString(message, x, y); g2.setPaint(Color.white); // Base lines drawLine(g2, x - PAD, y, x + messageWidth + PAD, y); drawLine(g2, x, y + PAD, x, y - ascent - PAD); g2.setPaint(Color.green); // Ascent line drawLine(g2, x - PAD, y - ascent, x + messageWidth + PAD, y - ascent); g2.setPaint(Color.red); // Descent line drawLine(g2, x - PAD, y + descent, x + messageWidth + PAD, y + descent); }
From source file:MainClass.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Serif", Font.PLAIN, 72); g2.setFont(font);//from www .j a v a2s . c o m String s = "www.java2s.com"; float x = 50, y = 150; FontRenderContext frc = g2.getFontRenderContext(); float width = (float) font.getStringBounds(s, frc).getWidth(); Line2D baseline = new Line2D.Float(x, y, x + width, y); g2.setPaint(Color.lightGray); g2.draw(baseline); // Draw the ascent. LineMetrics lm = font.getLineMetrics(s, frc); Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent()); g2.draw(ascent); // Draw the descent. Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent()); g2.draw(descent); // Draw the leading. Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width, y + lm.getDescent() + lm.getLeading()); g2.draw(leading); // Render the string. g2.setPaint(Color.black); g2.drawString(s, x, y); }
From source file:Main.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Serif", Font.PLAIN, 72); g2.setFont(font);// ww w . j a v a2 s.co m String s = "this is a test"; float x = 50, y = 150; // Draw the baseline. FontRenderContext frc = g2.getFontRenderContext(); float width = (float) font.getStringBounds(s, frc).getWidth(); Line2D baseline = new Line2D.Float(x, y, x + width, y); g2.setPaint(Color.red); g2.draw(baseline); // Draw the ascent. LineMetrics lm = font.getLineMetrics(s, frc); Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent()); g2.draw(ascent); // Draw the descent. Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent()); g2.draw(descent); // Draw the leading. Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width, y + lm.getDescent() + lm.getLeading()); g2.draw(leading); // Render the string. g2.setPaint(Color.black); g2.drawString(s, x, y); }
From source file:LineMetricsIllustration.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Serif", Font.PLAIN, 72); g2.setFont(font);/*w ww . j a va 2 s . c om*/ String s = "Java Source and Support"; float x = 50, y = 150; // Draw the baseline. FontRenderContext frc = g2.getFontRenderContext(); float width = (float) font.getStringBounds(s, frc).getWidth(); Line2D baseline = new Line2D.Float(x, y, x + width, y); g2.setPaint(Color.red); g2.draw(baseline); // Draw the ascent. LineMetrics lm = font.getLineMetrics(s, frc); Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent()); g2.draw(ascent); // Draw the descent. Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent()); g2.draw(descent); // Draw the leading. Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width, y + lm.getDescent() + lm.getLeading()); g2.draw(leading); // Render the string. g2.setPaint(Color.black); g2.drawString(s, x, y); }
From source file:net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.java
protected float determineLeading(Font font) { LineMetrics lineMetrics = font.getLineMetrics(" ", context.getFontRenderContext()); return lineMetrics.getLeading(); }
From source file:Chart.java
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // compute the minimum and maximum values if (values == null) return; double minValue = 0; double maxValue = 0; for (double v : values) {//from w w w . j a va 2 s . c o m if (minValue > v) minValue = v; if (maxValue < v) maxValue = v; } if (maxValue == minValue) return; int panelWidth = getWidth(); int panelHeight = getHeight(); Font titleFont = new Font("SansSerif", Font.BOLD, 20); Font labelFont = new Font("SansSerif", Font.PLAIN, 10); // compute the extent of the title FontRenderContext context = g2.getFontRenderContext(); Rectangle2D titleBounds = titleFont.getStringBounds(title, context); double titleWidth = titleBounds.getWidth(); double top = titleBounds.getHeight(); // draw the title double y = -titleBounds.getY(); // ascent double x = (panelWidth - titleWidth) / 2; g2.setFont(titleFont); g2.drawString(title, (float) x, (float) y); // compute the extent of the bar labels LineMetrics labelMetrics = labelFont.getLineMetrics("", context); double bottom = labelMetrics.getHeight(); y = panelHeight - labelMetrics.getDescent(); g2.setFont(labelFont); // get the scale factor and width for the bars double scale = (panelHeight - top - bottom) / (maxValue - minValue); int barWidth = panelWidth / values.length; // draw the bars for (int i = 0; i < values.length; i++) { // get the coordinates of the bar rectangle double x1 = i * barWidth + 1; double y1 = top; double height = values[i] * scale; if (values[i] >= 0) y1 += (maxValue - values[i]) * scale; else { y1 += maxValue * scale; height = -height; } // fill the bar and draw the bar outline Rectangle2D rect = new Rectangle2D.Double(x1, y1, barWidth - 2, height); g2.setPaint(Color.RED); g2.fill(rect); g2.setPaint(Color.BLACK); g2.draw(rect); // draw the centered label below the bar Rectangle2D labelBounds = labelFont.getStringBounds(names[i], context); double labelWidth = labelBounds.getWidth(); x = x1 + (barWidth - labelWidth) / 2; g2.drawString(names[i], (float) x, (float) y); } }
From source file:spinworld.gui.RadarPlot.java
/** * Draws the label for one axis.//from ww w. j a va2 s .c o m * * @param g2 the graphics device. * @param plotArea whole plot drawing area (e.g. including space for labels) * @param plotDrawingArea the plot drawing area (just spanning of axis) * @param value the value of the label (ignored). * @param cat the category (zero-based index). * @param startAngle the starting angle. * @param extent the extent of the arc. */ protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, Rectangle2D plotDrawingArea, double value, int cat, double startAngle, double extent) { FontRenderContext frc = g2.getFontRenderContext(); String label = null; if (this.dataExtractOrder == TableOrder.BY_ROW) { // if series are in rows, then the categories are the column keys label = this.labelGenerator.generateColumnLabel(this.dataset, cat); } else { // if series are in columns, then the categories are the row keys label = this.labelGenerator.generateRowLabel(this.dataset, cat); } double angle = normalize(startAngle); Font font = getLabelFont(); Point2D labelLocation; do { Rectangle2D labelBounds = font.getStringBounds(label, frc); LineMetrics lm = font.getLineMetrics(label, frc); double ascent = lm.getAscent(); labelLocation = calculateLabelLocation(labelBounds, ascent, plotDrawingArea, startAngle); boolean leftOut = angle > 90 && angle < 270 && labelLocation.getX() < plotArea.getX(); boolean rightOut = (angle < 90 || angle > 270) && labelLocation.getX() + labelBounds.getWidth() > plotArea.getX() + plotArea.getWidth(); if (leftOut || rightOut) { font = font.deriveFont(font.getSize2D() - 1); } else { break; } } while (font.getSize() > 8); Composite saveComposite = g2.getComposite(); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); g2.setPaint(getLabelPaint()); g2.setFont(font); g2.drawString(label, (float) labelLocation.getX(), (float) labelLocation.getY()); g2.setComposite(saveComposite); }
From source file:org.kalypsodeegree_impl.graphics.displayelements.LabelFactory.java
/** * Generates <tt>Label</tt> -representations for a given <tt>LabelDisplayElement</tt>. *///w w w . ja v a 2s .co m public Label[] createLabels() throws GM_Exception { try { final Feature feature = m_element.getFeature(); final String caption = m_element.getLabel().evaluate(feature); // sanity check: empty labels are ignored if (StringUtils.isBlank(caption)) return EMPTY_LABELS; // gather font information final org.kalypsodeegree.graphics.sld.Font sldFont = m_symbolizer.getFont(); final java.awt.Font font = new java.awt.Font(sldFont.getFamily(feature), sldFont.getStyle(feature) | sldFont.getWeight(feature), sldFont.getSize(feature)); m_graphics.setFont(font); final Color color = sldFont.getColor(feature); final FontRenderContext frc = m_graphics.getFontRenderContext(); // bugstories... // I got the following error in the next line: // # An unexpected error has been detected by HotSpot Virtual Machine: // # [...] // # Problematic frame: // # C [libfontmanager.so+0x2ecd5] // // while running kalypso on linux, kubuntu-Dapper. // The error was caused by some buggy fonts in Dapper (Rekha-normal and aakar-MagNet ). // Work-around is to remove the toxic fonts by removing the package ttf-gujarati-fonts from the distribution. // this error was not easy to locate, so do not remove this notice ! // ( v.doemming@tuhh.de ) final Rectangle2D bounds = font.getStringBounds(caption, frc); final Dimension size = bounds.getBounds().getSize(); final LineMetrics metrics = font.getLineMetrics(caption, frc); final GM_Object[] geometries = m_element.getGeometry(); final List<Label> allLabels = new ArrayList<>(); for (final GM_Object geometry : geometries) { final Label[] labels = createLabels(feature, caption, geometry, font, color, metrics, size); allLabels.addAll(Arrays.asList(labels)); } return allLabels.toArray(new Label[allLabels.size()]); } catch (final FilterEvaluationException e) { // if properties are unknown to features, this should be ignored! return EMPTY_LABELS; } }