From 791f824f614d4f49fff5c406d23c01361a160440 Mon Sep 17 00:00:00 2001 From: tpietzsch <tobias.pietzsch@gmail.com> Date: Mon, 23 Sep 2019 14:14:03 +0200 Subject: [PATCH] Improve BrightnessDialog.ColorIcon and move to separate class --- .../tools/brightness/BrightnessDialog.java | 72 +++----------- .../java/bdv/tools/brightness/ColorIcon.java | 99 +++++++++++++++++++ 2 files changed, 112 insertions(+), 59 deletions(-) create mode 100644 src/main/java/bdv/tools/brightness/ColorIcon.java diff --git a/src/main/java/bdv/tools/brightness/BrightnessDialog.java b/src/main/java/bdv/tools/brightness/BrightnessDialog.java index 34b60ebd..a123f3ee 100644 --- a/src/main/java/bdv/tools/brightness/BrightnessDialog.java +++ b/src/main/java/bdv/tools/brightness/BrightnessDialog.java @@ -31,13 +31,9 @@ package bdv.tools.brightness; import java.awt.BorderLayout; import java.awt.Color; -import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Frame; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.RenderingHints; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -52,7 +48,6 @@ import javax.swing.Action; import javax.swing.ActionMap; import javax.swing.BorderFactory; import javax.swing.BoxLayout; -import javax.swing.Icon; import javax.swing.InputMap; import javax.swing.JButton; import javax.swing.JCheckBox; @@ -131,42 +126,6 @@ public class BrightnessDialog extends JDialog setDefaultCloseOperation( WindowConstants.HIDE_ON_CLOSE ); } - /** - * Adapted from http://stackoverflow.com/a/3072979/230513 - */ - private static class ColorIcon implements Icon - { - private final int size = 16; - - private final Color color; - - public ColorIcon( final Color color ) - { - this.color = color; - } - - @Override - public void paintIcon( final Component c, final Graphics g, final int x, final int y ) - { - final Graphics2D g2d = ( Graphics2D ) g; - g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); - g2d.setColor( color ); - g2d.fillOval( x, y, size, size ); - } - - @Override - public int getIconWidth() - { - return size; - } - - @Override - public int getIconHeight() - { - return size; - } - } - public static class ColorsPanel extends JPanel { private final SetupAssignments setupAssignments; @@ -196,27 +155,22 @@ public class BrightnessDialog extends JDialog for ( final ConverterSetup setup : setupAssignments.getConverterSetups() ) { final JButton button = new JButton( new ColorIcon( getColor( setup ) ) ); - button.addActionListener( new ActionListener() - { - @Override - public void actionPerformed( final ActionEvent e ) + button.addActionListener( e -> { + colorChooser.setColor( getColor( setup ) ); + final JDialog d = JColorChooser.createDialog( button, "Choose a color", true, colorChooser, new ActionListener() { - colorChooser.setColor( getColor( setup ) ); - final JDialog d = JColorChooser.createDialog( button, "Choose a color", true, colorChooser, new ActionListener() + @Override + public void actionPerformed( final ActionEvent arg0 ) { - @Override - public void actionPerformed( final ActionEvent arg0 ) + final Color c = colorChooser.getColor(); + if (c != null) { - final Color c = colorChooser.getColor(); - if (c != null) - { - button.setIcon( new ColorIcon( c ) ); - setColor( setup, c ); - } + button.setIcon( new ColorIcon( c ) ); + setColor( setup, c ); } - }, null ); - d.setVisible( true ); - } + } + }, null ); + d.setVisible( true ); } ); button.setEnabled( setup.supportsColor() ); buttons.add( button ); @@ -237,7 +191,7 @@ public class BrightnessDialog extends JDialog return new Color( value ); } else - return new Color ( 0xFFBBBBBB ); + return null; } private static void setColor( final ConverterSetup setup, final Color color ) diff --git a/src/main/java/bdv/tools/brightness/ColorIcon.java b/src/main/java/bdv/tools/brightness/ColorIcon.java new file mode 100644 index 00000000..197a6533 --- /dev/null +++ b/src/main/java/bdv/tools/brightness/ColorIcon.java @@ -0,0 +1,99 @@ +package bdv.tools.brightness; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import javax.swing.Icon; + +/** + * Adapted from http://stackoverflow.com/a/3072979/230513 + */ +public class ColorIcon implements Icon +{ + private final int width; + + private final int height; + + private final boolean drawAsCircle; + + private final int arcWidth; + + private final int arcHeight; + + private final Color color; + + private final int size; // == min(width, height) + + private int ox; + + private int oy; + + public ColorIcon( final Color color ) + { + this( color, 16, 16, true ); + } + + public ColorIcon( final Color color, final int width, final int height, final boolean drawAsCircle ) + { + this( color, width, height, drawAsCircle, 3, 3 ); + } + + public ColorIcon( final Color color, final int width, final int height, final int arcWidth, final int arcHeight ) + { + this( color, width, height, false, arcWidth, arcHeight ); + } + + private ColorIcon( final Color color, final int width, final int height, final boolean drawAsCircle, final int arcWidth, final int arcHeight ) + { + this.color = color; + this.width = width; + this.height = height; + this.drawAsCircle = drawAsCircle; + this.arcWidth = arcWidth; + this.arcHeight = arcHeight; + + size = Math.min( width, height ); + ox = ( width - size ) / 2; + oy = ( height - size ) / 2; + } + + @Override + public void paintIcon( final Component c, final Graphics g, final int x, final int y ) + { + final Graphics2D g2d = ( Graphics2D ) g; + g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); + final int x0 = x + ox; + final int y0 = y + oy; + if ( color == null ) + { + g2d.setColor( new Color( 0xffbcbc ) ); + g2d.fillArc( x0, y0, size, size, 0, 120 ); + g2d.setColor( new Color( 0xbcffbc ) ); + g2d.fillArc( x0, y0, size, size, 120, 120 ); + g2d.setColor( new Color( 0xbcbcff ) ); + g2d.fillArc( x0, y0, size, size, 240, 120 ); + } + else + { + g2d.setColor( color ); + if ( drawAsCircle ) + g2d.fillOval( x0, y0, size, size ); + else + g2d.fillRoundRect( x, y, width, height, arcWidth, arcHeight ); + } + } + + @Override + public int getIconWidth() + { + return width; + } + + @Override + public int getIconHeight() + { + return height; + } +} -- GitLab