当前位置: 首页 > 知识库问答 >
问题:

分层边界-这可能在Java秋千吗?

鞠征
2023-03-14

乡亲们,

我已经读了几个关于stackoverflow的帖子,并在谷歌上搜索了一些(http://docs.oracle.com/javase/tutorial/uiswing/components/border.html)并在决定发布问题之前询问了工作中的所有Swing专家。我对Swing有基本的了解,这让事情变得非常复杂。

我附上了一个按钮的图片。。。这是一个几乎有3个边框的按钮(我的眼睛可以看到)。右边的矩形是Mac上的数字色度计应用程序。鼠标位于按钮图像上显示的黑色边框和灰色边框之间。

根据我们在边框的像素化图像上看到的,边框似乎有一个覆盖许多像素的尾巴或阴影。以Blac为例,它至少有3个像素(内部边框)。外部边框是灰色的——它跨越了近6个像素。

以下是我所做的:我使用了几个版本的Strokes和BasicStroke(3.0F),我可以为内部和外部边框生成2个像素的信息。

我的问题是:1。在Java7中使用Swing是否可以实现这种“高触感”渲染?2.如果是这样,你能给我指一下正确的方向吗?

非常感谢。

共有1个答案

岳鸿畴
2023-03-14

您可以随时创建复合边框并堆叠它们

class GUI extends JFrame {

    public static void main(String[] args) {

        new GUI();
    }

    GUI() {

        Border[] borders = new Border[]{
                   BorderFactory.createLineBorder(Color.GREEN, 3, true),
                   BorderFactory.createLineBorder(Color.BLACK, 3, true),
                   BorderFactory.createLineBorder(Color.GRAY, 6, true)};
        Border border = BorderFactory.createCompoundBorder(borders[2], 
                        BorderFactory.createCompoundBorder(borders[1], borders[0]));
        JButton button = new JButton("Button");
        button.setBorder(border);

        add(button);
        pack();
        setVisible(true);
    }
}

Edit1-堆叠LineBorders

这里有许多通过堆叠线条边框来创建渐变的例子。由于示例的数量,代码很长,您可以只复制私有方法。在最终视图中有超过3000个单个边界组件,我没有注意到任何性能问题。

class GUI extends JFrame {

    public static void main(String[] args) {

        new GUI();
    }

    public GUI() {

        JTabbedPane tabs = new JTabbedPane();
        JButton button;
        Border b1, b2, b3, b4, b5, b6;

        button = new JButton("Button");
        button.setBorder(compoundBorder(interpolateColors(Color.BLACK, Color.GREEN, 5)));
        tabs.addTab("Black-Green 5", button);

        button = new JButton("Button");
        button.setBorder(compoundBorder(interpolateColors(Color.BLACK, Color.GREEN, 100)));
        tabs.addTab("Black-Green 100", button);

        button = new JButton("Button");
        button.setBorder(compoundBorder(interpolateColors(Color.BLACK, Color.GREEN, 200)));
        tabs.addTab("Black-Green 200", button);

        button = new JButton("Button");
        button.setBorder(compoundBorder(interpolateColors(Color.GREEN, Color.BLACK, 5)));
        tabs.addTab("Green-Black 5", button);

        button = new JButton("Button");
        button.setBorder(compoundBorder(interpolateColors(Color.GREEN, Color.BLACK, 100)));
        tabs.addTab("Green-Black 100", button);

        button = new JButton("Button");
        button.setBorder(compoundBorder(interpolateColors(Color.GREEN, Color.BLACK, 200)));
        tabs.addTab("Green-Black 200", button);

        button = new JButton("Button");
        b1 = compoundBorder(interpolateColors(Color.GREEN, Color.BLACK, 5));
        b2 = compoundBorder(interpolateColors(Color.BLACK, Color.GRAY, 5));
        button.setBorder(BorderFactory.createCompoundBorder(b2, b1));
        tabs.add("Green-Black-Gray 5", button);

        button = new JButton("Button");
        b1 = compoundBorder(interpolateColors(Color.GREEN, Color.BLACK, 50));
        b2 = compoundBorder(interpolateColors(Color.BLACK, Color.GRAY, 50));
        button.setBorder(BorderFactory.createCompoundBorder(b2, b1));
        tabs.add("Green-Black-Gray 50", button);

        button = new JButton("Button");
        b1 = compoundBorder(interpolateColors(Color.GREEN, Color.BLACK, 100));
        b2 = compoundBorder(interpolateColors(Color.BLACK, Color.GRAY, 100));
        button.setBorder(BorderFactory.createCompoundBorder(b2, b1));
        tabs.add("Green-Black-Gray 100", button);

        button = new JButton("Button");
        b1 = compoundBorder(interpolateColors(Color.GREEN, Color.BLUE, 50));
        b2 = compoundBorder(interpolateColors(Color.BLUE, Color.RED, 50));
        b3 = compoundBorder(interpolateColors(Color.RED, Color.GREEN, 50));
        button.setBorder(compoundBorder(new Border[] {b1, b2, b3}));
        tabs.add("Green-Blue-Red-Green 50", button);

        button = new JButton("Button");
        b1 = compoundBorder(interpolateColors(Color.GREEN, Color.BLUE, 50));
        b2 = compoundBorder(interpolateColors(Color.BLUE, Color.RED, 50));
        b3 = compoundBorder(interpolateColors(Color.RED, Color.GREEN, 50));
        button.setBorder(compoundBorder(new Border[] {b3, b2, b1}));
        tabs.add("{Green, Blue, Red} 50", button);

        button = new JButton("Button");
        b1 = compoundBorder(interpolateColors(Color.MAGENTA, Color.BLUE, 2));
        b2 = compoundBorder(interpolateColors(Color.BLUE, Color.CYAN, 2));
        b3 = compoundBorder(interpolateColors(Color.CYAN, Color.GREEN, 2));
        b4 = compoundBorder(interpolateColors(Color.GREEN, Color.YELLOW, 2));
        b5 = compoundBorder(interpolateColors(Color.YELLOW, Color.ORANGE, 2));
        b6 = compoundBorder(interpolateColors(Color.ORANGE, Color.RED, 2));
        button.setBorder(compoundBorder(new Border[] {b1, b2, b3, b4, b5, b6}));
        tabs.add("Rainbow 2", button);

        button = new JButton("Button");
        b1 = compoundBorder(interpolateColors(Color.MAGENTA, Color.BLUE, 20));
        b2 = compoundBorder(interpolateColors(Color.BLUE, Color.CYAN, 20));
        b3 = compoundBorder(interpolateColors(Color.CYAN, Color.GREEN, 20));
        b4 = compoundBorder(interpolateColors(Color.GREEN, Color.YELLOW, 20));
        b5 = compoundBorder(interpolateColors(Color.YELLOW, Color.ORANGE, 20));
        b6 = compoundBorder(interpolateColors(Color.ORANGE, Color.RED, 20));
        button.setBorder(compoundBorder(new Border[] {b1, b2, b3, b4, b5, b6}));
        tabs.add("Rainbow 20", button);

        button = new JButton("Button");
        b1 = compoundBorder(interpolateColors(Color.MAGENTA, Color.BLUE, 40));
        b2 = compoundBorder(interpolateColors(Color.BLUE, Color.CYAN, 30));
        b3 = compoundBorder(interpolateColors(Color.CYAN, Color.GREEN, 15));
        b4 = compoundBorder(interpolateColors(Color.GREEN, Color.YELLOW, 15));
        b5 = compoundBorder(interpolateColors(Color.YELLOW, Color.ORANGE, 30));
        b6 = compoundBorder(interpolateColors(Color.ORANGE, Color.RED, 40));
        button.setBorder(compoundBorder(new Border[] {b1, b2, b3, b4, b5, b6}));
        tabs.add("Rainbow assymetric", button);

        add(tabs);
        pack();
        setSize(new Dimension(1000, 600));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    private static Color[] interpolateColors(Color c1, Color c2, int width) {

        Color[] colors = new Color[width];
        for (int i = 0; i < width; i++) {
            colors[i] = new Color( (c2.getRed() - c1.getRed()) / width * i + c1.getRed(),
                                  (c2.getGreen() - c1.getGreen()) / width * i + c1.getGreen(),
                                  (c2.getBlue() - c1.getBlue()) / width * i + c1.getBlue() );
        }
        return colors;
    }

    private static Border compoundBorder(Color[] colors) {

        Border b = BorderFactory.createLineBorder(colors[0], 1, true);
        for (int i = 1; i < colors.length; i++)
            b = BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(colors[i], 1, true), b);
        return b;
    }

    private static Border compoundBorder(Border[] borders) {

        Border b = borders[0];
        for (int i = 1; i < borders.length; i++)
            b = BorderFactory.createCompoundBorder(borders[i], b);
        return b;
    }
}

Edit2-单个Stroke边界

因为梯度可以是径向的或线性的,所以你不能得到矩形对称的边框,但是你仍然可以得到一些很好的结果。要充分利用这些,你必须阅读RadialGRadientPaint和LinearGRadientPaint。

虽然您只需要一个边框就可以了,但是有很多设置需要事先完成。此外,由于边框是图形对象而不是Swing对象,因此它不会随着组件的包围而调整大小。我能想到的唯一方法是绘制一个矩形(使用Java2D),并将其绑定到组件的边界,以便通过调整大小将其包围。无论如何,这对正确的人来说肯定是有用的,但这里有一些例子。

class GUI extends JFrame {

    public static void main(String[] args) {

        new GUI();
    }

    public GUI() {

        JTabbedPane mainTabs = new JTabbedPane();
        JTabbedPane linTabs = new JTabbedPane();
        JTabbedPane radTabs = new JTabbedPane();
        JButton button;
        Point2D start, end;
        float[] dist;
        Color[] colors;
        BasicStroke stroke;

        start = new Point2D.Float(0, 0);
        end = new Point2D.Float(500, 500);
        dist = new float[] {0.0f, 0.2f, 1.0f};
        colors = new Color[] {Color.RED, Color.WHITE, Color.BLUE};
        LinearGradientPaint lgp;
        stroke = new BasicStroke(5);
/// Linear ///
        button = new JButton("Button");
        lgp = new LinearGradientPaint(start, end, dist, colors);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, lgp));
        linTabs.addTab("Red-White-Blue 5", button);

        button = new JButton("Button");
        end = new Point2D.Float(200, 200);
        lgp = new LinearGradientPaint(start, end, dist, colors, CycleMethod.REFLECT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, lgp));
        linTabs.addTab("Red-White-Blue Reflect 5", button);

        button = new JButton("Button");
        end = new Point2D.Float(500, 500);
        lgp = new LinearGradientPaint(start, end, dist, colors, CycleMethod.REPEAT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, lgp));
        linTabs.addTab("Red-White-Blue Repeat 5", button);

        stroke = new BasicStroke(20);
        button = new JButton("Button");
        button.setBorder(BorderFactory.createStrokeBorder(stroke, lgp));
        linTabs.addTab("Red-White-Blue 20", button);

        button = new JButton("Button");
        end = new Point2D.Float(200, 200);
        lgp = new LinearGradientPaint(start, end, dist, colors, CycleMethod.REFLECT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, lgp));
        linTabs.addTab("Red-White-Blue Reflect 20", button);

        button = new JButton("Button");
        end = new Point2D.Float(500, 500);
        lgp = new LinearGradientPaint(start, end, dist, colors, CycleMethod.REPEAT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, lgp));
        linTabs.addTab("Red-White-Blue Repeat 20", button);

        dist = new float[] {0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1f};
        colors = new Color[] {Color.RED, Color.ORANGE, Color.YELLOW, Color.LIGHT_GRAY,
                                Color.BLACK, Color.RED};
        stroke = new BasicStroke(5);

        button = new JButton("Button");
        lgp = new LinearGradientPaint(start, end, dist, colors);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, lgp));
        linTabs.addTab("Dark 5", button);

        button = new JButton("Button");
        end = new Point2D.Float(200, 200);
        lgp = new LinearGradientPaint(start, end, dist, colors, CycleMethod.REFLECT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, lgp));
        linTabs.addTab("Dark Reflect 5", button);

        button = new JButton("Button");
        end = new Point2D.Float(500, 500);
        lgp = new LinearGradientPaint(start, end, dist, colors, CycleMethod.REPEAT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, lgp));
        linTabs.addTab("Dark Repeat 5", button);

        stroke = new BasicStroke(20);
        button = new JButton("Button");
        button.setBorder(BorderFactory.createStrokeBorder(stroke, lgp));
        linTabs.addTab("Dark 20", button);

        button = new JButton("Button");
        end = new Point2D.Float(200, 200);
        lgp = new LinearGradientPaint(start, end, dist, colors, CycleMethod.REFLECT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, lgp));
        linTabs.addTab("Dark Reflect 20", button);

        button = new JButton("Button");
        end = new Point2D.Float(500, 500);
        lgp = new LinearGradientPaint(start, end, dist, colors, CycleMethod.REPEAT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, lgp));
        linTabs.addTab("Dark Repeat 20", button);
