4

Java Swingで丸みを帯びたタイトルの境界線を作成する方法のように、丸みを帯びた角の境界線をJDialogに追加できます。しかし、それはまだ一色です。境界線を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 に答える