List of usage examples for java.awt.font LineMetrics getAscent
public abstract float getAscent();
From source file:Main.java
/** Draw 'str' centered at 'p'. */ public static void drawCenteredText(Graphics g, Point p, String str) { FontMetrics fm = g.getFontMetrics(); LineMetrics lm = fm.getLineMetrics(str, g); // Go to 'p', then add a/2 to get to the baseline. // I ignore the descent because it looks better to center without // regard to descenders. int baseY = p.y + (int) (lm.getAscent() / 2); int baseX = p.x - fm.stringWidth(str) / 2; g.drawString(str, baseX, baseY);// w w w . j av a 2s . com }
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);//from w w w. j a v a2s . c o 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);/* ww w. java2 s. c o m*/ 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: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 w w w . j a v a2 s. co 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: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);//w w w . j a v a2 s .c o 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:net.sourceforge.processdash.ui.web.reports.RadarPlot.java
/** * Draws the label for one radar axis./*from ww w . ja va 2 s . co m*/ * * @param g2 The graphics device. * @param chartArea The area for the radar chart. * @param data The data for the plot. * @param axis The axis (zero-based index). * @param startAngle The starting angle. */ protected void drawLabel(Graphics2D g2, Rectangle2D chartArea, String label, int axis, double labelX, double labelY) { // handle label drawing... FontRenderContext frc = g2.getFontRenderContext(); Rectangle2D labelBounds = this.axisLabelFont.getStringBounds(label, frc); LineMetrics lm = this.axisLabelFont.getLineMetrics(label, frc); double ascent = lm.getAscent(); if (labelX == chartArea.getCenterX()) labelX -= labelBounds.getWidth() / 2; else if (labelX < chartArea.getCenterX()) labelX -= labelBounds.getWidth(); if (labelY > chartArea.getCenterY()) labelY += ascent; g2.setPaint(this.axisLabelPaint); g2.setFont(this.axisLabelFont); g2.drawString(label, (float) labelX, (float) labelY); }
From source file:genlab.gui.jfreechart.EnhancedSpiderWebPlot.java
/** * Draws the label for one axis.//from w w w . j a va 2 s. c o m * * @param g2 the graphics device. * @param plotArea the plot area * @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, 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); } Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc); LineMetrics lm = getLabelFont().getLineMetrics(label, frc); double ascent = lm.getAscent(); Point2D labelLocation = calculateLabelLocation(labelBounds, ascent, plotArea, startAngle); Composite saveComposite = g2.getComposite(); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); g2.setPaint(getLabelPaint()); g2.setFont(getLabelFont()); g2.drawString(label, (float) labelLocation.getX(), (float) labelLocation.getY()); g2.setComposite(saveComposite); }
From source file:spinworld.gui.RadarPlot.java
/** * Draws the label for one axis.//from w w w . j a v a 2 s . co 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:ded.ui.DiagramController.java
/** The core of the paint routine, after we decide whether to interpose * another buffer. *///from ww w .j av a 2s . c o m private void innerPaint(Graphics g) { super.paint(g); // I do not know the proper way to get a font set automatically // in a Graphics object. Calling JComponent.setFont has gotten // me nowhere. Setting it myself when I first get control // seems to work; but note that I have to do this *after* // calling super.paint(). g.setFont(this.dedWindow.diagramFont); // Filename label. if (this.diagram.drawFileName && !this.fileName.isEmpty()) { String name = new File(this.fileName).getName(); FontMetrics fm = g.getFontMetrics(); LineMetrics lm = fm.getLineMetrics(name, g); int x = fileNameLabelMargin; int y = fileNameLabelMargin + (int) lm.getAscent(); g.drawString(name, x, y); y += (int) lm.getUnderlineOffset() + 1 /*...*/; g.drawLine(x, y, x + fm.stringWidth(name), y); } // Controllers. for (Controller c : this.controllers) { if (c.isSelected()) { c.paintSelectionBackground(g); } c.paint(g); } // Lasso rectangle. if (this.mode == Mode.DCM_RECT_LASSO) { Rectangle r = this.getLassoRect(); g.drawRect(r.x, r.y, r.width, r.height); } // Current focused Component. if (debugFocus) { KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); Component fo = kfm.getFocusOwner(); g.drawString("Focus: " + fo, 3, this.getHeight() - 22); } // Mode label. if (this.mode != Mode.DCM_SELECT) { g.drawString("Mode: " + this.mode.description, 3, this.getHeight() - 4); } else if (this.fpsMeasurementMode) { this.fpsFrameCount++; long current = System.currentTimeMillis(); long millis = current - this.fpsStartMillis; if (millis > 1000) { // Update the FPS measurement with the results for this // interval. this.fpsSampleCount++; this.fpsMeasurement = "FPS: " + this.fpsFrameCount + " (millis=" + millis + ", samples=" + this.fpsSampleCount + ")"; // Reset the counters. this.fpsStartMillis = current; this.fpsFrameCount = 0; } g.drawString(this.fpsMeasurement + " (Ctrl+G to stop)", 3, this.getHeight() - 4); } }