Added ProgressSquare

This commit is contained in:
Robert Vokac 2024-01-20 17:52:25 +00:00
parent f77f871089
commit 8e152189e4
No known key found for this signature in database
GPG Key ID: 693D30BEE3329055
4 changed files with 77 additions and 9 deletions

View File

@ -71,9 +71,9 @@ public class AnalogClock extends JPanel {
int radius) {
g2d.setStroke(new BasicStroke(2.0f));
g2d.setColor(highlight ? Color.BLUE : FOREGROUND_COLOR);
System.out.println("centerX=" + centerX);
System.out.println("centerY=" + centerY);
System.out.println("radius=" + radius);
// System.out.println("centerX=" + centerX);
// System.out.println("centerY=" + centerY);
// System.out.println("radius=" + radius);
g2d.drawOval(1, 1, centerX * 2 - 4, centerY * 2 - 4);
g2d.drawOval(2, 2, centerX * 2 - 4, centerY * 2 - 4);
// g2d.drawOval(3, 3, centerX * 2 - 6, centerY * 2 - 6);

View File

@ -32,8 +32,8 @@ public class Main {
"0:00"
);
try {
TimeCalc timeCalc =
new TimeCalc(startTime, overTime);
TimeCalcWindow timeCalc =
new TimeCalcWindow(startTime, overTime);
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error: " + e.getMessage(), e.getMessage(), JOptionPane.ERROR_MESSAGE);
}

View File

@ -0,0 +1,63 @@
package rvc.timecalc;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
public class ProgressSquare extends JPanel {
private static final Color FOREGROUND_COLOR = new Color(220, 220, 220);
private static final Color BACKGROUND_COLOR = new Color(238, 238, 238);
private int side = 0;
private int square;
private double donePercent = 0;
public void setDonePercent(double donePercent) {
this.donePercent = donePercent;
}
public ProgressSquare() {
setPreferredSize(new Dimension(400, 400));
setBackground(BACKGROUND_COLOR);
new Timer(1000, e -> repaint()).start();
}
@Override
public void paintComponent(Graphics g) {
if(side == 0) {
this.side = Math.min(getWidth(), getHeight());
this.square = side * side;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(FOREGROUND_COLOR);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
System.out.println("square=" + square);
int dotNumber = (int)(donePercent * square);
int y = dotNumber / side;
int x = dotNumber - y * side;
System.out.println("dotNumber=" + dotNumber);
System.out.println("x=" + x);
System.out.println("y=" + y);
if(y > 1) {
g2d.fillRect(side - 4, side - 4, 4, 4);
g2d.fillRect(1, side - 4, 4, 4);
g2d.fillRect(1, 1, side, y - 1);
g2d.drawRect(1, y, x, 1);
}
}
}

View File

@ -18,10 +18,10 @@ import java.util.HashSet;
import java.util.Set;
/**
* @author Robert
* @author pc00289
* @since 08.02.2024
*/
public class TimeCalc {
public class TimeCalcWindow {
private static final String DEFAULT_OVERTIME = "0:00";
private static final int WORKING_HOURS_LENGTH = 8;
private static final int WORKING_MINUTES_LENGTH = 30;
@ -41,7 +41,7 @@ public class TimeCalc {
private final Set<Integer> alreadyShownPercents = new HashSet<>();
private boolean stopBeforeEnd = false;
public TimeCalc(String startTimeIn, String overTimeIn) {
public TimeCalcWindow(String startTimeIn, String overTimeIn) {
this.startTime = startTimeIn;
this.overTime = (overTimeIn == null || overTimeIn.isEmpty()) ? DEFAULT_OVERTIME : overTimeIn;
@ -81,7 +81,7 @@ public class TimeCalc {
text.setBackground(new Color(238,238,238));
window.add(text);
window.setSize(800,350);
window.setSize(1050,350);
window.setLayout(null);
window.setVisible(true);
window.setTitle("Time Calc");
@ -100,6 +100,10 @@ public class TimeCalc {
analogClock.setBounds(550,10,200, 200);
window.add(analogClock);
ProgressSquare progressSquare = new ProgressSquare();
progressSquare.setBounds(550 + analogClock.getWidth() + 10,10,200, 200);
window.add(progressSquare);
StringBuilder sb = null;
while (true) {
if(stopBeforeEnd) {
@ -153,6 +157,7 @@ public class TimeCalc {
// double done = ((double)totalMinutesDone)/((double)totalMinutes);
double done = ((double)totalMillisecondsDone)/((double)totalMilliseconds);
progressSquare.setDonePercent(done);
String msg = "Done=" + formatter.format(done * 100) + "% Remains=" + String.format("%02d", hourRemains) + ":" + String
.format("%02d", minuteRemains) + " (" + String.format("%03d", (hourRemains * 60 + minuteRemains)) + " minute" + ((hourRemains * 60 + minuteRemains) > 1 ? "s" : " ") + ")" + " End=" + String