List of usage examples for java.awt FontMetrics getAscent
public int getAscent()
From source file:ucar.unidata.idv.control.chart.TrackSegment.java
/** * Draws the wayPoint.// w w w . java 2 s .c o m * * @param g2 the graphics device. * @param plot the plot. * @param dataArea the data area. * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param rendererIndex the renderer index. * @param info an optional info object that will be populated with * entity information. */ public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis, int rendererIndex, PlotRenderingInfo info) { super.setGraphicsState(g2); if (!getPlotWrapper().okToDraw(this)) { return; } WayPoint leftWayPoint = getLeft(); WayPoint rightWayPoint = getRight(); g2.setStroke(new BasicStroke()); int x1 = leftWayPoint.getXFromValue(dataArea, domainAxis); int x2 = rightWayPoint.getXFromValue(dataArea, domainAxis); int top = (int) (dataArea.getY()); int bottom = (int) (dataArea.getY() + dataArea.getHeight()); FontMetrics fm = g2.getFontMetrics(); int width = fm.stringWidth(getName()); int height = fm.getAscent() + fm.getDescent(); if (getSelected()) { g2.setColor(Color.red); } else { g2.setColor(Color.black); } // int y = bottom-3; y = top - 2; int textLeft = x1 + (x2 - x1) / 2 - width / 2; g2.drawString(getName(), textLeft, y); g2.setStroke(new BasicStroke(2.0f)); g2.drawLine(x1, top + 1, x2, top + 1); g2.setStroke(new BasicStroke(1.0f)); g2.setColor(Color.gray); g2.drawLine(x1, top, x1, bottom - WayPoint.ANNOTATION_WIDTH); g2.drawLine(x2, top, x2, bottom - WayPoint.ANNOTATION_WIDTH); }
From source file:Main.java
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) { return new JPanel() { public void paintComponent(Graphics g) { super.paintComponent(g); Font font = (Font) value; String text = font.getFamily(); FontMetrics fm = g.getFontMetrics(font); g.setColor(isSelected ? list.getSelectionBackground() : list.getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(isSelected ? list.getSelectionForeground() : list.getForeground()); g.setFont(font);/*from w w w . j a v a 2 s. co m*/ g.drawString(text, 0, fm.getAscent()); } public Dimension getPreferredSize() { Font font = (Font) value; String text = font.getFamily(); Graphics g = getGraphics(); FontMetrics fm = g.getFontMetrics(font); return new Dimension(fm.stringWidth(text), fm.getHeight()); } }; }
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:main.MapKit.java
@Override public void paintWaypoint(Graphics2D g, JXMapViewer viewer, MyWaypoint w) { g = (Graphics2D) g.create();//from ww w .j ava2 s .com Point2D point = viewer.getTileFactory().geoToPixel(w.getPosition(), viewer.getZoom()); int x = (int) point.getX(); int y = (int) point.getY(); if (w.amount == -1) { //means it's an original data point int fontSize = 28 - viewer.getZoom() * 2; //font size is larger when zoom in if (fontSize < 6) fontSize = 6; g.setFont(new Font("Arial", Font.PLAIN, fontSize)); g.setColor(w.color); g.drawString("x", x - fontSize / 2, y + fontSize / 2); g.dispose(); return; } if (origImage == null) { return; } BufferedImage myImg = map.get(w.color); if (myImg == null) { myImg = convert(origImage, w.color); map.put(w.color, myImg); } g.drawImage(myImg, x - myImg.getWidth() / 2, y - myImg.getHeight(), null); String label = String.valueOf(w.amount); // g.setFont(font); FontMetrics metrics = g.getFontMetrics(); int tw = metrics.stringWidth(label); int th = 1 + metrics.getAscent(); // g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawString(label, x - tw / 2, y + th); g.dispose(); }
From source file:AppletMenuBarDemo.java
/** * Measure the menu labels, and figure out their positions, so we can * determine when a click happens, and so we can redraw efficiently. *///from w w w. j a v a2 s. c o m protected void measure() { // Get information about the font FontMetrics fm = this.getFontMetrics(getFont()); // Remember the basic font size ascent = fm.getAscent(); descent = fm.getDescent(); // Create arrays to hold the measurements and positions int nummenus = labels.size(); widths = new int[nummenus]; startPositions = new int[nummenus]; // Measure the label strings and // figure out the starting position of each label int pos = margins.left; for (int i = 0; i < nummenus; i++) { startPositions[i] = pos; String label = (String) labels.elementAt(i); widths[i] = fm.stringWidth(label); pos += widths[i] + spacing; } // Compute our preferred size from this data prefsize.width = pos - spacing + margins.right; prefsize.height = ascent + descent + margins.top + margins.bottom; // We've don't need to be remeasured anymore. remeasure = false; }
From source file:uk.co.modularaudio.mads.base.oscilloscope.ui.OscilloscopeDisplayUiJComponent.java
private void drawScale(final Graphics g, final int height, final float maxMag, final int xOffset) { final FontMetrics fm = g.getFontMetrics(); // Do zero, and plus and minus maximum final int middle = height / 2; final int halfFontHeight = fm.getAscent() / 2; g.drawString("0.0", xOffset, middle + halfFontHeight); final int top = 0 + fm.getHeight(); // Only re-create the string when we need to if (previousPositiveMagStr == null || previousPositiveMag != maxMag) { previousPositiveMagStr = MathFormatter.fastFloatPrint(maxMag, 2, true); previousPositiveMag = maxMag;/* ww w .j a va 2 s .co m*/ } g.drawString(previousPositiveMagStr, xOffset, top); if (previousNegativeMagStr == null || previousNegativeMag != -maxMag) { previousNegativeMagStr = MathFormatter.fastFloatPrint(-maxMag, 2, true); previousNegativeMag = -maxMag; } final int bottom = height - halfFontHeight; g.drawString(previousNegativeMagStr, xOffset, bottom); }
From source file:FontPanel.java
public void paintComponent(Graphics g) { super.paintComponent(g); Font f = new Font("SansSerif", Font.BOLD, 14); Font fi = new Font("SansSerif", Font.BOLD + Font.ITALIC, 14); FontMetrics fm = g.getFontMetrics(f); FontMetrics fim = g.getFontMetrics(fi); String s1 = "Java "; String s2 = "Source and Support"; String s3 = " at www.java2s.com"; int width1 = fm.stringWidth(s1); int width2 = fim.stringWidth(s2); int width3 = fm.stringWidth(s3); Dimension d = getSize();/* w ww.j a v a2s. c o m*/ int cx = (d.width - width1 - width2 - width3) / 2; int cy = (d.height - fm.getHeight()) / 2 + fm.getAscent(); g.setFont(f); g.drawString(s1, cx, cy); cx += width1; g.setFont(fi); g.drawString(s2, cx, cy); cx += width2; g.setFont(f); g.drawString(s3, cx, cy); }
From source file:com.callidusrobotics.swing.SwingConsole.java
private void initConsole(final Font font, final int rows, final int cols) { frame = new JFrame(); icon = new ImageIcon(); label = new JLabel(icon); // Compute font metrics final DisplayMode displayMode = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDisplayMode();//ww w . ja v a 2 s. co m frame.setVisible(true); imageBuffer = frame.createImage(displayMode.getWidth(), displayMode.getHeight()); graphicsRenderer = imageBuffer.getGraphics(); graphicsRenderer.setFont(font); final FontMetrics fontMetrics = graphicsRenderer.getFontMetrics(font); charHeight = fontMetrics.getHeight(); charWidth = fontMetrics.getHeight(); charHOffset = 0; charVOffset = fontMetrics.getAscent(); // Compute console dimensions and initialize graphics renderer final int width = charWidth * cols; final int height = charHeight * rows; imageBuffer = frame.createImage(width, height); graphicsRenderer = imageBuffer.getGraphics(); graphicsRenderer.setFont(font); render(); label.setBackground(TrueColor.MAGENTA); label.setBounds(0, 0, width, height); label.setVisible(true); frame.setBounds(0, 0, width, height); frame.setBackground(TrueColor.MAGENTA); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(label); frame.setCursor(null); frame.pack(); frame.setLocationRelativeTo(null); frame.setResizable(false); frame.addKeyListener(this); }
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:krasa.cpu.CpuUsagePanel.java
@Override public void paintComponent(final Graphics g) { final boolean pressed = getModel().isPressed(); final boolean stateChanged = myWasPressed != pressed; myWasPressed = pressed;// w w w. j a va 2 s .c o m Image bufferedImage = myBufferedImage; if (bufferedImage == null || stateChanged) { final Dimension size = getSize(); final Insets insets = getInsets(); bufferedImage = UIUtil.createImage(g, size.width, size.height, BufferedImage.TYPE_INT_ARGB); final Graphics2D g2 = (Graphics2D) bufferedImage.getGraphics().create(); final int max = 100; int system = CpuUsageManager.system; int process = CpuUsageManager.process; final int otherProcesses = system - process; final int totalBarLength = size.width - insets.left - insets.right - 3; final int processUsageBarLength = totalBarLength * process / max; final int otherProcessesUsageBarLength = totalBarLength * otherProcesses / max; final int barHeight = Math.max(size.height, getFont().getSize() + 2); final int yOffset = (size.height - barHeight) / 2; final int xOffset = insets.left; // background g2.setColor(UIUtil.getPanelBackground()); g2.fillRect(0, 0, size.width, size.height); // gauge (ide) g2.setColor(ideColor); g2.fillRect(xOffset + 1, yOffset, processUsageBarLength + 1, barHeight); // gauge (system) g2.setColor(systemColor); g2.fillRect(xOffset + processUsageBarLength + 1, yOffset, otherProcessesUsageBarLength + 1, barHeight); // label g2.setFont(getFont()); // final String info = CpuUsageBundle.message("cpu.usage.panel.message.text", CpuUsageManager.process, // CpuUsageManager.system); final String info = fixedLengthString(String.valueOf(process), 3) + "% / " + fixedLengthString(String.valueOf(system), 3) + "%"; final FontMetrics fontMetrics = g.getFontMetrics(); final int infoWidth = fontMetrics.charsWidth(info.toCharArray(), 0, info.length()); final int infoHeight = fontMetrics.getAscent(); UISettings.setupAntialiasing(g2); final Color fg = pressed ? UIUtil.getLabelDisabledForeground() : JBColor.foreground(); g2.setColor(fg); g2.drawString(info, xOffset + (totalBarLength - infoWidth) / 2, yOffset + infoHeight + (barHeight - infoHeight) / 2 - 1); // border g2.setStroke(new BasicStroke(1)); g2.setColor(JBColor.GRAY); g2.drawRect(0, 0, size.width - 2, size.height - 1); g2.dispose(); myBufferedImage = bufferedImage; } draw(g, bufferedImage); }