Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a 3D border for a JDialog with rounded corners?

I could add a rounded corner border to my JDialog as in How to create a rounded title border in Java Swing. But it is still one color. I want to make the border looks like 3D.

Here is how I tried.

Graphics2D g2d = (Graphics2D) g;
        Color c1 = getBackground();
        Color c2 = color1.darker();
        int w = getWidth();
        int h = getHeight();

      GradientPaint gp = new GradientPaint(
                0, 0, c1,
                0, h, c2);

        g2d.setPaint(gp);
        g2d.fill3DRect(0,0, w, h,true);

Then, no 3D look, but the border has been widen more with its border color.
How can I achieve this?

Any sample code or links will be highly appreciated.

like image 241
Débora Avatar asked Dec 12 '22 22:12

Débora


1 Answers

3D Border

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.*;

public class ThreeDimensionalBorder extends AbstractBorder {

    private static final long serialVersionUID = 1L;
    private Color color;
    private int thickness = 8;
    private int radii = 8;
    private Insets insets = null;
    private BasicStroke stroke = null;
    private int strokePad;
    RenderingHints hints;
    int shadowPad = 3;

    ThreeDimensionalBorder(Color color) {
        this(color, 128, 8);
    }

    ThreeDimensionalBorder(Color color, int transparency, int shadowWidth) {
        this.color = color;
        shadowPad = shadowWidth;

        stroke = new BasicStroke(thickness);
        strokePad = thickness/2;

        hints = new RenderingHints(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        int pad = radii + strokePad;
        int bottomPad = pad + strokePad + shadowPad;
        int rightPad = pad + strokePad + shadowPad;
        insets = new Insets(pad,pad,bottomPad+shadowPad,rightPad);
    }

    @Override
    public Insets getBorderInsets(Component c) {
        return insets;
    }

    @Override
    public Insets getBorderInsets(Component c, Insets insets) {
        return getBorderInsets(c);
    }

    @Override
    public void paintBorder(
            Component c,
            Graphics g,
            int x, int y,
            int width, int height) {

        Graphics2D g2 = (Graphics2D)g;

        int bottomLineY = height-thickness-shadowPad;

        RoundRectangle2D.Double bubble = new RoundRectangle2D.Double(
                0+strokePad,
                0+strokePad,
                width-thickness-shadowPad,
                bottomLineY,
                radii,
                radii
                );

        Area area = new Area(bubble);

        g2.setRenderingHints(hints);

        g2.setColor(color);
        g2.setStroke(stroke);
        g2.draw(area);

        Area shadowArea = new Area(new Rectangle(0,0,width,height));
        shadowArea.subtract(area);
        g.setClip(shadowArea);
        Color shadow = new Color(color.getRed(),color.getGreen(),color.getBlue(),128);
        g2.setColor(shadow);
        g2.translate(shadowPad,shadowPad);
        g2.draw(area);
        AffineTransform at = g2.getTransform();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel p = new JPanel();
                String t = "The quick brown fox jumps over the lazy dog!";

                JLabel l1 = new JLabel(t);
                l1.setBorder(new ThreeDimensionalBorder(Color.MAGENTA.darker(),128,4));
                p.add(l1);

                JLabel l2 = new JLabel(t);
                l2.setBorder(new ThreeDimensionalBorder(Color.BLACK,200,5));
                p.add(l2);

                JLabel l3 = new JLabel(t);
                l3.setBorder(new ThreeDimensionalBorder(Color.BLUE,40,6));
                p.add(l3);

                JOptionPane.showMessageDialog(null, p);
            }
        });
    }
}
like image 180
Andrew Thompson Avatar answered Dec 27 '22 03:12

Andrew Thompson