From e0e0aa574c3df219796468d3916c7b96ca79e1d5 Mon Sep 17 00:00:00 2001 From: Adrian Blanco Date: Sat, 17 Feb 2018 00:58:54 +0100 Subject: [PATCH 1/2] Implement steps instead of gradient for rainbow, switch rainbow colors to the "official" ones, add .gitignore --- .gitignore | 94 ++++++++++++++++++++++++++++++++++++++ src/NyanProgressBarUi.java | 45 +++++++++++------- src/RainbowColors.java | 12 +++++ 3 files changed, 134 insertions(+), 17 deletions(-) create mode 100644 .gitignore create mode 100644 src/RainbowColors.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b3e60c --- /dev/null +++ b/.gitignore @@ -0,0 +1,94 @@ + +# Created by https://www.gitignore.io/api/java,intellij + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Ruby plugin and RubyMine +/.rakeTasks + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +.idea/sonarlint + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + + +# End of https://www.gitignore.io/api/java,intellij diff --git a/src/NyanProgressBarUi.java b/src/NyanProgressBarUi.java index 9cb04e1..2301d39 100644 --- a/src/NyanProgressBarUi.java +++ b/src/NyanProgressBarUi.java @@ -9,19 +9,16 @@ import javax.swing.*; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicProgressBarUI; + import java.awt.*; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.geom.*; +import java.util.ArrayList; +import java.util.List; public class NyanProgressBarUi extends BasicProgressBarUI { - private static final float ONE_OVER_SEVEN = 1f / 7; - private static final Color VIOLET = new Color(90, 0, 157); - - { - - } @SuppressWarnings({"MethodOverridesStaticMethodOfSuperclass", "UnusedDeclaration"}) public static ComponentUI createUI(JComponent c) { c.setBorder(JBUI.Borders.empty().asUIResource()); @@ -74,9 +71,7 @@ protected void paintIndeterminate(Graphics g2d, JComponent c) { int h = c.getPreferredSize().height; if (!isEven(c.getHeight() - h)) h++; - LinearGradientPaint baseRainbowPaint = new LinearGradientPaint(0, JBUI.scale(2), 0, h - JBUI.scale(6), - new float[]{ONE_OVER_SEVEN * 1, ONE_OVER_SEVEN * 2, ONE_OVER_SEVEN * 3, ONE_OVER_SEVEN * 4, ONE_OVER_SEVEN * 5, ONE_OVER_SEVEN * 6, ONE_OVER_SEVEN * 7}, - new Color[]{Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.cyan, Color.blue, VIOLET}); + LinearGradientPaint baseRainbowPaint = getRainbowPaintFromHeight(h); g.setPaint(baseRainbowPaint); @@ -88,11 +83,6 @@ protected void paintIndeterminate(Graphics g2d, JComponent c) { g.translate(0, (c.getHeight() - h) / 2); int x = -offset; - -// LinearGradientPaint baseRainbowPaint = new LinearGradientPaint(0, JBUI.scale(2), 0, h - JBUI.scale(6), -// new float[]{ONE_OVER_SEVEN * 1, ONE_OVER_SEVEN * 2, ONE_OVER_SEVEN * 3, ONE_OVER_SEVEN * 4, ONE_OVER_SEVEN * 5, ONE_OVER_SEVEN * 6, ONE_OVER_SEVEN * 7}, -// new Color[]{Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.cyan, Color.blue, VIOLET}); - Paint old = g.getPaint(); g.setPaint(baseRainbowPaint); @@ -204,9 +194,7 @@ protected void paintDeterminate(Graphics g, JComponent c) { g2.setColor(c.getParent().getBackground()); g2.fill(new RoundRectangle2D.Float(off, off, w - 2f*off - off, h - 2f*off - off, R, R)); // g2.setColor(progressBar.getForeground()); - g2.setPaint(new LinearGradientPaint(0, JBUI.scale(2), 0, h - JBUI.scale(6), - new float[]{ONE_OVER_SEVEN * 1, ONE_OVER_SEVEN * 2, ONE_OVER_SEVEN * 3, ONE_OVER_SEVEN * 4, ONE_OVER_SEVEN * 5, ONE_OVER_SEVEN * 6, ONE_OVER_SEVEN * 7}, - new Color[]{Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.cyan, Color.blue, VIOLET})); + g2.setPaint(getRainbowPaintFromHeight(h)); NyanIcons.CAT_ICON.paintIcon(progressBar, g2, amountFull - JBUI.scale(10), -JBUI.scale(6)); g2.fill(new RoundRectangle2D.Float(2f*off,2f*off, amountFull - JBUI.scale(5f), h - JBUI.scale(5f), JBUI.scale(7f), JBUI.scale(7f))); @@ -258,6 +246,29 @@ private void paintString(Graphics g, int x, int y, int w, int h, int fillStart, g2.setClip(oldClip); } + /** Create a gradient such as [0, 0.99, 1, 1.99, ...], [RED, RED, ORANGE, ORANGE, ..] */ + private LinearGradientPaint getRainbowPaintFromHeight(float scaledHeight) { + + int numRainbowColors = RainbowColors.RAINBOW_ARRAY.length; + float epsilon = 0.000001f; + + float[] fractionList = new float[numRainbowColors * 2]; + Color[] colorList = new Color[numRainbowColors * 2]; + + for (int i = 0; i < numRainbowColors; i++) { + fractionList[i * 2] = (float) i / numRainbowColors; + fractionList[i * 2 + 1] = ((i + 1) - epsilon) / numRainbowColors; + + colorList[i * 2] = RainbowColors.RAINBOW_ARRAY[i]; + colorList[i * 2 + 1] = RainbowColors.RAINBOW_ARRAY[i]; + } + + return new LinearGradientPaint(0, JBUI.scale(1), + 0, scaledHeight - JBUI.scale(3), + fractionList, colorList); + } + + @Override protected int getBoxLength(int availableLength, int otherDimension) { return availableLength; diff --git a/src/RainbowColors.java b/src/RainbowColors.java new file mode 100644 index 0000000..c146ef7 --- /dev/null +++ b/src/RainbowColors.java @@ -0,0 +1,12 @@ +import java.awt.*; + +public class RainbowColors { + static final Color RED = new Color(255, 0, 0); + static final Color ORANGE = new Color(255, 154, 0); + static final Color YELLOW = new Color(255, 255, 0); + static final Color GREEN = new Color(25, 255, 0); + static final Color BLUE = new Color(0, 150, 255); + static final Color VIOLET = new Color(100, 30, 255); + + static final Color[] RAINBOW_ARRAY = new Color[]{RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET}; +} From 759b4910e64013e94049ac08a381eb87e8793747 Mon Sep 17 00:00:00 2001 From: Adrian Blanco Date: Sat, 17 Feb 2018 01:02:42 +0100 Subject: [PATCH 2/2] Remove accidentally added imports --- src/NyanProgressBarUi.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/NyanProgressBarUi.java b/src/NyanProgressBarUi.java index 2301d39..08b6890 100644 --- a/src/NyanProgressBarUi.java +++ b/src/NyanProgressBarUi.java @@ -9,13 +9,10 @@ import javax.swing.*; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicProgressBarUI; - import java.awt.*; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.geom.*; -import java.util.ArrayList; -import java.util.List; public class NyanProgressBarUi extends BasicProgressBarUI {