From 5ef0c9aae14348f52be047e837aa9ac856fe5a10 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 28 Nov 2020 23:20:58 +0100 Subject: [PATCH] Table: fixed unstable grid line thickness when scaled on HiDPI screens (issue #152) --- CHANGELOG.md | 2 ++ .../com/formdev/flatlaf/ui/FlatTableUI.java | 26 ++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5970c8c..fdb8a348 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ FlatLaf Change Log property `JTabbedPane.hideTabAreaWithOneTab` to `true`) - Table: Do not paint last vertical grid line if auto-resize mode is not off. (issue #46) +- Table: Fixed unstable grid line thickness when scaled on HiDPI screens. (issue + #152) #### Fixed bugs diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java index ed67da8a..ea73f861 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java @@ -23,6 +23,7 @@ import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; +import java.awt.geom.Rectangle2D; import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JTable; @@ -210,25 +211,32 @@ public class FlatTableUI @Override public void paint( Graphics g, JComponent c ) { - if( table.getShowVerticalLines() ) { + boolean horizontalLines = table.getShowHorizontalLines(); + boolean verticalLines = table.getShowVerticalLines(); + if( horizontalLines || verticalLines ) { // fix grid painting issues in BasicTableUI // - do not paint last vertical grid line if auto-resize mode is not off // - in right-to-left component orientation, do not paint last vertical grid line // in any auto-resize mode; can not paint on left side of table because // cells are painted over left line + // - fix unstable grid line thickness when scaled at 125%, 150%, 175%, 225%, ... + // which paints either 1px or 2px lines depending on location boolean hideLastVerticalLine = table.getAutoResizeMode() != JTable.AUTO_RESIZE_OFF || !table.getComponentOrientation().isLeftToRight(); int tableWidth = table.getWidth(); + double systemScaleFactor = UIScale.getSystemScaleFactor( (Graphics2D) g ); + double lineThickness = (1. / systemScaleFactor) * (int) systemScaleFactor; + // Java 8 uses drawLine() to paint grid lines // Java 9+ uses fillRect() to paint grid lines g = new Graphics2DProxy( (Graphics2D) g ) { @Override public void drawLine( int x1, int y1, int x2, int y2 ) { // do not paint last vertical line - if( hideLastVerticalLine && + if( hideLastVerticalLine && verticalLines && x1 == x2 && y1 == 0 && x1 == tableWidth - 1 && wasInvokedFromPaintGrid() ) return; @@ -239,11 +247,23 @@ public class FlatTableUI @Override public void fillRect( int x, int y, int width, int height ) { // do not paint last vertical line - if( hideLastVerticalLine && + if( hideLastVerticalLine && verticalLines && width == 1 && y == 0 && x == tableWidth - 1 && wasInvokedFromPaintGrid() ) return; + // reduce line thickness to avoid unstable painted line thickness + if( lineThickness != 1 ) { + if( horizontalLines && height == 1 && wasInvokedFromPaintGrid() ) { + super.fill( new Rectangle2D.Double( x, y, width, lineThickness ) ); + return; + } + if( verticalLines && width == 1 && y == 0 && wasInvokedFromPaintGrid() ) { + super.fill( new Rectangle2D.Double( x, y, lineThickness, height ) ); + return; + } + } + super.fillRect( x, y, width, height ); }