Search in sources :

Example 56 with FontMetrics

use of java.awt.FontMetrics in project JMRI by JMRI.

the class SpeedoDial method paint.

@Override
public void paint(Graphics g) {
    super.paint(g);
    if (!(g instanceof Graphics2D)) {
        throw new IllegalArgumentException("Graphics object passed is not the correct type");
    }
    Graphics2D g2 = (Graphics2D) g;
    // overridden Paint method to draw the speedo dial
    g2.translate(centreX, centreY);
    // Draw the dial outline scaled to the panel size with a dot in the middle and
    // center ring for the pointer
    g2.setColor(Color.white);
    g2.fillOval(-faceSize / 2, -faceSize / 2, faceSize, faceSize);
    g2.setColor(Color.black);
    g2.drawOval(-faceSize / 2, -faceSize / 2, faceSize, faceSize);
    int dotSize = faceSize / 40;
    g2.fillOval(-dotSize * 2, -dotSize * 2, 4 * dotSize, 4 * dotSize);
    // Draw the JMRI logo
    g2.drawImage(scaledLogo, -logoWidth / 2, -faceSize / 4, logoWidth, logoHeight, this);
    // Currently selected units are plotted every 10 units with major and minor
    // tick marks around the outer edge of the dial
    // Other units are plotted in a differrent color, smaller font with dots
    // in an inner ring
    // Scaled font size for primary units
    int fontSize = faceSize / 10;
    if (fontSize < 1) {
        fontSize = 1;
    }
    Font sizedFont = new Font("Serif", Font.PLAIN, fontSize);
    g2.setFont(sizedFont);
    FontMetrics fontM = g2.getFontMetrics(sizedFont);
    // Draw the speed markers for the primary units
    int dashSize = size / 60;
    setTicks();
    // Add minor tick marks
    for (float i = 150; i < 391; i = i + priMinorTick) {
        g2.drawLine(dotX((float) faceSize / 2, i), dotY((float) faceSize / 2, i), dotX((float) faceSize / 2 - dashSize, i), dotY((float) faceSize / 2 - dashSize, i));
    }
    // Add major tick marks and digits
    int j = 0;
    for (float i = 150; i < 391; i = i + priMajorTick) {
        g2.drawLine(dotX((float) faceSize / 2, i), dotY((float) faceSize / 2, i), dotX((float) faceSize / 2 - 3 * dashSize, i), dotY((float) faceSize / 2 - 3 * dashSize, i));
        String speed = Integer.toString(10 * j);
        int xOffset = fontM.stringWidth(speed);
        int yOffset = fontM.getHeight();
        // offset by 210 degrees to start in lower left quadrant and work clockwise
        g2.drawString(speed, dotX((float) faceSize / 2 - 6 * dashSize, j * priMajorTick - 210) - xOffset / 2, dotY((float) faceSize / 2 - 6 * dashSize, j * priMajorTick - 210) + yOffset / 4);
        j++;
    }
    // Add dots and digits for secondary units
    // First make a smaller font
    fontSize = faceSize / 15;
    if (fontSize < 1) {
        fontSize = 1;
    }
    sizedFont = new Font("Serif", Font.PLAIN, fontSize);
    g2.setFont(sizedFont);
    fontM = g2.getFontMetrics(sizedFont);
    g2.setColor(Color.green);
    j = 0;
    for (float i = 150; i < 391; i = i + secTick) {
        g2.fillOval(dotX((float) faceSize / 2 - 10 * dashSize, i), dotY((float) faceSize / 2 - 10 * dashSize, i), 5, 5);
        if (((j & 1) == 0) || (units == Speed.KPH)) {
            // kph are plotted every 20 when secondary, mph every 10
            String speed = Integer.toString(10 * j);
            int xOffset = fontM.stringWidth(speed);
            int yOffset = fontM.getHeight();
            // offset by 210 degrees to start in lower left quadrant and work clockwise
            g2.drawString(speed, dotX((float) faceSize / 2 - 13 * dashSize, j * secTick - 210) - xOffset / 2, dotY((float) faceSize / 2 - 13 * dashSize, j * secTick - 210) + yOffset / 4);
        }
        j++;
    }
    // Draw secondary units string
    g2.drawString(secString, dotX((float) faceSize / 2 - 5 * dashSize, 45) - fontM.stringWidth(secString) / 2, dotY((float) faceSize / 2 - 5 * dashSize, 45) + fontM.getHeight() / 4);
    g2.setColor(Color.black);
    // Draw pointer rotated to appropriate angle
    // Calculation mimics the AffineTransform class calculations in Graphics2D
    // Graphics2D and AffineTransform not used to maintain compatabilty with Java 1.1.8
    double speedAngleRadians = Math.toRadians(speedAngle);
    for (int i = 0; i < scaledMinuteX.length; i++) {
        rotatedMinuteX[i] = (int) (scaledMinuteX[i] * Math.cos(speedAngleRadians) - scaledMinuteY[i] * Math.sin(speedAngleRadians));
        rotatedMinuteY[i] = (int) (scaledMinuteX[i] * Math.sin(speedAngleRadians) + scaledMinuteY[i] * Math.cos(speedAngleRadians));
    }
    scaledMinuteHand = new Polygon(rotatedMinuteX, rotatedMinuteY, rotatedMinuteX.length);
    g2.fillPolygon(scaledMinuteHand);
    // Draw primary units indicator in slightly smaller font than speed digits
    int unitsFontSize = (int) ((float) faceSize / 10 * .75);
    if (unitsFontSize < 1) {
        unitsFontSize = 1;
    }
    Font unitsSizedFont = new Font("Serif", Font.PLAIN, unitsFontSize);
    g2.setFont(unitsSizedFont);
    FontMetrics unitsFontM = g2.getFontMetrics(unitsSizedFont);
    //        g2.drawString(unitsString, -amPmFontM.stringWidth(unitsString)/2, faceSize/5 );
    g2.drawString(priString, dotX((float) faceSize / 2 - 5 * dashSize, -225) - unitsFontM.stringWidth(priString) / 2, dotY((float) faceSize / 2 - 5 * dashSize, -225) + unitsFontM.getHeight() / 4);
    // Show numeric speed
    String speedString = Integer.toString(speedDigits);
    int digitsFontSize = (int) (fontSize * 1.5);
    Font digitsSizedFont = new Font("Serif", Font.PLAIN, digitsFontSize);
    g2.setFont(digitsSizedFont);
    FontMetrics digitsFontM = g2.getFontMetrics(digitsSizedFont);
    // draw a box around the digital speed
    int pad = (int) (digitsFontSize * 0.2);
    int h = (int) (digitsFontM.getAscent() * 0.8);
    int w = digitsFontM.stringWidth("999");
    if (pad < 2) {
        pad = 2;
    }
    g2.setColor(Color.LIGHT_GRAY);
    g2.fillRect(-w / 2 - pad, 2 * faceSize / 5 - h - pad, w + pad * 2, h + pad * 2);
    g2.setColor(Color.DARK_GRAY);
    g2.drawRect(-w / 2 - pad, 2 * faceSize / 5 - h - pad, w + pad * 2, h + pad * 2);
    g2.setColor(Color.BLACK);
    g2.drawString(speedString, -digitsFontM.stringWidth(speedString) / 2, 2 * faceSize / 5);
}
Also used : FontMetrics(java.awt.FontMetrics) Polygon(java.awt.Polygon) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 57 with FontMetrics