/// Radial ///
        Point2D center = new Point2D.Float(20, 20);
        float radius = 1;
        dist = new float[] {0.0f, 0.2f, 1.0f};
        colors = new Color[] {Color.RED, Color.WHITE, Color.BLUE};
        RadialGradientPaint  rgp;
        stroke = new BasicStroke(40);

        button = new JButton("Button");
        rgp = new RadialGradientPaint(center, radius, dist, colors, CycleMethod.REPEAT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, rgp));
        radTabs.addTab("{Red, Blue White} assymetric 40", button);

        button = new JButton("Button");
        radius = 300;
        stroke = new BasicStroke(10);
        center = new Point2D.Float(500, 250);
        rgp = new RadialGradientPaint(center, radius, dist, colors, CycleMethod.REPEAT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, rgp));
        radTabs.addTab("{Red, Blue White} REPEAT 40", button);

        button = new JButton("Button");
        radius = 150;
        rgp = new RadialGradientPaint(center, radius, dist, colors, CycleMethod.REFLECT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, rgp));
        radTabs.addTab("{Red, Blue White} Reflect 40", button);

        colors = new Color[] {Color.PINK, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.PINK};
        dist = new float[] {0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1f};

        button = new JButton("Button");
        radius = 70;
        Point2D focus = new Point2D.Float(40, 90);
        rgp = new RadialGradientPaint(center, radius, focus, dist, colors, CycleMethod.REFLECT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, rgp));
        radTabs.addTab("Light Reflect assymetric", button);

        button = new JButton("Button");
        rgp = new RadialGradientPaint(center, radius, focus, dist, colors, CycleMethod.REPEAT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, rgp));
        radTabs.addTab("Light Repeat assymetric", button);

        button = new JButton("Button");
        radius = 70;
        stroke = new BasicStroke(10);
        center = new Point2D.Float(500, 250);
        rgp = new RadialGradientPaint(center, radius, dist, colors, CycleMethod.REFLECT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, rgp));
        radTabs.addTab("Light Reflect 10", button);

        button = new JButton("Button");
        stroke = new BasicStroke(10);
        center = new Point2D.Float(500, 250);
        rgp = new RadialGradientPaint(center, radius, dist, colors, CycleMethod.REPEAT);
        button.setBorder(BorderFactory.createStrokeBorder(stroke, rgp));
        radTabs.addTab("Light Repeat 10", button);

        mainTabs.addTab("Linear", linTabs);
        mainTabs.addTab("Radial", radTabs);
        add(mainTabs);
        pack();
        setSize(new Dimension(1000, 600));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
}

