Skip to content

Commit

Permalink
FlatLineBorder: use arc from Label or Panel, if not specified in bord…
Browse files Browse the repository at this point in the history
…er (issue #842)
  • Loading branch information
DevCharly committed Jun 1, 2024
1 parent 2384430 commit 7ba8274
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ private static Object parseBorder( String value, Function<String, String> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 */
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down

0 comments on commit 7ba8274

Please sign in to comment.