Search in sources :

Example 6 with FontMetrics

use of java.awt.FontMetrics in project sonarqube by SonarSource.

the class TextLineNumber method getOffsetY.

/*
   * Determine the Y offset for the current row
   */
private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics) throws BadLocationException {
    // Get the bounding rectangle of the row
    Rectangle r = component.modelToView(rowStartOffset);
    int lineHeight = fontMetrics.getHeight();
    int y = r.y + r.height;
    int descent = 0;
    if (// default font is being used
    r.height == lineHeight) {
        descent = fontMetrics.getDescent();
    } else // We need to check all the attributes for font changes
    {
        if (fonts == null)
            fonts = new HashMap<>();
        Element root = component.getDocument().getDefaultRootElement();
        int index = root.getElementIndex(rowStartOffset);
        Element line = root.getElement(index);
        for (int i = 0; i < line.getElementCount(); i++) {
            Element child = line.getElement(i);
            AttributeSet as = child.getAttributes();
            String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily);
            Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize);
            String key = fontFamily + fontSize;
            FontMetrics fm = fonts.get(key);
            if (fm == null) {
                Font font = new Font(fontFamily, Font.PLAIN, fontSize);
                fm = component.getFontMetrics(font);
                fonts.put(key, fm);
            }
            descent = Math.max(descent, fm.getDescent());
        }
    }
    return y - descent;
}
Also used : HashMap(java.util.HashMap) AttributeSet(javax.swing.text.AttributeSet) FontMetrics(java.awt.FontMetrics) Element(javax.swing.text.Element) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Font(java.awt.Font)

Example 7 with FontMetrics

use of java.awt.FontMetrics in project sonarqube by SonarSource.

the class TextLineNumber method paintComponent.

/**
   *  Draw the line numbers
   */
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // Determine the width of the space available to draw the line number
    FontMetrics fontMetrics = component.getFontMetrics(component.getFont());
    Insets insets = getInsets();
    int availableWidth = getSize().width - insets.left - insets.right;
    // Determine the rows to draw within the clipped bounds.
    Rectangle clip = g.getClipBounds();
    int rowStartOffset = component.viewToModel(new Point(0, clip.y));
    int endOffset = component.viewToModel(new Point(0, clip.y + clip.height));
    while (rowStartOffset <= endOffset) {
        try {
            if (isCurrentLine(rowStartOffset))
                g.setColor(getCurrentLineForeground());
            else
                g.setColor(getForeground());
            // Get the line number as a string and then determine the
            // "X" and "Y" offsets for drawing the string.
            String lineNumber = getTextLineNumber(rowStartOffset);
            int stringWidth = fontMetrics.stringWidth(lineNumber);
            int x = getOffsetX(availableWidth, stringWidth) + insets.left;
            int y = getOffsetY(rowStartOffset, fontMetrics);
            g.drawString(lineNumber, x, y);
            // Move to the next row
            rowStartOffset = Utilities.getRowEnd(component, rowStartOffset) + 1;
        } catch (Exception e) {
            break;
        }
    }
}
Also used : Insets(java.awt.Insets) FontMetrics(java.awt.FontMetrics) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

Example 8 with FontMetrics

use of java.awt.FontMetrics in project OpenNotebook by jaltekruse.

the class Graph method drawErrorMessage.

public void drawErrorMessage(Graphics g, int xSize, int ySize, int xPicOrigin, int yPicOrigin) {
    FontMetrics fm = g.getFontMetrics();
    int errorWidth = fm.stringWidth("error");
    g.setColor(Color.WHITE);
    g.fillRect((xPicOrigin + xSize / 2) - errorWidth / 2, (yPicOrigin + ySize / 2) - fm.getHeight() / 2, errorWidth + 4, fm.getHeight() + 4);
    g.setColor(Color.BLACK);
    g.drawRect((xPicOrigin + xSize / 2) - errorWidth / 2, (yPicOrigin + ySize / 2) - fm.getHeight() / 2, errorWidth + 4, fm.getHeight() + 4);
    g.setColor(Color.RED);
    g.drawString("error", (xPicOrigin + xSize / 2) - errorWidth / 2 + 2, (yPicOrigin + ySize / 2) + fm.getHeight() / 2);
}
Also used : FontMetrics(java.awt.FontMetrics) GridPoint(doc.GridPoint)

Example 9 with FontMetrics

use of java.awt.FontMetrics in project hudson-2.x by hudson.

the class DependencyGraph method doGraph.

/**
     * Experimental visualization of project dependencies.
     */