欢迎提出意见和想法。

 类似资料:
  • 边界值分析是广泛使用的黑盒测试用例设计技术之一。它用于测试边界值,因为边界附近的输入值具有较高的误差机会。 每当我们通过边界值分析进行测试时,测试人员会在输入边界值时关注软件是否产生正确的输出。 边界值是包含变量上限和下限的值。假设是任何函数的变量,其最小值为,最大值为,和都将被视为边界值。 边界值分析的基本假设是,使用边界值创建的测试用例最有可能导致错误。 和是边界值,所以测试人员更关注这些值,

  • 问题内容: 我正在使用JFrame,并且在框架上保留了背景图像。现在的问题是图像的大小小于框架的大小,因此我必须在窗口的空白部分再次保留相同的图像。如果用户单击最大化按钮,则可能需要在运行时将图像放置在框架的空白区域。谁能告诉我如何做到这一点? 问题答案: 您想要Windows桌面的背景图像之类的东西时,多次使用背景图像而不是调整其大小或仅将其居中显示吗? 您只需要保留一次图像,然后在paintC

  • 在区划图层一级图层内,可设置图层边界,用于明确该图层的业务范围,超出图层边界的区划会自动裁切 操作步骤如下: 1.点击打开区划图层“更多”菜单,选择“设置边界范围” 2.单击选中区域,可以连续选中多个 双击进入下一级区划 3.选中行政区确认后,自动显示行政区边界,对于超过边界的画区行为,会报错显示

  • 问题内容: 嗨,我有以下代码是为了找到单词“ is”,但不是在另一个字符串中时才找到它,因此单词“ this”不应返回匹配项,因此我使用\ b。但是以下代码找不到匹配项,我无法弄清楚为什么? 问题答案: 两次转义: Java中的正则表达式要求您加倍转义某些特殊的正则表达式符号,一个转义为Java,另一个转义为基础正则表达式引擎。

  • 我需要你的帮助, 似乎我在尝试在容器div内的textarea周围添加div以及包含容器div底部按钮的div边框时遇到了一些困难。 第一个问题:右侧边框丢失 第二个问题,inner2 div缺少1px纯红。 以下是问题和期望结果的图片: 预期结果是: 这里是超文本标记语言