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

淺唱寂寞╮ 提交于 2019-12-21 17:41:01

问题


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.


回答1:


Would this suffice??

It's far from perfect, but the basic idea works...

public class MyRoundedBorder implements Border {

    protected static final Insets DEFAULT_INSETS = new Insets(4, 4, 4, 4);

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

        Graphics2D g2d = (Graphics2D) g.create();

        g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g2d.setColor(Color.WHITE);
        Shape corner = new RoundedShape(width - 8, height - 8);
        g2d.translate(x + 2, y + 2);
        g2d.draw(corner);
        g2d.transform(AffineTransform.getRotateInstance(Math.toRadians(180), (width - 8) / 2, (height - 8) / 2));
        g2d.setColor(Color.LIGHT_GRAY);
        g2d.draw(corner);

        g2d.dispose();

    }

    @Override
    public Insets getBorderInsets(Component c) {

        return DEFAULT_INSETS;

    }

    @Override
    public boolean isBorderOpaque() {

        return true;

    }

    public class RoundedShape extends Path2D.Float {

        public RoundedShape(int width, int height) {

            moveTo(0, height - 20);
            append(new Arc2D.Float(0, height - 20, 20, 20, 180, 45, Arc2D.CHORD), false);
            lineTo(0, 20);
            curveTo(0, 0, 0, 0, 20, 0);
            lineTo(width - 10, 0);
            append(new Arc2D.Float(width - 20, 0, 20, 20, 90, -45, Arc2D.CHORD), false);

        }

    }

}



回答2:


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);
            }
        });
    }
}


来源:https://stackoverflow.com/questions/12233928/how-to-set-a-3d-border-for-a-jdialog-with-rounded-corners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!