public void doGraph(StaplerRequest req, StaplerResponse rsp) throws IOException {
    // Require admin permission for now (avoid exposing project names with restricted permissions)
    Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
    try {
        // creates a dummy graphics just so that we can measure font metrics
        BufferedImage emptyImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = emptyImage.createGraphics();
        graphics.setFont(FONT);
        final FontMetrics fontMetrics = graphics.getFontMetrics();
        // TODO: timestamp check
        Layout<AbstractProject> layout = new Layout<AbstractProject>(new Navigator<AbstractProject>() {

            public Collection<AbstractProject> vertices() {
                // only include projects that have some dependency
                List<AbstractProject> r = new ArrayList<AbstractProject>();
                for (AbstractProject p : Hudson.getInstance().getAllItems(AbstractProject.class)) {
                    if (!getDownstream(p).isEmpty() || !getUpstream(p).isEmpty())
                        r.add(p);
                }
                return r;
            }

            public Collection<AbstractProject> edge(AbstractProject p) {
                return getDownstream(p);
            }

            public Dimension getSize(AbstractProject p) {
                int w = fontMetrics.stringWidth(p.getDisplayName()) + MARGIN * 2;
                return new Dimension(w, fontMetrics.getHeight() + MARGIN * 2);
            }
        }, Direction.LEFTRIGHT);
        Rectangle area = layout.calcDrawingArea();
        // give it a bit of margin
        area.grow(4, 4);
        BufferedImage image = new BufferedImage(area.width, area.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        g2.setTransform(AffineTransform.getTranslateInstance(-area.x, -area.y));
        g2.setPaint(Color.WHITE);
        g2.fill(area);
        g2.setFont(FONT);
        g2.setPaint(Color.BLACK);
        for (AbstractProject p : layout.vertices()) {
            final Point sp = center(layout.vertex(p));
            for (AbstractProject q : layout.edges(p)) {
                Point cur = sp;
                for (Point pt : layout.edge(p, q)) {
                    g2.drawLine(cur.x, cur.y, pt.x, pt.y);
                    cur = pt;
                }
                final Point ep = center(layout.vertex(q));
                g2.drawLine(cur.x, cur.y, ep.x, ep.y);
            }
        }
        int diff = fontMetrics.getAscent() + fontMetrics.getLeading() / 2;
        for (AbstractProject p : layout.vertices()) {
            Rectangle r = layout.vertex(p);
            g2.setPaint(Color.WHITE);
            g2.fillRect(r.x, r.y, r.width, r.height);
            g2.setPaint(Color.BLACK);
            g2.drawRect(r.x, r.y, r.width, r.height);
            g2.drawString(p.getDisplayName(), r.x + MARGIN, r.y + MARGIN + diff);
        }
        rsp.setContentType("image/png");
        ServletOutputStream os = rsp.getOutputStream();
        ImageIO.write(image, "PNG", os);
        os.close();
    } catch (HeadlessException e) {
        // not available. send out error message
        rsp.sendRedirect2(req.getContextPath() + "/images/headless.png");
    }
}
Also used : HeadlessException(java.awt.HeadlessException) ServletOutputStream(javax.servlet.ServletOutputStream) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D) Layout(org.kohsuke.graph_layouter.Layout) FontMetrics(java.awt.FontMetrics) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with FontMetrics

use of java.awt.FontMetrics in project openblocks by mikaelhg.

the class CBorderlessButton method paint.

/**
     * re paints this
     */
@Override
public void paint(Graphics g) {
    // Set up graphics and buffer
    //super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // Set up first layer
    int buttonHeight = this.getHeight() - (INSET * 2);
    int buttonWidth = this.getWidth() - (INSET * 2);
    if (this.focus) {
        int arc = buttonHeight / 3;
        Color topColoring;
        Color bottomColoring;
        if (this.pressed || this.selected) {
            topColoring = this.selectedColor.darker();
            bottomColoring = CGraphite.blue;
        } else {
            topColoring = this.buttonColor;
            bottomColoring = this.buttonColor;
        }
        // Paint the first layer
        g2.setPaint(new GradientPaint(0, 0, topColoring, 0, buttonHeight, bottomColoring, false));
        g2.fillRoundRect(INSET, INSET, buttonWidth, buttonHeight, arc, arc);
        g2.setColor(Color.darkGray);
        g2.drawRoundRect(INSET, INSET, buttonWidth, buttonHeight, arc, arc);
        // set up paint data fields for second layer
        int highlightHeight = buttonHeight / 2 - HIGHLIGHT_INSET;
        int highlightWidth = buttonWidth - (HIGHLIGHT_INSET * 2) + 1;
        if (this.pressed || this.selected) {
            topColoring = Color.white;
            bottomColoring = this.selectedColor;
        } else {
            topColoring = Color.white;
            bottomColoring = Color.darkGray;
        }
        // Paint the second layer
        g2.setPaint(new GradientPaint(0, 0, topColoring, 0, buttonHeight, bottomColoring, false));
        g2.fillRoundRect(INSET + HIGHLIGHT_INSET, INSET + HIGHLIGHT_INSET + 1, highlightWidth, highlightHeight, arc, arc);
    }
    // Draw the text (if any)
    if (this.getText() != null) {
        g2.setColor(Color.white);
        Font font = g2.getFont().deriveFont((float) (((float) buttonHeight) * .5));
        g2.setFont(font);
        FontMetrics metrics = g2.getFontMetrics();
        Rectangle2D textBounds = metrics.getStringBounds(this.getText(), g2);
        float x = (float) ((this.getWidth() / 2) - (textBounds.getWidth() / 2));
        float y = (float) ((this.getHeight() / 2) + (textBounds.getHeight() / 2)) - metrics.getDescent();
        g2.drawString(this.getText(), x, y);
    }
}
Also used : FontMetrics(java.awt.FontMetrics) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) GradientPaint(java.awt.GradientPaint) GradientPaint(java.awt.GradientPaint) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Aggregations

FontMetrics (java.awt.FontMetrics)116 Font (java.awt.Font)43 Graphics2D (java.awt.Graphics2D)32 Point (java.awt.Point)29 Dimension (java.awt.Dimension)21 Rectangle2D (java.awt.geom.Rectangle2D)20 Color (java.awt.Color)18 Insets (java.awt.Insets)18 Rectangle (java.awt.Rectangle)17 GradientPaint (java.awt.GradientPaint)9 BufferedImage (java.awt.image.BufferedImage)9 Graphics (java.awt.Graphics)8 BasicStroke (java.awt.BasicStroke)6 Shape (java.awt.Shape)5 JLabel (javax.swing.JLabel)5 Canvas (java.awt.Canvas)4 BadLocationException (javax.swing.text.BadLocationException)4 Polygon (java.awt.Polygon)3 FontRenderContext (java.awt.font.FontRenderContext)3 ArrayList (java.util.ArrayList)3