From 7ba8274fd4fe0f0f7adc114e544f1d745c465b39 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 1 Jun 2024 14:48:33 +0200 Subject: [PATCH] FlatLineBorder: use arc from Label or Panel, if not specified in border (issue #842) --- .../com/formdev/flatlaf/UIDefaultsLoader.java | 2 +- .../formdev/flatlaf/ui/FlatLineBorder.java | 22 +++++++++++++++++-- .../formdev/flatlaf/TestUIDefaultsLoader.java | 2 +- .../flatlaf/demo/MoreComponentsPanel.java | 2 +- .../flatlaf/demo/MoreComponentsPanel.jfd | 2 +- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java index c94342749..59576d146 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java @@ -646,7 +646,7 @@ private static Object parseBorder( String value, Function resolv : 1f; int arc = (parts.size() >= 7) && !parts.get( 6 ).isEmpty() ? parseInteger( parts.get( 6 ) ) - : 0; + : -1; return (LazyValue) t -> { return (lineColor != null || arc > 0) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatLineBorder.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatLineBorder.java index cec6fd429..1ec34129f 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatLineBorder.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatLineBorder.java @@ -23,6 +23,9 @@ import java.awt.Graphics2D; import java.awt.Insets; import javax.swing.JComponent; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.plaf.ComponentUI; /** * Line border for various components. @@ -45,7 +48,7 @@ public class FlatLineBorder /** @since 2 */ private final int arc; public FlatLineBorder( Insets insets, Color lineColor ) { - this( insets, lineColor, 1f, 0 ); + this( insets, lineColor, 1f, -1 ); } /** @since 2 */ @@ -92,11 +95,26 @@ public void paintBorder( Component c, Graphics g, int x, int y, int width, int h if( lineColor == null || lineThickness <= 0 ) return; + int arc = getArc(); + if( arc < 0 ) { + // get arc from label or panel + ComponentUI ui = (c instanceof JLabel) + ? ((JLabel)c).getUI() + : (c instanceof JPanel ? ((JPanel)c).getUI() : null); + if( ui instanceof FlatLabelUI ) + arc = ((FlatLabelUI)ui).arc; + else if( ui instanceof FlatPanelUI ) + arc = ((FlatPanelUI)ui).arc; + + if( arc < 0 ) + arc = 0; + } + Graphics2D g2 = (Graphics2D) g.create(); try { FlatUIUtils.setRenderingHints( g2 ); FlatUIUtils.paintOutlinedComponent( g2, x, y, width, height, - 0, 0, 0, scale( lineThickness ), scale( getArc() ), null, lineColor, null ); + 0, 0, 0, scale( lineThickness ), scale( arc ), null, lineColor, null ); } finally { g2.dispose(); } diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/TestUIDefaultsLoader.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/TestUIDefaultsLoader.java index a303f31b2..798daa9ac 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/TestUIDefaultsLoader.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/TestUIDefaultsLoader.java @@ -86,7 +86,7 @@ void parseBorders() { assertBorderEquals( new FlatEmptyBorder( insets ), "1,2,3,4" ); assertBorderEquals( new FlatEmptyBorder( insets ), "1,2,3,4,,," ); assertBorderEquals( new FlatLineBorder( insets, Color.red ), "1,2,3,4,#f00" ); - assertBorderEquals( new FlatLineBorder( insets, Color.red, 2.5f, 0 ), "1,2,3,4,#f00,2.5" ); + assertBorderEquals( new FlatLineBorder( insets, Color.red, 2.5f, -1 ), "1,2,3,4,#f00,2.5" ); assertBorderEquals( new FlatLineBorder( insets, Color.red, 2.5f, 6 ), "1,2,3,4,#f00,2.5,6" ); assertBorderEquals( new FlatLineBorder( insets, Color.red, 1, 6 ), "1,2,3,4,#f00,,6" ); assertBorderEquals( new FlatLineBorder( insets, null, 1, 6 ), "1,2,3,4,,,6" ); diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/MoreComponentsPanel.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/MoreComponentsPanel.java index 5a19d4c7b..2728226c8 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/MoreComponentsPanel.java +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/MoreComponentsPanel.java @@ -553,7 +553,7 @@ private void initComponents() { //---- label10 ---- label10.setText("rounded border"); - label10.putClientProperty("FlatLaf.style", "border: 2,10,2,10,#135b76,1,999"); + label10.putClientProperty("FlatLaf.style", "arc: 999; border: 2,10,2,10,#135b76"); label10.setBackground(new Color(0xb8e4f3)); label10.setForeground(new Color(0x135b76)); add(label10, "cell 1 13 4 1"); diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/MoreComponentsPanel.jfd b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/MoreComponentsPanel.jfd index 9c7a2945e..5cd05c762 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/MoreComponentsPanel.jfd +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/MoreComponentsPanel.jfd @@ -517,7 +517,7 @@ new FormModel { add( new FormComponent( "javax.swing.JLabel" ) { name: "label10" "text": "rounded border" - "$client.FlatLaf.style": "border: 2,10,2,10,#135b76,1,999" + "$client.FlatLaf.style": "arc: 999; border: 2,10,2,10,#135b76" "background": new java.awt.Color( 184, 228, 243, 255 ) "foreground": new java.awt.Color( 19, 91, 118, 255 ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {