@ -4,11 +4,19 @@ import com.fr.base.CellBorderSourceFlag;
import com.fr.base.CellBorderStyle ;
import com.fr.base.Style ;
import com.fr.design.mainframe.theme.utils.DefaultThemedTemplateCellElementCase ;
import com.fr.general.IOUtils ;
import com.fr.report.cell.TemplateCellElement ;
import javax.swing.JPanel ;
import java.awt.AlphaComposite ;
import java.awt.Color ;
import java.awt.Composite ;
import java.awt.Dimension ;
import java.awt.Graphics ;
import java.awt.Graphics2D ;
import java.awt.GridLayout ;
import java.awt.RenderingHints ;
import java.awt.image.BufferedImage ;
/ * *
* @author Starryi
@ -16,6 +24,11 @@ import java.awt.GridLayout;
* Created by Starryi on 2021 / 9 / 3
* /
public class CellRectangleStylePreviewPane extends JPanel {
private static final BufferedImage transparentBackgroundImage = IOUtils . readImage ( "/com/fr/design/images/transparent_background.png" ) ;
private final float transparentBackgroundWidth ;
private final float transparentBackgroundHeight ;
private static final int ROW_COUNT = 2 ;
private static final int COLUMN_COUNT = 2 ;
@ -24,7 +37,12 @@ public class CellRectangleStylePreviewPane extends JPanel {
private final CellStylePreviewPane [ ] [ ] cellStylePreviewPaneGrid = new CellStylePreviewPane [ ROW_COUNT ] [ COLUMN_COUNT ] ;
public CellRectangleStylePreviewPane ( boolean supportInnerBorder ) {
transparentBackgroundWidth = transparentBackgroundImage . getWidth ( null ) ;
transparentBackgroundHeight = transparentBackgroundImage . getHeight ( null ) ;
setLayout ( new GridLayout ( 2 , 2 ) ) ;
setOpaque ( false ) ;
setBackground ( null ) ;
for ( int r = 0 ; r < ROW_COUNT ; r + + ) {
for ( int c = 0 ; c < COLUMN_COUNT ; c + + ) {
@ -101,4 +119,56 @@ public class CellRectangleStylePreviewPane extends JPanel {
return new Dimension ( width , height ) ;
}
@Override
public void paint ( Graphics g ) {
Graphics2D g2d = ( Graphics2D ) g ;
g2d . clearRect ( 0 , 0 , getWidth ( ) , getHeight ( ) ) ;
g2d . setRenderingHint ( RenderingHints . KEY_ANTIALIASING , RenderingHints . VALUE_ANTIALIAS_ON ) ;
g2d . setRenderingHint ( RenderingHints . KEY_TEXT_ANTIALIASING , RenderingHints . VALUE_TEXT_ANTIALIAS_ON ) ;
paintTransparentBackground ( ( Graphics2D ) g , cellElementGrid [ 0 ] [ 0 ] . getStyle ( ) ) ;
g2d . setRenderingHint ( RenderingHints . KEY_ANTIALIASING , RenderingHints . VALUE_ANTIALIAS_OFF ) ;
g2d . setRenderingHint ( RenderingHints . KEY_TEXT_ANTIALIASING , RenderingHints . VALUE_TEXT_ANTIALIAS_OFF ) ;
super . paint ( g ) ;
}
private void paintTransparentBackground ( Graphics2D g2d , Style style ) {
float alpha = computeTransparentBackgroundAlpha ( style ) ;
float scaleWidth = 1 . 0F * getWidth ( ) / transparentBackgroundWidth ;
float scaleHeight = 1 . 0F * getHeight ( ) / transparentBackgroundHeight ;
float maxScale = Math . max ( scaleWidth , scaleHeight ) ;
if ( maxScale < = 1 ) {
scaleWidth = scaleHeight = 1 ;
} else {
scaleHeight = scaleWidth = maxScale ;
}
Composite oldComposite = g2d . getComposite ( ) ;
g2d . setComposite ( AlphaComposite . getInstance ( AlphaComposite . SRC_ATOP , alpha ) ) ;
g2d . drawImage ( transparentBackgroundImage , 0 , 0 , ( int ) ( transparentBackgroundWidth * scaleWidth ) , ( int ) ( transparentBackgroundHeight * scaleHeight ) , null ) ;
g2d . setComposite ( oldComposite ) ;
}
private float computeTextColorBrightness ( Style style ) {
Color fontColor = style . getFRFont ( ) . getForeground ( ) ;
return fontColor . getRed ( ) * 0 . 299F + fontColor . getGreen ( ) * 0 . 587F + fontColor . getBlue ( ) * 0 . 114F ;
}
private float computeTransparentBackgroundAlpha ( Style style ) {
float textBrightness = computeTextColorBrightness ( style ) ;
float alpha = 1 . 0F ;
if ( textBrightness < 50 ) {
alpha = 0 . 2F ;
} else if ( textBrightness < 160 ) {
alpha = 0 . 5F ;
}
return alpha ;
}
}