4

我可以在我的 JDialog 中添加一个圆角边框,如如何在 Java Swing 中创建圆角标题边框。但它仍然是一种颜色。我想让边框看起来像 3D。

这是我尝试的方法。

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

然后,没有 3D 外观,但边框已随着边框颜色变宽。
我怎样才能做到这一点?

任何示例代码或链接将不胜感激。

4

2 回答 2

7

3D边框

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);
            }
        });
    }
}
于 2012-09-02T08:21:41.893 回答
6

这样就够了吗??

在此处输入图像描述

它远非完美,但基本的想法是有效的......

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

        }

    }

}
于 2012-09-02T07:46:17.387 回答