FlatTitlePaneIcon: use getResolutionVariant(width, height) instead of getResolutionVariants() to allow creation of requested size on demand and to avoids creation of all resolution variants

Extras: `FlatSVGUtils.createWindowIconImages()` now returns a single multi-resolution image that creates requested image sizes on demand from SVG

(issue #323)
This commit is contained in:
Karl Tauber
2021-05-14 16:43:47 +02:00
parent 439e63b52f
commit 29e1dc6b55
7 changed files with 145 additions and 35 deletions

View File

@@ -19,12 +19,16 @@ package com.formdev.flatlaf.testing;
import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.*;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.ui.FlatUIUtils;
import com.formdev.flatlaf.util.MultiResolutionImageSupport;
import net.miginfocom.swing.*;
/**
@@ -355,6 +359,32 @@ public class FlatWindowDecorationsTest
window.setIconImages( images );
else if( iconTestRandomRadioButton.isSelected() )
window.setIconImage( images.get( (int) (Math.random() * images.size()) ) );
else if( iconTestMRIRadioButton.isSelected() ) {
ArrayList<Image> sortedImages = new ArrayList<>( images );
sortedImages.sort( (image1, image2) -> {
return image1.getWidth( null ) - image2.getWidth( null );
} );
window.setIconImage( MultiResolutionImageSupport.create( 0, sortedImages.toArray( new Image[sortedImages.size()] ) ) );
} else if( iconTestDynMRIRadioButton.isSelected() ) {
window.setIconImage( MultiResolutionImageSupport.create( 0,
new Dimension[] {
new Dimension( 16, 16 ),
}, dim -> {
BufferedImage image = new BufferedImage( dim.width, dim.height, BufferedImage.TYPE_INT_ARGB );
Graphics2D g = image.createGraphics();
try {
g.setColor( Color.getHSBColor( (dim.width - 16) / 64f, 1, 0.8f ) );
g.fillRect( 0, 0, dim.width, dim.height );
g.setColor( Color.white );
g.setFont( new Font( "Dialog", Font.PLAIN, (int) (dim.width * 0.8) ) );
FlatUIUtils.drawString( this, g, String.valueOf( dim.width ), 0, dim.height - 2 );
} finally {
g.dispose();
}
return image;
} ) );
}
}
private JRootPane getWindowRootPane() {
@@ -403,6 +433,8 @@ public class FlatWindowDecorationsTest
iconNoneRadioButton = new JRadioButton();
iconTestAllRadioButton = new JRadioButton();
iconTestRandomRadioButton = new JRadioButton();
iconTestMRIRadioButton = new JRadioButton();
iconTestDynMRIRadioButton = new JRadioButton();
JButton openDialogButton = new JButton();
JButton openFrameButton = new JButton();
menuBar = new JMenuBar();
@@ -642,6 +674,8 @@ public class FlatWindowDecorationsTest
// rows
"[]" +
"[]" +
"[]" +
"[]" +
"[]"));
//---- iconNoneRadioButton ----
@@ -659,6 +693,16 @@ public class FlatWindowDecorationsTest
iconTestRandomRadioButton.setText("test random");
iconTestRandomRadioButton.addActionListener(e -> iconChanged());
panel2.add(iconTestRandomRadioButton, "cell 0 2");
//---- iconTestMRIRadioButton ----
iconTestMRIRadioButton.setText("test multi-resolution (Java 9+)");
iconTestMRIRadioButton.addActionListener(e -> iconChanged());
panel2.add(iconTestMRIRadioButton, "cell 0 3");
//---- iconTestDynMRIRadioButton ----
iconTestDynMRIRadioButton.setText("test dynamic multi-resolution (Java 9+)");
iconTestDynMRIRadioButton.addActionListener(e -> iconChanged());
panel2.add(iconTestDynMRIRadioButton, "cell 0 4");
}
add(panel2, "cell 1 8");
@@ -858,6 +902,8 @@ public class FlatWindowDecorationsTest
iconButtonGroup.add(iconNoneRadioButton);
iconButtonGroup.add(iconTestAllRadioButton);
iconButtonGroup.add(iconTestRandomRadioButton);
iconButtonGroup.add(iconTestMRIRadioButton);
iconButtonGroup.add(iconTestDynMRIRadioButton);
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
@@ -892,6 +938,8 @@ public class FlatWindowDecorationsTest
private JRadioButton iconNoneRadioButton;
private JRadioButton iconTestAllRadioButton;
private JRadioButton iconTestRandomRadioButton;
private JRadioButton iconTestMRIRadioButton;
private JRadioButton iconTestDynMRIRadioButton;
private JMenuBar menuBar;
// JFormDesigner - End of variables declaration //GEN-END:variables
}

View File

@@ -328,7 +328,7 @@ new FormModel {
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$columnConstraints": "[fill]"
"$rowConstraints": "[][][]"
"$rowConstraints": "[][][][][]"
"$layoutConstraints": "ltr,insets 0,hidemode 3,gap 0 0"
} ) {
name: "panel2"
@@ -366,6 +366,28 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 2"
} )
add( new FormComponent( "javax.swing.JRadioButton" ) {
name: "iconTestMRIRadioButton"
"text": "test multi-resolution (Java 9+)"
"$buttonGroup": new FormReference( "iconButtonGroup" )
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "iconChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 3"
} )
add( new FormComponent( "javax.swing.JRadioButton" ) {
name: "iconTestDynMRIRadioButton"
"text": "test dynamic multi-resolution (Java 9+)"
"$buttonGroup": new FormReference( "iconButtonGroup" )
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "iconChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 4"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 8"
} )
@@ -386,7 +408,7 @@ new FormModel {
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 690, 440 )
"size": new java.awt.Dimension( 690, 495 )
} )
add( new FormContainer( "javax.swing.JMenuBar", new FormLayoutManager( class javax.swing.JMenuBar ) ) {
name: "menuBar"
@@ -552,18 +574,18 @@ new FormModel {
} )
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 470 )
"location": new java.awt.Point( 0, 515 )
"size": new java.awt.Dimension( 255, 30 )
} )
add( new FormNonVisual( "javax.swing.ButtonGroup" ) {
name: "styleButtonGroup"
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 520 )
"location": new java.awt.Point( 0, 565 )
} )
add( new FormNonVisual( "javax.swing.ButtonGroup" ) {
name: "iconButtonGroup"
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 570 )
"location": new java.awt.Point( 0, 615 )
} )
}
}