use of java.awt.FontMetrics in project processdash by dtuma.

the class JHintTextField method paint.

@Override
public void paint(Graphics g) {
    super.paint(g);
    if (isHintVisible()) {
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        Color fg = getForeground();
        Color bg = getBackground();
        g.setColor(PaintUtils.mixColors(fg, bg, 0.5));
        g.setFont(italic);
        int h = getHeight();
        Insets ins = getInsets();
        FontMetrics fm = g.getFontMetrics();
        g.drawString(hint, ins.left, h / 2 + fm.getAscent() / 2 - 2);
    }
}
Also used : Insets(java.awt.Insets) FontMetrics(java.awt.FontMetrics) Color(java.awt.Color) Graphics2D(java.awt.Graphics2D)

Example 58 with FontMetrics

use of java.awt.FontMetrics in project JMRI by JMRI.

the class AnalogClock2Display method paint.

@Override
public void paint(Graphics g) {
    // overridden Paint method to draw the clock
    g.setColor(color.getValue());
    g.translate(centreX, centreY);
    // Draw the clock face
    g.drawImage(clockFace, -faceSize / 2, -faceSize / 2, faceSize, faceSize, this);
    // Draw the JMRI logo
    g.drawImage(scaledLogo, -logoWidth / 2, -faceSize / 4, logoWidth, logoHeight, this);
    // Draw hour hand rotated to appropriate angle
    // Calculation mimics the AffineTransform class calculations in Graphics2D
    // Grpahics2D and AffineTransform not used to maintain compatabilty with Java 1.1.8
    double minuteAngleRadians = Math.toRadians(minuteAngle);
    for (int i = 0; i < scaledMinuteX.length; i++) {
        rotatedMinuteX[i] = (int) (scaledMinuteX[i] * Math.cos(minuteAngleRadians) - scaledMinuteY[i] * Math.sin(minuteAngleRadians));
        rotatedMinuteY[i] = (int) (scaledMinuteX[i] * Math.sin(minuteAngleRadians) + scaledMinuteY[i] * Math.cos(minuteAngleRadians));
    }
    scaledMinuteHand = new Polygon(rotatedMinuteX, rotatedMinuteY, rotatedMinuteX.length);
    double hourAngleRadians = Math.toRadians(hourAngle);
    for (int i = 0; i < scaledHourX.length; i++) {
        rotatedHourX[i] = (int) (scaledHourX[i] * Math.cos(hourAngleRadians) - scaledHourY[i] * Math.sin(hourAngleRadians));
        rotatedHourY[i] = (int) (scaledHourX[i] * Math.sin(hourAngleRadians) + scaledHourY[i] * Math.cos(hourAngleRadians));
    }
    scaledHourHand = new Polygon(rotatedHourX, rotatedHourY, rotatedHourX.length);
    g.fillPolygon(scaledHourHand);
    g.fillPolygon(scaledMinuteHand);
    // Draw AM/PM indicator in slightly smaller font than hour digits
    int amPmFontSize = (int) (faceSize * .075);
    if (amPmFontSize < 1) {
        amPmFontSize = 1;
    }
    Font amPmSizedFont = new Font("Serif", Font.BOLD, amPmFontSize);
    g.setFont(amPmSizedFont);
    FontMetrics amPmFontM = g.getFontMetrics(amPmSizedFont);
    g.drawString(amPm, -amPmFontM.stringWidth(amPm) / 2, faceSize / 5);
}
Also used : FontMetrics(java.awt.FontMetrics) Polygon(java.awt.Polygon) Font(java.awt.Font)

