diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatPaintingIconsTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatPaintingIconsTest.java new file mode 100644 index 00000000..b0ef0ea8 --- /dev/null +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatPaintingIconsTest.java @@ -0,0 +1,276 @@ +/* + * Copyright 2022 FormDev Software GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.formdev.flatlaf.testing; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.geom.AffineTransform; +import javax.swing.*; +import com.formdev.flatlaf.FlatSystemProperties; +import com.formdev.flatlaf.icons.FlatCapsLockIcon; +import com.formdev.flatlaf.icons.FlatClearIcon; +import com.formdev.flatlaf.icons.FlatFileChooserDetailsViewIcon; +import com.formdev.flatlaf.icons.FlatFileChooserHomeFolderIcon; +import com.formdev.flatlaf.icons.FlatFileChooserListViewIcon; +import com.formdev.flatlaf.icons.FlatFileChooserNewFolderIcon; +import com.formdev.flatlaf.icons.FlatFileChooserUpFolderIcon; +import com.formdev.flatlaf.icons.FlatFileViewComputerIcon; +import com.formdev.flatlaf.icons.FlatFileViewDirectoryIcon; +import com.formdev.flatlaf.icons.FlatFileViewFileIcon; +import com.formdev.flatlaf.icons.FlatFileViewFloppyDriveIcon; +import com.formdev.flatlaf.icons.FlatFileViewHardDriveIcon; +import com.formdev.flatlaf.icons.FlatHelpButtonIcon; +import com.formdev.flatlaf.icons.FlatOptionPaneErrorIcon; +import com.formdev.flatlaf.icons.FlatOptionPaneInformationIcon; +import com.formdev.flatlaf.icons.FlatOptionPaneQuestionIcon; +import com.formdev.flatlaf.icons.FlatOptionPaneWarningIcon; +import com.formdev.flatlaf.icons.FlatRevealIcon; +import com.formdev.flatlaf.icons.FlatSearchIcon; +import com.formdev.flatlaf.icons.FlatSearchWithHistoryIcon; +import com.formdev.flatlaf.icons.FlatTreeClosedIcon; +import com.formdev.flatlaf.icons.FlatTreeCollapsedIcon; +import com.formdev.flatlaf.icons.FlatTreeExpandedIcon; +import com.formdev.flatlaf.icons.FlatTreeLeafIcon; +import com.formdev.flatlaf.icons.FlatTreeOpenIcon; +import com.formdev.flatlaf.ui.FlatUIUtils; +import com.formdev.flatlaf.util.HiDPIUtils; +import com.formdev.flatlaf.util.UIScale; +import net.miginfocom.swing.*; + +/** + * @author Karl Tauber + */ +public class FlatPaintingIconsTest + extends JPanel +{ + private int scale = 16; + private Timer timer = null; + + public static void main( String[] args ) { + System.setProperty( FlatSystemProperties.UI_SCALE, "1x" ); + System.setProperty( "sun.java2d.uiScale", "1x" ); + + SwingUtilities.invokeLater( () -> { + FlatTestFrame frame = FlatTestFrame.create( args, "FlatPaintingIconsTest" ); + frame.showFrame( FlatPaintingIconsTest::new ); + } ); + } + + FlatPaintingIconsTest() { + initComponents(); + + scrollPane.getHorizontalScrollBar().setUnitIncrement( UIScale.scale( 25 ) ); + scrollPane.getVerticalScrollBar().setUnitIncrement( UIScale.scale( 25 ) ); + + addIconPainter( new FlatFileViewDirectoryIcon(), false ); + addIconPainter( new FlatFileViewFileIcon(), false ); + addIconPainter( new FlatFileViewComputerIcon(), false ); + addIconPainter( new FlatFileViewHardDriveIcon(), false ); + addIconPainter( new FlatFileViewFloppyDriveIcon(), true ); + + addIconPainter( new FlatFileChooserNewFolderIcon(), false ); + addIconPainter( new FlatFileChooserUpFolderIcon(), false ); + addIconPainter( new FlatFileChooserHomeFolderIcon(), false ); + addIconPainter( new FlatFileChooserDetailsViewIcon(), false ); + addIconPainter( new FlatFileChooserListViewIcon(), true ); + + addIconPainter( new FlatTreeClosedIcon(), false ); + addIconPainter( new FlatTreeOpenIcon(), false ); + addIconPainter( new FlatTreeLeafIcon(), false ); + addIconPainter( new FlatTreeCollapsedIcon(), false ); + addIconPainter( new FlatTreeExpandedIcon(), true ); + + addIconPainter( new FlatSearchIcon(), false ); + addIconPainter( new FlatSearchWithHistoryIcon(), false ); + addIconPainter( new FlatClearIcon(), false ); + addIconPainter( new FlatRevealIcon(), false ); + addIconPainter( new FlatCapsLockIcon(), true ); + + + addIconPainter( new FlatOptionPaneErrorIcon(), false ); + addIconPainter( new FlatOptionPaneInformationIcon(), false ); + addIconPainter( new FlatOptionPaneWarningIcon(), false ); + addIconPainter( new FlatOptionPaneQuestionIcon(), false ); + addIconPainter( new FlatHelpButtonIcon(), true ); +/* + addIconPainter( new FlatAscendingSortIcon(), false ); + addIconPainter( new FlatDescendingSortIcon(), false ); + addIconPainter( new FlatMenuArrowIcon(), true ); + + addIconPainter( new FlatWindowIconifyIcon(), false ); + addIconPainter( new FlatWindowMaximizeIcon(), false ); + addIconPainter( new FlatWindowRestoreIcon(), false ); + addIconPainter( new FlatWindowCloseIcon(), false ); + addIconPainter( new FlatTabbedPaneCloseIcon(), true ); + + addIconPainter( new FlatInternalFrameIconifyIcon(), false ); + addIconPainter( new FlatInternalFrameMaximizeIcon(), false ); + addIconPainter( new FlatInternalFrameRestoreIcon(), false ); + addIconPainter( new FlatInternalFrameCloseIcon(), true ); +*/ + // repaint to see code changes immediately when running in debugger + timer = new Timer( 500, e -> { + // stop timer to allow application to exit + if( !isDisplayable() ) + timer.stop(); + + repaint(); + } ); + timer.start(); + } + + private void addIconPainter( Icon icon, boolean wrap ) { + String name = icon.getClass().getSimpleName(); + panel.add( new JLabel( name ), "split 2, flowy" ); + panel.add( new IconPainter( icon ), wrap ? "wrap" : null ); + } + + private void scaleChanged() { + scale = Math.max( scaleSlider.getValue(), 1 ); + panel.revalidate(); + panel.repaint(); + } + + private void initComponents() { + // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents + scrollPane = new JScrollPane(); + panel = new JPanel(); + JPanel panel1 = new JPanel(); + JLabel scaleLabel = new JLabel(); + scaleSlider = new JSlider(); + + //======== this ======== + setLayout(new BorderLayout()); + + //======== scrollPane ======== + { + scrollPane.setBorder(null); + + //======== panel ======== + { + panel.setLayout(new MigLayout( + "insets dialog,hidemode 3", + // columns + "[left]", + // rows + "[top]")); + } + scrollPane.setViewportView(panel); + } + add(scrollPane, BorderLayout.CENTER); + + //======== panel1 ======== + { + panel1.setLayout(new MigLayout( + "hidemode 3", + // columns + "[fill]" + + "[grow,fill]", + // rows + "[]")); + + //---- scaleLabel ---- + scaleLabel.setText("Scale:"); + panel1.add(scaleLabel, "cell 0 0"); + + //---- scaleSlider ---- + scaleSlider.setMaximum(64); + scaleSlider.setValue(16); + scaleSlider.setPaintTicks(true); + scaleSlider.setMajorTickSpacing(8); + scaleSlider.setPaintLabels(true); + scaleSlider.setMinorTickSpacing(1); + scaleSlider.setSnapToTicks(true); + scaleSlider.addChangeListener(e -> scaleChanged()); + panel1.add(scaleSlider, "cell 1 0"); + } + add(panel1, BorderLayout.NORTH); + // JFormDesigner - End of component initialization //GEN-END:initComponents + } + + // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables + private JScrollPane scrollPane; + private JPanel panel; + private JSlider scaleSlider; + // JFormDesigner - End of variables declaration //GEN-END:variables + + //---- class IconPainter -------------------------------------------------- + + public class IconPainter + extends JComponent + { + private final Icon icon; + + public IconPainter( Icon icon ) { + this.icon = icon; + } + + private int getScale() { + int iconWidth = icon.getIconWidth(); + return iconWidth <= 16 ? scale + : iconWidth <= 24 ? (int) (scale * 0.75) + : iconWidth <= 32 ? (scale / 2) + : (int) (scale * (16. / iconWidth)); + } + + @Override + public Dimension getPreferredSize() { + int scale = getScale(); + return UIScale.scale( new Dimension( icon.getIconWidth() * scale, icon.getIconHeight() * scale ) ); + } + + @Override + public Dimension getMinimumSize() { + return getPreferredSize(); + } + + @Override + protected void paintComponent( Graphics g ) { + Graphics2D g2 = (Graphics2D) g; + Object[] oldRenderingHints = FlatUIUtils.setRenderingHints( g2 ); + int scale = getScale(); + + // paint icon scaled + AffineTransform oldTransform = g2.getTransform(); + g2.scale( scale, scale ); + icon.paintIcon( this, g2, 0, 0 ); + g2.setTransform( oldTransform ); + + // paint border and grid + HiDPIUtils.paintAtScale1x( g2, 0, 0, getWidth(), getHeight(), + (g2d, x2, y2, width2, height2, scaleFactor) -> { + // draw border + g2d.setColor( Color.magenta ); + g2d.drawRect( x2, y2, width2 - 1, height2 - 1 ); + + // draw grid + if( scale > 2 ) { + g2d.setColor( new Color( (Color.blue.getRGB() & 0xffffff) | (0x20 << 24), true ) ); + for( int x = x2 + scale; x < x2 + width2; x += scale ) + g2d.drawLine( x, y2 + 1, x, y2 + height2 - 2 ); + for( int y = y2 + scale; y < y2 + height2; y += scale ) + g2d.drawLine( x2 + 1, y, x2 + width2 - 2, y ); + } + } ); + + FlatUIUtils.resetRenderingHints( g2, oldRenderingHints ); + } + } +} diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatPaintingIconsTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatPaintingIconsTest.jfd new file mode 100644 index 00000000..77a753ac --- /dev/null +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatPaintingIconsTest.jfd @@ -0,0 +1,66 @@ +JFDML JFormDesigner: "8.0.0.0.122" Java: "17.0.2" encoding: "UTF-8" + +new FormModel { + contentType: "form/swing" + root: new FormRoot { + auxiliary() { + "JavaCodeGenerator.defaultVariableLocal": true + } + add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.BorderLayout ) ) { + name: "this" + add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "scrollPane" + "border": sfield com.jformdesigner.model.FormObject NULL_VALUE + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { + "$layoutConstraints": "insets dialog,hidemode 3" + "$columnConstraints": "[left]" + "$rowConstraints": "[top]" + } ) { + name: "panel" + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + } ) + }, new FormLayoutConstraints( class java.lang.String ) { + "value": "Center" + } ) + add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { + "$layoutConstraints": "hidemode 3" + "$columnConstraints": "[fill][grow,fill]" + "$rowConstraints": "[]" + } ) { + name: "panel1" + add( new FormComponent( "javax.swing.JLabel" ) { + name: "scaleLabel" + "text": "Scale:" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 0" + } ) + add( new FormComponent( "javax.swing.JSlider" ) { + name: "scaleSlider" + "maximum": 64 + "value": 16 + "paintTicks": true + "majorTickSpacing": 8 + "paintLabels": true + "minorTickSpacing": 1 + "snapToTicks": true + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + addEvent( new FormEvent( "javax.swing.event.ChangeListener", "stateChanged", "scaleChanged", false ) ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 0" + } ) + }, new FormLayoutConstraints( class java.lang.String ) { + "value": "North" + } ) + }, new FormLayoutConstraints( null ) { + "location": new java.awt.Point( 0, 0 ) + "size": new java.awt.Dimension( 565, 200 ) + } ) + } +}