From 763f2ee903cf17ea2ee82c4d1807fb5ed48a8d7c Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Sat, 23 Mar 2024 16:49:20 +0100 Subject: [PATCH] Added circle progress for analog clock --- .../timecalc/app/TimeCalcConfiguration.java | 5 ++ .../utils/timecalc/app/TimeCalcProperty.java | 1 + .../utils/timecalc/swing/common/Widget.java | 27 ++++++++ .../timecalc/swing/progress/AnalogClock.java | 69 ++++++++++++------- .../timecalc/swing/progress/Battery.java | 25 +------ .../timecalc/swing/windows/ConfigWindow.java | 6 ++ .../timecalc/swing/windows/MainWindow.java | 1 + .../src/main/resources/timecalc-default.conf | 1 + 8 files changed, 87 insertions(+), 48 deletions(-) diff --git a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/app/TimeCalcConfiguration.java b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/app/TimeCalcConfiguration.java index b7e8645..9381dea 100644 --- a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/app/TimeCalcConfiguration.java +++ b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/app/TimeCalcConfiguration.java @@ -86,6 +86,10 @@ public class TimeCalcConfiguration { = new BooleanProperty( TimeCalcProperty.CLOCK_PERCENT_PROGRESS_VISIBLE .getKey()); + public final BooleanProperty clockCircleProgressVisibleProperty + = new BooleanProperty( + TimeCalcProperty.CLOCK_CIRCLE_PROGRESS_VISIBLE + .getKey()); // public final BooleanProperty batteryWavesVisibleProperty = new BooleanProperty(TimeCalcProperty.BATTERY_WAVES_VISIBLE @@ -243,6 +247,7 @@ public class TimeCalcConfiguration { clockDateVisibleOnlyIfMouseMovingOverProperty, clockSmileyVisibleProperty, clockPercentProgressVisibleProperty, + clockCircleProgressVisibleProperty, batteryWavesVisibleProperty, batteryCircleProgressProperty, batteryPercentProgressProperty, diff --git a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/app/TimeCalcProperty.java b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/app/TimeCalcProperty.java index 92af8d0..ce8a156 100644 --- a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/app/TimeCalcProperty.java +++ b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/app/TimeCalcProperty.java @@ -51,6 +51,7 @@ public enum TimeCalcProperty { "Clock : Date visible only, if mouse moving over"), CLOCK_SMILEY_VISIBLE("clock.smiley.visible","Clock : Smiley : Visible"), CLOCK_PERCENT_PROGRESS_VISIBLE("clock.percent-progress.visible","Clock : Percent progress : Visible"), + CLOCK_CIRCLE_PROGRESS_VISIBLE("clock.circle-progress.visible", "Clock : Circle Progress"), // BATTERY_WAVES_VISIBLE("battery.waves.visible", "Battery : Waves"), BATTERY_CIRCLE_PROGRESS_VISIBLE("battery.circle-progress.visible", diff --git a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/common/Widget.java b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/common/Widget.java index d918420..9d83cb8 100644 --- a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/common/Widget.java +++ b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/common/Widget.java @@ -32,6 +32,10 @@ import java.util.List; import java.util.Locale; import java.util.function.Consumer; import javax.swing.JMenuItem; +import static org.nanoboot.utils.timecalc.swing.progress.Battery.HIGH_STRONGLY_COLORED; +import static org.nanoboot.utils.timecalc.swing.progress.Battery.HIGH_WEAKLY_COLORED; +import static org.nanoboot.utils.timecalc.swing.progress.Battery.LIGHT_RED; +import static org.nanoboot.utils.timecalc.swing.progress.Battery.ULTRA_LIGHT_RED; /** * @author Robert Vokac @@ -534,6 +538,29 @@ public class Widget extends JPanel implements //brush.setBackground(currentBackgroundColor); brush.setFont(currentFont); } + + protected void paintCircleProgress(Graphics2D brush, Visibility visibility, int totalWidth, int totalHeight) { + Color currentColor = brush.getColor(); + brush.setColor( + visibility.isStronglyColored() ? HIGH_STRONGLY_COLORED + : (visibility.isWeaklyColored() ? HIGH_WEAKLY_COLORED + : Color.lightGray)); + + double angleDouble = donePercent() * 360; + + brush.fillArc(((int) (totalWidth * 1)) - 15, + totalHeight / 4 * 3 + 28, + 15, 15, 90, -(int) angleDouble); + brush.setColor( + visibility.isStronglyColored() ? LIGHT_RED + : visibility.isWeaklyColored() ? ULTRA_LIGHT_RED + : BACKGROUND_COLOR); + brush.fillArc(((int) (totalWidth * 1)) - 15, + totalHeight / 4 * 3 + 28, + 15, 15, 90, +(int) (360 - angleDouble)); + + brush.setColor(currentColor); + } public void hideWidget() { this.hidden = true; diff --git a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/progress/AnalogClock.java b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/progress/AnalogClock.java index 801acdd..7cf7deb 100644 --- a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/progress/AnalogClock.java +++ b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/progress/AnalogClock.java @@ -28,6 +28,7 @@ import java.util.List; import java.util.Locale; import java.util.function.Consumer; import javax.swing.JMenuItem; +import org.nanoboot.utils.timecalc.entity.Progress; //https://kodejava.org/how-do-i-write-a-simple-analog-clock-using-java-2d/ public class AnalogClock extends Widget { @@ -68,6 +69,10 @@ public class AnalogClock extends Widget { TimeCalcProperty.CLOCK_DATE_VISIBLE_ONLY_IF_MOUSE_MOVING_OVER .getKey()); + public BooleanProperty circleProgressVisibleProperty + = new BooleanProperty(TimeCalcProperty.BATTERY_CIRCLE_PROGRESS_VISIBLE + .getKey(), true); + public IntegerProperty startHourProperty = new IntegerProperty("startHourProperty"); public IntegerProperty startMinuteProperty @@ -194,14 +199,14 @@ public class AnalogClock extends Widget { int nowMS = now.toTotalMilliseconds(); int total = endMS - startMS; int done = nowMS - startMS; - double progress = ((double)done) / ((double)total); - this.dayProgress = progress; + double progress_ = ((double) done) / ((double) total); + this.dayProgress = progress_; //System.out.println("clock.handsLongProperty=" + handsLongProperty.isEnabled()); Visibility visibility = Visibility.valueOf(visibilityProperty.getValue()); - Graphics2D g2d = (Graphics2D) g; - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + Graphics2D brush = (Graphics2D) g; + brush.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); this.side = Math.min(getWidth(), getHeight()); @@ -220,27 +225,26 @@ public class AnalogClock extends Widget { if ((mouseOver || progressVisibleOnlyIfMouseMovingOverProperty .isDisabled()) && visibility.isStronglyColored()) { - Color currentColor = g2d.getColor(); + Color currentColor = brush.getColor(); - - g2d.setColor(Battery.getColourForProgress(progress, Visibility.WEAKLY_COLORED, mouseOver)); + brush.setColor(Battery.getColourForProgress(progress_, Visibility.WEAKLY_COLORED, mouseOver)); int startAngle = computeStartAngle(); - g2d.fillArc(0, 0, side, side, -startAngle + 90, + brush.fillArc(0, 0, side, side, -startAngle + 90, startAngle - computeEndAngle()); //System.out.println("ANGLES: " + startAngle + " " + endAngle + " " + angleDiff ); - g2d.setColor(currentColor); + brush.setColor(currentColor); } // if (millisecondEnabledProperty.isEnabled() && secondEnabledProperty .isEnabled() && minuteEnabledProperty.isEnabled() && hourEnabledProperty.isEnabled()) { - drawHand(g2d, side / 2 - 10, millisecond / 1000.0, 1.0f, + drawHand(brush, side / 2 - 10, millisecond / 1000.0, 1.0f, COLOR_FOR_MILLISECOND_HAND_STRONGLY_COLORED, visibility); if (handsLongProperty.isEnabled()) { - drawHand(g2d, (side / 2 - 10) / 4, + drawHand(brush, (side / 2 - 10) / 4, (millisecond > 500 ? millisecond - 500 : millisecond + 500) / 1000.0, 1.0f, COLOR_FOR_MILLISECOND_HAND_STRONGLY_COLORED, @@ -250,11 +254,11 @@ public class AnalogClock extends Widget { if (secondEnabledProperty.isEnabled() && minuteEnabledProperty .isEnabled() && hourEnabledProperty.isEnabled()) { - drawHand(g2d, side / 2 - 10, second / 60.0, 0.5f, Color.RED, + drawHand(brush, side / 2 - 10, second / 60.0, 0.5f, Color.RED, visibility); if (handsLongProperty.isEnabled()) { - drawHand(g2d, (side / 2 - 10) / 4, + drawHand(brush, (side / 2 - 10) / 4, (second > 30 ? second - 30 : second + 30) / 60.0, 0.5f, Color.RED, visibility); } @@ -262,10 +266,10 @@ public class AnalogClock extends Widget { if (minuteEnabledProperty.isEnabled() && hourEnabledProperty .isEnabled()) { double minutes = minute / 60.0 + second / 60.0 / 60.0; - drawHand(g2d, side / 2 - 20, minutes, 2.0f, + drawHand(brush, side / 2 - 20, minutes, 2.0f, Color.BLUE, visibility); if (handsLongProperty.isEnabled()) { - drawHand(g2d, (side / 2 - 20) / 4, + drawHand(brush, (side / 2 - 20) / 4, minutes + minutes > 0.5 ? minutes - 0.5 : minutes + (minutes > 0.5 ? (-1) : 1) * 0.5, 2.0f, @@ -275,11 +279,11 @@ public class AnalogClock extends Widget { if (hourEnabledProperty.isEnabled()) { double hours = hour / 12.0 + minute / 60.0 / 12 + second / 60 / 60 / 12; - drawHand(g2d, side / 2 - 40, + drawHand(brush, side / 2 - 40, hours, 4.0f, Color.BLACK, visibility); if (handsLongProperty.isEnabled()) { - drawHand(g2d, (side / 2 - 40) / 4, + drawHand(brush, (side / 2 - 40) / 4, hours + hours > 0.5 ? hours - 0.5 : hours + (hours > 0.5 ? (-1) : 1) * 0.5, 4.0f, Color.BLACK, visibility); @@ -290,16 +294,25 @@ public class AnalogClock extends Widget { if (borderOnlyHoursProperty.isEnabled() && minuteI % 5 != 0) { continue; } - drawBorder(g2d, minuteI, minuteI % 5 == 0 + drawBorder(brush, minuteI, minuteI % 5 == 0 ? (numbersVisibleProperty.isEnabled() ? 2f : 4f) : 1f, Color.BLACK, visibility); } } if (centreCircleVisibleProperty.isEnabled()) { - drawCentreCircle(g2d, centerX, centerY); + drawCentreCircle(brush, centerX, centerY); + } + + drawClockFace(brush, centerX, centerY, side / 2 - 40, visibility); + + if (progress == null) { + progress = new Progress(); + } + progress.set(WidgetType.DAY, (hour * 60 * 60 + minute * 60 + second) / (24d * 60d * 60d)); + if (circleProgressVisibleProperty.isEnabled()) { + paintCircleProgress(brush, visibility, getWidth(), getHeight()); } - drawClockFace(g2d, centerX, centerY, side / 2 - 40, visibility); } @@ -393,13 +406,20 @@ public class AnalogClock extends Widget { ((int) (side * 0.35)) + 60); } - if(percentProgressVisibleProperty.isEnabled()) { - brush.drawString(((int) Math.floor(dayProgress * 100)) + "%", + if (percentProgressVisibleProperty.isEnabled()) { + int d = (int) Math.floor(dayProgress * 100); + if(d < 0) { + d = 0; + } + if(d > 100) { + d = 100; + } + brush.drawString(d + "%", ((int) (side * 0.25)), ((int) (side * 0.35)) + 35); } - if(smileyVisibleProperty.isEnabled()) { + if (smileyVisibleProperty.isEnabled()) { paintSmiley(visibility, brush, ((int) (side * 0.25) + 90), ((int) (side * 0.35)) + 20); } @@ -423,7 +443,7 @@ public class AnalogClock extends Widget { @Override public List createAdditionalMenus() { - if(menuItems == null) { + if (menuItems == null) { menuItems = new ArrayList<>(); JMenu hands = new JMenu("Hands"); menuItems.add(hands); @@ -473,6 +493,7 @@ public class AnalogClock extends Widget { } return this.menuItems; } + protected Consumer createRefreshConsumer() { return (o) -> { millisecondHandMenuItem.disableMenuItem(); diff --git a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/progress/Battery.java b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/progress/Battery.java index 47cf835..4b36adb 100644 --- a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/progress/Battery.java +++ b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/progress/Battery.java @@ -325,7 +325,7 @@ public class Battery extends Widget { } if (circleProgressVisibleProperty.isEnabled()) { - paintCircleProgress(brush, visibility); + paintCircleProgress(brush, visibility, totalWidth, totalHeight); } if (donePercent > 0) { @@ -376,29 +376,6 @@ public class Battery extends Widget { ); } - private void paintCircleProgress(Graphics2D brush, Visibility visibility) { - Color currentColor = brush.getColor(); - brush.setColor( - visibility.isStronglyColored() ? HIGH_STRONGLY_COLORED - : (visibility.isWeaklyColored() ? HIGH_WEAKLY_COLORED - : Color.lightGray)); - - double angleDouble = donePercent() * 360; - - brush.fillArc(((int) (totalWidth * 0.45)) + 15, - totalHeight / 4 * 3 + 28, - 15, 15, 90, -(int) angleDouble); - brush.setColor( - visibility.isStronglyColored() ? LIGHT_RED - : visibility.isWeaklyColored() ? ULTRA_LIGHT_RED - : BACKGROUND_COLOR); - brush.fillArc(((int) (totalWidth * 0.45)) + 15, - totalHeight / 4 * 3 + 28, - 15, 15, 90, +(int) (360 - angleDouble)); - - brush.setColor(currentColor); - } - private double getRandom(int index) { return getRandom(index, false); } diff --git a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/windows/ConfigWindow.java b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/windows/ConfigWindow.java index b465537..4204753 100644 --- a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/windows/ConfigWindow.java +++ b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/windows/ConfigWindow.java @@ -128,6 +128,10 @@ public class ConfigWindow extends TWindow { = new JCheckBox( TimeCalcProperty.CLOCK_PERCENT_PROGRESS_VISIBLE .getKey()); + private final JCheckBox clockCircleProgressVisibleProperty + = new JCheckBox( + TimeCalcProperty.CLOCK_CIRCLE_PROGRESS_VISIBLE + .getKey()); // private final JCheckBox batteryWavesVisibleProperty = new JCheckBox(TimeCalcProperty.BATTERY_WAVES_VISIBLE.getKey()); @@ -362,6 +366,7 @@ public class ConfigWindow extends TWindow { .setSelected(false); clockSmileyVisibleProperty.setSelected(enable); clockPercentProgressVisibleProperty.setSelected(enable); + clockCircleProgressVisibleProperty.setSelected(enable); batteryVisibleProperty.setSelected(true); batteryWavesVisibleProperty.setSelected(enable); batteryCircleProgressVisibleProperty.setSelected(enable); @@ -417,6 +422,7 @@ public class ConfigWindow extends TWindow { clockDateVisibleOnlyIfMouseMovingOverProperty, clockSmileyVisibleProperty, clockPercentProgressVisibleProperty, + clockCircleProgressVisibleProperty, batteryVisibleProperty, batteryWavesVisibleProperty, batteryCircleProgressVisibleProperty, diff --git a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/windows/MainWindow.java b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/windows/MainWindow.java index 8d582ed..6cfeebf 100644 --- a/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/windows/MainWindow.java +++ b/modules/time-calc-app/src/main/java/org/nanoboot/utils/timecalc/swing/windows/MainWindow.java @@ -605,6 +605,7 @@ public class MainWindow extends TWindow { .bindTo(timeCalcConfiguration.clockVisibleProperty); clock.percentProgressVisibleProperty.bindTo(timeCalcConfiguration.clockPercentProgressVisibleProperty); clock.smileyVisibleProperty.bindTo(timeCalcConfiguration.clockSmileyVisibleProperty); + clock.circleProgressVisibleProperty.bindTo(timeCalcConfiguration.clockCircleProgressVisibleProperty); ComponentRegistry componentRegistry = new ComponentRegistry(); diff --git a/modules/time-calc-app/src/main/resources/timecalc-default.conf b/modules/time-calc-app/src/main/resources/timecalc-default.conf index 76119d2..a40f0a2 100644 --- a/modules/time-calc-app/src/main/resources/timecalc-default.conf +++ b/modules/time-calc-app/src/main/resources/timecalc-default.conf @@ -20,6 +20,7 @@ clock.progress.visible-only-if-mouse-moving-over=true clock.date.visible-only-if-mouse-moving-over=true clock.smiley.visible=false clock.percent-progress.visible=false +clock.circle-progress.visible=true # battery.waves.visible=true battery.circle-progress.visible=true