moved ScaledSVGIcon.java from demo to flatlaf-extras and renamed to FlatSVGIcon.java

This commit is contained in:
Karl Tauber
2019-11-16 22:27:54 +01:00
parent f175c36736
commit 3de329a332
4 changed files with 24 additions and 21 deletions

View File

@@ -23,19 +23,20 @@ plugins {
dependencies {
implementation( project( ":flatlaf-core" ) )
implementation( project( ":flatlaf-extras" ) )
implementation( "com.miglayout:miglayout-swing:5.2" )
implementation( "com.jgoodies:jgoodies-forms:1.9.0" )
implementation( "com.formdev:svgSalamander:1.1.2.1" )
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
tasks {
jar {
dependsOn( ":flatlaf-core:jar" )
dependsOn( ":flatlaf-extras:jar" )
manifest {
attributes( "Main-Class" to "com.formdev.flatlaf.demo.FlatLafDemo" )

View File

@@ -20,6 +20,7 @@ import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.formdev.flatlaf.demo.intellijthemes.*;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import net.miginfocom.swing.*;
/**
@@ -273,20 +274,20 @@ class DemoFrame
buttonGroup1.add(radioButtonMenuItem3);
// JFormDesigner - End of component initialization //GEN-END:initComponents
undoMenuItem.setIcon( new ScaledSVGIcon( "icons/undo.svg" ) );
redoMenuItem.setIcon( new ScaledSVGIcon( "icons/redo.svg" ) );
undoMenuItem.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/undo.svg" ) );
redoMenuItem.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/redo.svg" ) );
cutMenuItem.setIcon( new ScaledSVGIcon( "icons/menu-cut.svg" ) );
copyMenuItem.setIcon( new ScaledSVGIcon( "icons/copy.svg" ) );
pasteMenuItem.setIcon( new ScaledSVGIcon( "icons/menu-paste.svg" ) );
cutMenuItem.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/menu-cut.svg" ) );
copyMenuItem.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/copy.svg" ) );
pasteMenuItem.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/menu-paste.svg" ) );
backButton.setIcon( new ScaledSVGIcon( "icons/back.svg" ) );
forwardButton.setIcon( new ScaledSVGIcon( "icons/forward.svg" ) );
cutButton.setIcon( new ScaledSVGIcon( "icons/menu-cut.svg" ) );
copyButton.setIcon( new ScaledSVGIcon( "icons/copy.svg" ) );
pasteButton.setIcon( new ScaledSVGIcon( "icons/menu-paste.svg" ) );
refreshButton.setIcon( new ScaledSVGIcon( "icons/refresh.svg" ) );
showToggleButton.setIcon( new ScaledSVGIcon( "icons/show.svg" ) );
backButton.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/back.svg" ) );
forwardButton.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/forward.svg" ) );
cutButton.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/menu-cut.svg" ) );
copyButton.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/copy.svg" ) );
pasteButton.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/menu-paste.svg" ) );
refreshButton.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/refresh.svg" ) );
showToggleButton.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/show.svg" ) );
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables

View File

@@ -1,136 +0,0 @@
/*
* Copyright 2019 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
*
* http://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.demo;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.net.URISyntaxException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.ui.FlatUIUtils;
import com.formdev.flatlaf.util.UIScale;
import com.kitfox.svg.app.beans.SVGIcon;
/**
* @author Karl Tauber
*/
public class ScaledSVGIcon
extends ImageIcon
{
private final String name;
private final SVGIcon svgIcon;
private boolean dark;
public ScaledSVGIcon( String name ) {
this.name = name;
svgIcon = new SVGIcon();
svgIcon.setAntiAlias( true );
}
private void update() {
if( dark == isDarkLaf() && svgIcon.getSvgURI() != null )
return;
dark = isDarkLaf();
URL url = getIconURL( name, dark );
if( url == null & dark )
url = getIconURL( name, false );
try {
svgIcon.setSvgURI( url.toURI() );
} catch( URISyntaxException ex ) {
ex.printStackTrace();
}
}
private URL getIconURL( String name, boolean dark ) {
if( dark ) {
int dotIndex = name.lastIndexOf( '.' );
name = name.substring( 0, dotIndex ) + "_dark" + name.substring( dotIndex );
}
return getClass().getResource( name );
}
@Override
public int getIconWidth() {
update();
return UIScale.scale( svgIcon.getIconWidth() );
}
@Override
public int getIconHeight() {
update();
return UIScale.scale( svgIcon.getIconHeight() );
}
@Override
public void paintIcon( Component c, Graphics g, int x, int y ) {
update();
Graphics2D g2 = (Graphics2D) g.create();
try {
FlatUIUtils.setRenderingHints( g2 );
g2.translate( x, y );
UIScale.scaleGraphics( g2 );
svgIcon.paintIcon( c, g2, 0, 0 );
} finally {
g2.dispose();
}
}
@Override
public Image getImage() {
update();
BufferedImage image = new BufferedImage( getIconWidth(), getIconHeight(), BufferedImage.TYPE_INT_ARGB );
Graphics2D g = image.createGraphics();
try {
paintIcon( null, g, 0, 0 );
} finally {
g.dispose();
}
return image;
}
private static Boolean darkLaf;
private static boolean isDarkLaf() {
if( darkLaf == null ) {
lafChanged();
UIManager.addPropertyChangeListener( e -> {
lafChanged();
} );
}
return darkLaf;
}
private static void lafChanged() {
LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
darkLaf = (lookAndFeel instanceof FlatLaf && ((FlatLaf)lookAndFeel).isDark());
}
}