143 lines
5.9 KiB
Java
Raw Normal View History

2024-01-27 16:50:19 +00:00
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;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
2024-01-27 17:56:55 +00:00
import java.text.DecimalFormat;
import java.text.NumberFormat;
2024-01-27 16:50:19 +00:00
public class Battery extends JPanel {
2024-01-27 18:14:59 +00:00
public static final Color LOW = new Color(253, 130, 130);
2024-01-27 16:50:19 +00:00
public static final Color MEDIUM = new Color(255, 204, 153);
public static final Color HIGH = new Color(204, 255, 204);
public static final Color HIGHEST = new Color(153, 255, 153);
2024-01-27 17:41:31 +00:00
public static final Color LOW_HIGHLIGHTED = Color.red;
public static final Color MEDIUM_HIGHLIGHTED = Color.ORANGE;
public static final Color HIGH_HIGHLIGHTED = new Color(158, 227, 158);
public static final Color HIGHEST_HIGHLIGHTED = Color.green;
2024-01-27 23:41:48 +00:00
private static final Color FOREGROUND_COLOR = new Color(220, 220, 220);
private static final Color BACKGROUND_COLOR = new Color(238, 238, 238);
NumberFormat formatter3 = new DecimalFormat("#0.000");
2024-01-28 14:17:30 +00:00
private int totalHeight = 0;
2024-01-27 16:50:19 +00:00
private double donePercent = 0;
2024-01-27 23:16:01 +00:00
private boolean mouseOver = false;
2024-01-27 16:50:19 +00:00
private int width_;
public Battery() {
setPreferredSize(new Dimension(40, 100));
setBackground(BACKGROUND_COLOR);
2024-01-28 14:17:30 +00:00
new Timer(250, e -> repaint()).start();
2024-01-27 16:50:19 +00:00
addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
2024-01-27 19:40:45 +00:00
Utils.highlighted.flip();
2024-01-27 16:50:19 +00:00
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
2024-01-27 23:16:01 +00:00
mouseOver = true;
2024-01-27 16:50:19 +00:00
}
@Override
public void mouseExited(MouseEvent e) {
2024-01-27 23:16:01 +00:00
mouseOver = false;
2024-01-27 16:50:19 +00:00
}
});
}
public void setDonePercent(double donePercent) {
this.donePercent = donePercent;
}
@Override
public void paintComponent(Graphics g) {
2024-01-28 14:17:30 +00:00
if (totalHeight == 0) {
this.totalHeight = Math.min(getWidth(), getHeight());
this.width_ = (int) (this.totalHeight * 0.6);
2024-01-27 16:50:19 +00:00
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
2024-01-27 23:41:48 +00:00
g2d.setColor(Utils.highlighted.get() || mouseOver ? Color.YELLOW :
FOREGROUND_COLOR);
2024-01-27 16:50:19 +00:00
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
2024-01-28 14:17:30 +00:00
g2d.fillRect(width_ / 4, 1, width_, totalHeight - 2);
2024-01-27 23:41:48 +00:00
g2d.setColor(Utils.highlighted.get() || mouseOver ? Color.BLACK :
Color.LIGHT_GRAY);
2024-01-28 14:17:30 +00:00
g2d.drawRect(width_ / 4 - 1, 0, width_ + 1, totalHeight + 0);
2024-01-27 23:41:48 +00:00
if (Utils.highlighted.get() || mouseOver) {
g2d.setColor(
donePercent < 0.1 ? LOW_HIGHLIGHTED : (donePercent < 0.75 ?
MEDIUM_HIGHLIGHTED :
(donePercent < 0.9 ? HIGH_HIGHLIGHTED :
2024-01-28 14:17:30 +00:00
HIGHEST_HIGHLIGHTED)));
2024-01-27 17:41:31 +00:00
} else {
g2d.setColor(donePercent < 0.1 ? LOW : (donePercent < 0.75 ?
MEDIUM : (donePercent < 0.9 ? HIGH : HIGHEST)));
}
2024-01-28 14:17:30 +00:00
int doneHeight = (int) (totalHeight * donePercent);
int intX = width_ / 4;
int todoHeight = totalHeight - doneHeight;
double surfacePower = 1;//donePercent < 0.5 ? 0.5 : donePercent;// (donePercent * 100 - ((int)(donePercent * 100)));
int waterSurfaceHeight = (int) (4 * surfacePower);//2 + (int) (Math.random() * 3);
if(waterSurfaceHeight <= 2) {
waterSurfaceHeight = 0;
}
g2d.fillRect(intX, doneHeight < waterSurfaceHeight || donePercent >= 1 ? todoHeight : todoHeight + waterSurfaceHeight,
width_, doneHeight < waterSurfaceHeight || donePercent >= 1 ? doneHeight : doneHeight - waterSurfaceHeight);
int pointCount = 8;
if(doneHeight >= waterSurfaceHeight && donePercent < 1) {// && todoHeight > waterSurfaceHeight) {
//g2d.fillArc(intX, intY, width_, intHeight - waterSurfaceHeight, 30, 60);
g2d.fillPolygon(
new int[]{intX,
(int) (intX + width_ / pointCount * 0.5),
intX + width_ / pointCount * 3,
intX + width_ / pointCount * 4,
intX + width_ / pointCount * 5,
intX + width_ / pointCount * 6,
(int) (intX + width_ / pointCount * 7.9),
intX + width_ / pointCount * 8},
new int[]{(int) (todoHeight + (waterSurfaceHeight * 1)),
todoHeight + (int) (waterSurfaceHeight * getRandom(0)),
todoHeight + (int) (waterSurfaceHeight * getRandom(1)),
todoHeight + (int) (waterSurfaceHeight * getRandom(2)),
todoHeight + (int) (waterSurfaceHeight * getRandom(3)),
todoHeight + (int) (waterSurfaceHeight * getRandom(4)),
todoHeight + (int) (waterSurfaceHeight * getRandom(5)),
(int) (todoHeight + (waterSurfaceHeight * 1))},
pointCount);
}
2024-01-27 23:41:48 +00:00
g2d.setColor(Utils.highlighted.get() || mouseOver ? Color.BLACK :
Color.LIGHT_GRAY);
2024-01-27 17:56:55 +00:00
g2d.drawString(
2024-01-27 23:41:48 +00:00
formatter3.format(donePercent * 100) + "%",
2024-01-28 14:17:30 +00:00
((int) (width_ * 0.4)), donePercent > 0.5 ? totalHeight / 4 * 3 : totalHeight / 4 * 1);
2024-01-27 16:50:19 +00:00
}
2024-01-28 14:17:30 +00:00
private double[] randomDoubles = new double[]{1d,1d,1d,1d,1d,1d,1};
private double getRandom(int index) {
if(Math.random() > 0.7) {randomDoubles[index] = Math.random();}
return randomDoubles[index];
}
2024-01-27 16:50:19 +00:00
}