Example 59 with FontMetrics

use of java.awt.FontMetrics in project processdash by dtuma.

the class StandardDiscItemRenderer method drawDiscLabel.

protected void drawDiscLabel(Graphics2D g2, Ellipse2D shape, Paint labelPaint, Font labelFont, String label) {
    g2.setFont(labelFont);
    g2.setPaint(labelPaint);
    FontMetrics m = g2.getFontMetrics();
    int height = m.getAscent();
    double halfHeight = height / 2.0;
    double radius = shape.getWidth() / 2;
    double availableRadius = radius - getLabelPadding();
    double halfWidth = Math.sqrt(availableRadius * availableRadius - halfHeight * halfHeight);
    int width = (int) Math.floor(halfWidth * 2 + 0.99);
    Rectangle viewR = new Rectangle(width, height);
    Rectangle iconR = new Rectangle();
    Rectangle textR = new Rectangle();
    String text = SwingUtilities.layoutCompoundLabel(m, label, null, SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.TRAILING, viewR, iconR, textR, 0);
    if (text.equals(label) || text.length() >= 3 + minLabelChars) {
        double x = shape.getCenterX() - halfWidth + textR.x;
        double y = shape.getCenterY() + halfHeight + textR.y;
        g2.drawString(text, (float) x, (float) y);
    }
}
Also used : FontMetrics(java.awt.FontMetrics) Rectangle(java.awt.Rectangle) Paint(java.awt.Paint)

Example 60 with FontMetrics

use of java.awt.FontMetrics in project jo-client-platform by jo-source.

the class NodeRenderer method render.

