1. #1

    Registriert seit
    19.11.2011
    Beiträge
    496
    Thanked 412 Times in 268 Posts

    Standard Java - RoundedTitleBorder

    Hallo,

    die Border aus dem SwingSet3 fand ich schön und habe es deshalb nachgebastelt.

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Font;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Insets;
    import java.awt.RenderingHints;

    import javax.swing.border.AbstractBorder;

    public class RoundedTitleBorder extends AbstractBorder {
    public static final int LEFT = 0;
    public static final int CENTER = 1;
    public static final int RIGHT = 2;

    private String title;
    private Font font;
    private int cornerRadius;
    private Color textColor;
    private Color gradient1;
    private Color gradient2;
    private boolean gradient;
    private int pos;

    public RoundedTitleBorder(String title, Font font) {
    this(title, font, 15,
    new Color(47, 76, 102),
    new Color(162, 187, 207),
    new Color(199, 202, 208),
    true, LEFT);
    }

    public RoundedTitleBorder(String title, Font font, int pos) {
    this(title, font, 15,
    new Color(47, 76, 102),
    new Color(162, 187, 207),
    new Color(199, 202, 208),
    true, pos);
    }

    public RoundedTitleBorder(String title, Font font, int cornerRadius,
    Color textColor, Color gradient1, Color gradient2, boolean gradient,
    int pos) {
    this.title = title;
    this.font = font;
    this.cornerRadius = cornerRadius;
    this.textColor = textColor;
    this.gradient1 = gradient1;
    this.gradient2 = gradient2;
    this.gradient = gradient;
    this.pos = pos;
    }

    public Insets getBorderInsets(Component c) {
    return getBorderInsets(c, new Insets(0, 0, 0, 0));
    }

    public Insets getBorderInsets(Component c, Insets insets) {
    insets.top = c.getFontMetrics(font).getHeight() + 10;
    insets.bottom = cornerRadius > 1 ? (cornerRadius / 2) : 1;
    insets.left = insets.right = 1;
    return insets;
    }

    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);

    int fontHeight = c.getFontMetrics(font).getHeight();
    paintGradient(g2, width, fontHeight + 10);
    paintTitle(g2, width, c.getFontMetrics(font).stringWidth(title), fontHeight);
    Color color = deriveColorHSB(c.getBackground(), 0, 0, -.3f);
    g2.setColor(deriveColorAlpha(color, 40));
    g2.drawRoundRect(x, y + 2, width - 1, height - 3, cornerRadius, cornerRadius);
    g2.setColor(deriveColorAlpha(color, 90));
    g2.drawRoundRect(x, y + 1, width - 1, height - 2, cornerRadius, cornerRadius);
    g2.setColor(deriveColorAlpha(color, 255));
    g2.drawRoundRect(x, y, width - 1, height - 1, cornerRadius, cornerRadius);
    g2.dispose();
    }

    private void paintGradient(Graphics2D g2, int width, int height) {
    if(gradient) {
    GradientPaint gp = new GradientPaint(0, 0, gradient1,
    width, height, gradient2);
    g2.setPaint(gp);
    g2.fillRoundRect(0, 0, width, height, cornerRadius, cornerRadius);
    g2.fillRect(0, height - cornerRadius / 2, width, cornerRadius / 2);
    } else {
    g2.setColor(gradient1);
    g2.fillRoundRect(0, 0, width, height, cornerRadius, cornerRadius);
    }
    }

    private void paintTitle(Graphics2D g2, int width, int fontWidth, int fontHeight) {
    int textX = 0;
    switch(pos) {
    case LEFT:
    textX = 10;
    break;
    case CENTER:
    textX = width / 2 - fontWidth / 2;
    break;
    case RIGHT:
    textX = width - fontWidth - 10;
    break;
    }
    g2.setFont(font);
    g2.setColor(textColor);
    g2.drawString(title, textX, fontHeight);
    }

    // Aus SwingSet3 Utilities.java
    private Color deriveColorAlpha(Color base, int alpha) {
    return new Color(base.getRed(), base.getGreen(), base.getBlue(), alpha);
    }

    // Aus SwingSet3 Utilities.java
    private Color deriveColorHSB(Color base, float dH, float dS, float dB) {
    float hsb[] = Color.RGBtoHSB(
    base.getRed(), base.getGreen(), base.getBlue(), null);
    hsb[0] += dH;
    hsb[1] += dS;
    hsb[2] += dB;
    return Color.getHSBColor(
    hsb[0] < 0 ? 0 : (hsb[0] > 1 ? 1 : hsb[0]),
    hsb[1] < 0 ? 0 : (hsb[1] > 1 ? 1 : hsb[1]),
    hsb[2] < 0 ? 0 : (hsb[2] > 1 ? 1 : hsb[2]));
    }
    }


    Beispiel (Nimbus LnF):
    Code:
    JPanel test = new JPanel(new BorderLayout());
    JLabel lbl = new JLabel("content area");
    lbl.setHorizontalAlignment(JLabel.CENTER);
    test.add(BorderLayout.CENTER, lbl);
    test.setBorder(new RoundedTitleBorder("JPanel Border Demo", new Font("SansSerif", Font.BOLD, 18)));
    Name:  unbenanntnhxno.png
Hits: 245
Größe:  20,6 KB


    Verlauf und Farben können geändert werden. Siehe Konstruktoren.


    EDIT: Habe Getter und Setter vergessen. Wer aber schon soweit ist und die Klasse benutzen kann, kann sie nachträglich hinzufügen.
    Geändert von Mr. White (14.12.2011 um 16:32 Uhr)

  2. The Following 2 Users Say Thank You to Mr. White For This Useful Post:

    Mentos (13.12.2011)

  3. #2

    Registriert seit
    28.10.2011
    Beiträge
    630
    Thanked 283 Times in 171 Posts

    Standard AW: Java - RoundedTitleBorder

    very nice

    Hast du noch mehr derartige Layouts & Co?
    Man kann immer ein wenig inspiration gebrauchen.

  4. #3

    Registriert seit
    19.11.2011
    Beiträge
    496
    Thanked 412 Times in 268 Posts

    Standard AW: Java - RoundedTitleBorder

    Zitat Zitat von Bizzi Beitrag anzeigen
    very nice
    Danke.

    Zitat Zitat von Bizzi Beitrag anzeigen
    Hast du noch mehr derartige Layouts & Co?
    Man kann immer ein wenig inspiration gebrauchen.
    Nein, ich mache normalerweise selten etwas mit GUIs. Ausnahmen gibt es natürlich und ich wollte mal etwas Anderes ausprobieren, deshalb schrieb ich diese Borderklasse. Ansonsten reicht mir das, was Swing schon mitbringt, völlig aus.

Ähnliche Themen

  1. Antworten: 8
    Letzter Beitrag: 02.12.2013, 23:41
  2. Java EE 32 Bit?
    Von Pwned im Forum Java
    Antworten: 2
    Letzter Beitrag: 04.08.2012, 23:08
  3. Ubuntu Sun Java
    Von hYpercrites im Forum Linux
    Antworten: 3
    Letzter Beitrag: 20.06.2012, 15:17
  4. [Java] Timer
    Von rVs14 im Forum Java
    Antworten: 1
    Letzter Beitrag: 16.06.2012, 12:13
  5. [Java] ImageCache
    Von uncopyable im Forum Sourcecode
    Antworten: 1
    Letzter Beitrag: 07.06.2012, 00:49
Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.