Search in sources :

Example 11 with FontMetrics

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

the class CButton method paint.

/**
     * re paints this
     */
@Override
public void paint(Graphics g) {
    //super.paint(g);
    //selected color
    Color backgroundColor;
    if (this.pressed || this.selected) {
        backgroundColor = this.selectedColor;
    } else {
        backgroundColor = this.buttonColor;
    }
    // Set up graphics and buffer
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    BufferedImage buffer = GraphicsManager.gc.createCompatibleImage(this.getWidth(), this.getHeight(), Transparency.TRANSLUCENT);
    Graphics2D gb = buffer.createGraphics();
    gb.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // Set up first layer
    int buttonHeight = this.getHeight() - (INSET * 2);
    int buttonWidth = this.getWidth() - (INSET * 2);
    int arc = buttonHeight;
    Color topColoring = backgroundColor.darker();
    Color bottomColoring = backgroundColor.darker();
    gb.setPaint(new GradientPaint(0, INSET, topColoring, 0, buttonHeight, bottomColoring, false));
    // Paint the first layer
    gb.fillRoundRect(INSET, INSET, buttonWidth, buttonHeight, arc, arc);
    gb.setColor(Color.darkGray);
    gb.drawRoundRect(INSET, INSET, buttonWidth, buttonHeight, arc, arc);
    // set up paint data fields for second layer
    int highlightHeight = buttonHeight - (HIGHLIGHT_INSET * 2);
    int highlightWidth = buttonWidth - (HIGHLIGHT_INSET * 2);
    int highlightArc = highlightHeight;
    topColoring = backgroundColor.brighter().brighter().brighter().brighter();
    bottomColoring = backgroundColor.brighter().brighter().brighter().brighter();
    // Paint the second layer
    gb.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .8f));
    gb.setPaint(new GradientPaint(0, INSET + HIGHLIGHT_INSET, topColoring, 0, INSET + HIGHLIGHT_INSET + (highlightHeight / 2), backgroundColor.brighter(), false));
    gb.setClip(new RoundRectangle2D.Float(INSET + HIGHLIGHT_INSET, INSET + HIGHLIGHT_INSET, highlightWidth, highlightHeight / 2, highlightHeight / 3, highlightHeight / 3));
    gb.fillRoundRect(INSET + HIGHLIGHT_INSET, INSET + HIGHLIGHT_INSET, highlightWidth, highlightHeight, highlightArc, highlightArc);
    // Blur
    ConvolveOp blurOp = new ConvolveOp(new Kernel(3, 3, BLUR));
    BufferedImage blurredImage = blurOp.filter(buffer, null);
    // Draw button
    g2.drawImage(blurredImage, 0, 0, null);
    // Draw the text (if any)
    if (this.getText() != null) {
        if (this.focus) {
            g2.setColor(hoveredColor);
        } else {
            g2.setColor(foregroundColor);
        }
        Font font = g2.getFont().deriveFont((float) (((float) buttonHeight) * .6));
        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 : Color(java.awt.Color) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Rectangle2D(java.awt.geom.Rectangle2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D) GradientPaint(java.awt.GradientPaint) BufferedImage(java.awt.image.BufferedImage) GradientPaint(java.awt.GradientPaint) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) FontMetrics(java.awt.FontMetrics) ConvolveOp(java.awt.image.ConvolveOp) Kernel(java.awt.image.Kernel)

Example 12 with FontMetrics

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

the class CGraphiteButton 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);
    int arc = buttonHeight / 3;
    Color topColoring;
    Color bottomColoring;
    if (this.pressed || this.selected) {
        if (this.focus) {
            topColoring = this.selectedColor.darker().darker().darker();
            bottomColoring = CGraphite.blue;
        } else {
            topColoring = CGraphite.blue.darker().darker().darker();
            bottomColoring = CGraphite.blue;
        }
    } else {
        if (this.focus) {
            topColoring = this.buttonColor;
            bottomColoring = Color.darkGray;
        } 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) {
        if (this.focus) {
            topColoring = Color.white;
            bottomColoring = this.selectedColor;
        } else {
            topColoring = Color.white;
            bottomColoring = this.selectedColor;
        }
    } else {
        if (this.focus) {
            topColoring = Color.white;
            bottomColoring = Color.darkGray;
        } else {
            topColoring = Color.gray;
            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) {
        if (this.focus) {
            g2.setColor(Color.white);
        } else {
            g2.setColor(Color.white);
        }
        Font font = g2.getFont().deriveFont((float) (((float) buttonHeight) * .4));
        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)

Example 13 with FontMetrics

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

the class CGraphiteSquareButton method paint.

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // Set up first layer
    int buttonHeight = this.getHeight();
    int buttonWidth = this.getWidth();
    Color topColoring;
    Color bottomColoring;
    if (this.pressed || this.selected) {
        if (this.focus) {
            topColoring = this.selectedColor.darker().darker().darker();
            bottomColoring = this.selectedColor;
        } else {
            topColoring = this.selectedColor.darker().darker().darker();
            bottomColoring = this.selectedColor;
        }
    } else {
        if (this.focus) {
            topColoring = this.buttonColor;
            bottomColoring = Color.darkGray;
        } else {
            topColoring = this.buttonColor;
            bottomColoring = this.buttonColor;
        }
    }
    g2.setPaint(new GradientPaint(0, 0, topColoring, 0, buttonHeight, bottomColoring, false));
    g2.fillRect(0, 0, buttonWidth, buttonHeight);
    // set up paint data fields for second layer
    int highlightHeight = buttonHeight / 2 - HIGHLIGHT_INSET;
    int highlightWidth = buttonWidth;
    if (this.pressed || this.selected) {
        if (this.focus) {
            topColoring = Color.white;
            bottomColoring = this.selectedColor;
        } else {
            topColoring = Color.white;
            bottomColoring = this.selectedColor;
        }
    } else {
        if (this.focus) {
            topColoring = Color.white;
            bottomColoring = Color.darkGray;
        } else {
            topColoring = Color.gray;
            bottomColoring = Color.darkGray;
        }
    }
    g2.setPaint(new GradientPaint(0, 0, topColoring, 0, buttonHeight, bottomColoring, false));
    g2.fillRect(0, 0, highlightWidth, highlightHeight);
    //set border
    g2.setColor(Color.DARK_GRAY);
    g2.setStroke(new BasicStroke(2));
    g2.drawRect(0, 0, buttonWidth - 1, buttonHeight - 1);
    // Draw the text (if any)
    if (this.getText() != null) {
        g2.setColor(Color.white);
        Font font = g2.getFont().deriveFont((float) (((float) buttonHeight) * .3));
        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 : BasicStroke(java.awt.BasicStroke) 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)

Example 14 with FontMetrics

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

the class CMenuItem method paint.

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    if (focus) {
        g2.setColor(background);
    } else {
        g.setColor(highlight);
    }
    g2.fillRect(0, 0, this.getWidth(), this.getHeight());
    String text = this.getText();
    if (text != null) {
        Font font = g2.getFont().deriveFont((float) (((float) this.getHeight()) * .8));
        g2.setFont(font);
        FontMetrics metrics = g2.getFontMetrics();
        Rectangle2D textBounds = metrics.getStringBounds(this.getText(), g2);
        double textHeight = textBounds.getHeight();
        double textWidth = textBounds.getWidth() > this.getWidth() ? this.getWidth() / 2 : textBounds.getWidth();
        float y = (float) ((this.getHeight() / 2) + (textHeight / 2)) - metrics.getDescent();
        float x;
        if (textPosition == Position.LEFT) {
            x = 10;
        } else {
            x = (float) ((this.getWidth() / 2) - (textWidth / 2));
        }
        g2.setColor(Color.black);
        g2.drawString(text, x, y);
    }
}
Also used : FontMetrics(java.awt.FontMetrics) Rectangle2D(java.awt.geom.Rectangle2D) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 15 with FontMetrics

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