@Override
public void render(final Graphics2D g, final VisualItem item) {
    final RectangularShape shape = (RectangularShape) getShape(item);
    gradientColor = new GradientPaint(shape.getBounds().x + shape.getBounds().width / 2, shape.getBounds().y, lightenFillColor(item.getFillColor()), shape.getBounds().x + shape.getBounds().width / 2, shape.getBounds().y + shape.getBounds().height / 2, ColorLib.getColor(item.getFillColor()), false);
    g.setPaint(gradientColor);
    g.fill(shape);
    if ((Boolean) item.get("marked")) {
        g.setStroke(new BasicStroke(2));
        g.setPaint(Color.DARK_GRAY);
        g.draw(shape);
    }
    final String text = m_text;
    final Image img = getImage(item);
    if (text == null && img == null) {
        return;
    }
    final double size = item.getSize();
    final boolean useInt = 1.5 > Math.max(g.getTransform().getScaleX(), g.getTransform().getScaleY());
    double x = shape.getMinX() + size * m_horizBorder;
    double y = shape.getMinY() + size * m_vertBorder;
    // render image
    renderImage(g, useInt, img, size, x, y, shape);
    if (img != null) {
        final double w = size * img.getWidth(null);
        final double h = size * img.getHeight(null);
        double ix = x;
        double iy = y;
        // determine one co-ordinate based on the image position
        switch(m_imagePos) {
            case Constants.LEFT:
                x += w + size * m_imageMargin;
                break;
            case Constants.RIGHT:
                ix = shape.getMaxX() - size * m_horizBorder - w;
                break;
            case Constants.TOP:
                y += h + size * m_imageMargin;
                break;
            case Constants.BOTTOM:
                iy = shape.getMaxY() - size * m_vertBorder - h;
                break;
            default:
                throw new IllegalStateException("Unrecognized image alignment setting.");
        }
        // determine the other coordinate based on image alignment
        switch(m_imagePos) {
            case Constants.LEFT:
            case Constants.RIGHT:
                // need to set image y-coordinate
                switch(m_vImageAlign) {
                    case Constants.TOP:
                        break;
                    case Constants.BOTTOM:
                        iy = shape.getMaxY() - size * m_vertBorder - h;
                        break;
                    case Constants.CENTER:
                        iy = shape.getCenterY() - h / 2;
                        break;
                    default:
                        break;
                }
                break;
            case Constants.TOP:
            case Constants.BOTTOM:
                // need to set image x-coordinate
                switch(m_hImageAlign) {
                    case Constants.LEFT:
                        break;
                    case Constants.RIGHT:
                        ix = shape.getMaxX() - size * m_horizBorder - w;
                        break;
                    case Constants.CENTER:
                        ix = shape.getCenterX() - w / 2;
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
        if (useInt && size == 1.0) {
            // if possible, use integer precision
            // results in faster, flicker-free image rendering
            g.drawImage(img, (int) ix, (int) iy, null);
        } else {
            transform.setTransform(size, 0, 0, size, ix, iy);
            g.drawImage(img, transform, null);
        }
    }
    // render text
    final int textColor = item.getTextColor();
    if (text != null && ColorLib.alpha(textColor) > 0) {
        g.setPaint(ColorLib.getColor(textColor));
        g.setFont(m_font);
        final FontMetrics fm = DEFAULT_GRAPHICS.getFontMetrics(m_font);
        // compute available width
        final double tw;
        switch(m_imagePos) {
            case Constants.TOP:
            case Constants.BOTTOM:
                tw = shape.getWidth() - 2 * size * m_horizBorder;
                break;
            default:
                tw = m_textDim.width;
        }
        // compute available height
        final double th;
        switch(m_imagePos) {
            case Constants.LEFT:
            case Constants.RIGHT:
                th = shape.getHeight() - 2 * size * m_vertBorder;
                break;
            default:
                th = m_textDim.height;
        }
        // compute starting y-coordinate
        y += fm.getAscent();
        switch(m_vTextAlign) {
            case Constants.TOP:
                break;
            case Constants.BOTTOM:
                y += th - m_textDim.height;
                break;
            case Constants.CENTER:
                y += (th - m_textDim.height) / 2;
                break;
            default:
                break;
        }
        // render each line of text
        // the line height
        final int lh = fm.getHeight();
        int start = 0;
        int end = text.indexOf(m_delim);
        for (; end >= 0; y += lh) {
            drawString(g, fm, text.substring(start, end), useInt, x, y, tw);
            start = end + 1;
            end = text.indexOf(m_delim, start);
        }
        drawString(g, fm, text.substring(start), useInt, x, y, tw);
    }
    // render (+) or (-) if isParent and expanded
    if ((Boolean) item.get("isParent") && item.get("expanded") == Expand.NOT) {
        renderImage(g, useInt, expandedIcon, size, shape.getX(), shape.getY(), shape);
    } else if ((Boolean) item.get("isParent") && item.get("expanded") == Expand.FULL) {
        renderImage(g, useInt, notExpandedIcon, size, shape.getX(), shape.getY(), shape);
    } else if ((Boolean) item.get("isParent") && item.get("expanded") == Expand.PARTIALLY) {
        renderImage(g, useInt, notExpandedIcon, size, shape.getX(), shape.getY(), shape);
        renderImage(g, useInt, expandedIcon, size, shape.getX() + 16, shape.getY(), shape);
    }
}
Also used : BasicStroke(java.awt.BasicStroke) FontMetrics(java.awt.FontMetrics) RectangularShape(java.awt.geom.RectangularShape) GradientPaint(java.awt.GradientPaint) Image(java.awt.Image) GradientPaint(java.awt.GradientPaint)

Aggregations

FontMetrics (java.awt.FontMetrics)179 Font (java.awt.Font)61 Graphics2D (java.awt.Graphics2D)35 Point (java.awt.Point)34 Dimension (java.awt.Dimension)29 Color (java.awt.Color)23 Rectangle (java.awt.Rectangle)21 Rectangle2D (java.awt.geom.Rectangle2D)21 Insets (java.awt.Insets)20 Graphics (java.awt.Graphics)16 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)14 GradientPaint (java.awt.GradientPaint)14 MPart (gov.sandia.n2a.eqset.MPart)13 NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)12 Paint (java.awt.Paint)10 JTree (javax.swing.JTree)10 BasicStroke (java.awt.BasicStroke)9 BufferedImage (java.awt.image.BufferedImage)9 PanelModel (gov.sandia.n2a.ui.eq.PanelModel)8 JLabel (javax.swing.JLabel)8