Search in sources :

Example 1 with PrintGraphics

use of java.awt.PrintGraphics in project pivot by apache.

the class LabelSkin method paint.

@Override
public void paint(Graphics2D graphics) {
    Label label = (Label) this.getComponent();
    int width = getWidth();
    int height = getHeight();
    // Draw the background
    if (backgroundColor != null) {
        graphics.setPaint(backgroundColor);
        graphics.fillRect(0, 0, width, height);
    }
    // Draw the text
    if (glyphVectors != null && glyphVectors.getLength() > 0) {
        graphics.setFont(font);
        if (label.isEnabled()) {
            graphics.setPaint(color);
        } else {
            graphics.setPaint(disabledColor);
        }
        FontRenderContext fontRenderContext = Platform.getFontRenderContext();
        LineMetrics lm = font.getLineMetrics("", fontRenderContext);
        float ascent = lm.getAscent();
        float lineHeight = lm.getHeight();
        float y = 0;
        switch(verticalAlignment) {
            case TOP:
                {
                    y = padding.top;
                    break;
                }
            case BOTTOM:
                {
                    y = height - (textHeight + padding.bottom);
                    break;
                }
            case CENTER:
                {
                    y = (height - textHeight) / 2;
                    break;
                }
            default:
                {
                    break;
                }
        }
        for (int i = 0, n = glyphVectors.getLength(); i < n; i++) {
            GlyphVector glyphVector = glyphVectors.get(i);
            Rectangle2D textBounds = glyphVector.getLogicalBounds();
            float lineWidth = (float) textBounds.getWidth();
            float x = 0;
            switch(horizontalAlignment) {
                case LEFT:
                    {
                        x = padding.left;
                        break;
                    }
                case RIGHT:
                    {
                        x = width - (lineWidth + padding.right);
                        break;
                    }
                case CENTER:
                    {
                        x = (width - lineWidth) / 2;
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            if (graphics instanceof PrintGraphics) {
                // Work-around for printing problem in applets
                String text = label.getText();
                if (text != null && text.length() > 0) {
                    graphics.drawString(text, x, y + ascent);
                }
            } else {
                graphics.drawGlyphVector(glyphVector, x, y + ascent);
            }
            // Draw the text decoration
            if (textDecoration != null) {
                graphics.setStroke(new BasicStroke());
                float offset = 0;
                switch(textDecoration) {
                    case UNDERLINE:
                        {
                            offset = y + ascent + 2;
                            break;
                        }
                    case STRIKETHROUGH:
                        {
                            offset = y + lineHeight / 2 + 1;
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                Line2D line = new Line2D.Float(x, offset, x + lineWidth, offset);
                graphics.draw(line);
            }
            y += textBounds.getHeight();
        }
    }
}
Also used : BasicStroke(java.awt.BasicStroke) GlyphVector(java.awt.font.GlyphVector) Label(org.apache.pivot.wtk.Label) Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext) LineMetrics(java.awt.font.LineMetrics) Line2D(java.awt.geom.Line2D) PrintGraphics(java.awt.PrintGraphics)

Aggregations

BasicStroke (java.awt.BasicStroke)1 PrintGraphics (java.awt.PrintGraphics)1 FontRenderContext (java.awt.font.FontRenderContext)1 GlyphVector (java.awt.font.GlyphVector)1 LineMetrics (java.awt.font.LineMetrics)1 Line2D (java.awt.geom.Line2D)1 Rectangle2D (java.awt.geom.Rectangle2D)1 Label (org.apache.pivot.wtk.Label)1