the class CRadioactiveButton method paint.

@Override
public void paint(Graphics g) {
    // Set up graphics and buffer
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    BufferedImage buffer = GraphicsManager.gc.createCompatibleImage(this.getWidth(), this.getHeight(), Transparency.TRANSLUCENT);
    Graphics2D gb = buffer.createGraphics();
    gb.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // Set up first layer
    int buttonHeight = this.getHeight() - INSET * 2;
    int buttonWidth = this.getWidth() - INSET * 2;
    int arc = buttonHeight;
    if (this.pressed || this.selected) {
        g2.setPaint(new GradientPaint(0, -buttonHeight, Color.darkGray, 0, buttonHeight, buttonColor, false));
        g2.fillRoundRect(INSET, INSET, buttonWidth, buttonHeight, arc, arc);
        g2.setColor(Color.darkGray);
        g2.drawRoundRect(INSET, INSET, buttonWidth, buttonHeight, arc, arc);
    } else {
        //paint highlightlayer
        if (this.focus) {
            gb.setColor(Color.yellow);
            gb.setStroke(new BasicStroke(3));
            gb.drawRoundRect(INSET, INSET, buttonWidth, buttonHeight, arc, arc);
            gb.setStroke(new BasicStroke(1));
        }
        // Paint the first layer
        gb.setColor(buttonColor.darker());
        gb.fillRoundRect(INSET, INSET, buttonWidth, buttonHeight, arc, arc);
        gb.setColor(Color.darkGray);
        gb.drawRoundRect(INSET, INSET, buttonWidth, buttonHeight, arc, arc);
        // set up paint data fields for second layer
        int highlightHeight = buttonHeight * 2 / 3;
        int highlightWidth = buttonWidth;
        int highlightArc = highlightHeight;
        // Paint the second layer
        gb.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .8f));
        gb.setColor(buttonColor);
        gb.setClip(new RoundRectangle2D.Float(INSET, INSET, highlightWidth, highlightHeight, highlightArc, highlightArc));
        gb.fillRoundRect(INSET, INSET, buttonWidth, buttonHeight, arc, arc);
        // Blur
        ConvolveOp blurOp = new ConvolveOp(new Kernel(3, 3, BLUR));
        BufferedImage blurredImage = blurOp.filter(buffer, null);
        // Draw button
        g2.drawImage(blurredImage, 1, 1, null);
    }
    // Draw the text (if any)
    String text = this.getText();
    if (text != null && buttonHeight > 4) {
        //Font font = g2.getFont().deriveFont((float)(((float)this.getHeight()) * .6));
        Font font = g2.getFont().deriveFont((float) (this.getHeight() - INSET * 2 - 2) * .7f);
        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();
        g.setColor(Color.black);
        for (int i = 0; i < shadowPositionArray.length; i++) {
            int dx = shadowPositionArray[i][0];
            int dy = shadowPositionArray[i][1];
            g2.setColor(new Color(0, 0, 0, shadowColorArray[i]));
            g2.drawString(text, x + (int) ((dx) * offsetSize), y + (int) ((dy) * offsetSize));
        }
        g2.setColor(Color.white);
        g2.drawString(text, x, y);
    }
}
Also used : BasicStroke(java.awt.BasicStroke) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D) GradientPaint(java.awt.GradientPaint) BufferedImage(java.awt.image.BufferedImage) GradientPaint(java.awt.GradientPaint) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) FontMetrics(java.awt.FontMetrics) ConvolveOp(java.awt.image.ConvolveOp) Kernel(java.awt.image.Kernel)

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