2024-01-20 14:30:23 +00:00
|
|
|
package rvc.timecalc;
|
|
|
|
|
|
|
|
import javax.swing.JFrame;
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
import javax.swing.Timer;
|
|
|
|
import java.awt.BasicStroke;
|
|
|
|
import java.awt.Color;
|
|
|
|
import java.awt.Dimension;
|
|
|
|
import java.awt.Font;
|
|
|
|
import java.awt.Graphics;
|
|
|
|
import java.awt.Graphics2D;
|
|
|
|
import java.awt.RenderingHints;
|
2024-01-21 07:45:10 +00:00
|
|
|
import java.awt.event.MouseEvent;
|
|
|
|
import java.awt.event.MouseListener;
|
2024-01-27 13:40:23 +00:00
|
|
|
import java.io.IOException;
|
2024-01-20 14:30:23 +00:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
|
|
|
|
//https://kodejava.org/how-do-i-write-a-simple-analog-clock-using-java-2d/
|
|
|
|
public class AnalogClock extends JPanel {
|
|
|
|
|
2024-01-21 09:14:47 +00:00
|
|
|
private static final Color FOREGROUND_COLOR = new Color(220, 220, 220);
|
|
|
|
private static final Color BACKGROUND_COLOR = new Color(238, 238, 238);
|
2024-01-20 15:27:13 +00:00
|
|
|
|
2024-01-20 16:13:23 +00:00
|
|
|
private boolean highlight = false;
|
|
|
|
|
2024-01-20 14:30:23 +00:00
|
|
|
public AnalogClock() {
|
|
|
|
setPreferredSize(new Dimension(400, 300));
|
2024-01-20 15:27:13 +00:00
|
|
|
setBackground(BACKGROUND_COLOR);
|
2024-01-20 14:30:23 +00:00
|
|
|
new Timer(1000, e -> repaint()).start();
|
2024-01-21 07:45:10 +00:00
|
|
|
|
|
|
|
addMouseListener(new MouseListener() {
|
|
|
|
@Override
|
|
|
|
public void mouseClicked(MouseEvent e) {
|
|
|
|
highlight = !highlight;
|
2024-01-27 13:40:23 +00:00
|
|
|
if(highlight && !Utils.highlightTxt.exists()) {
|
|
|
|
try {
|
|
|
|
Utils.highlightTxt.createNewFile();
|
|
|
|
} catch (IOException ioException) {
|
|
|
|
System.out.println(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!highlight && Utils.highlightTxt.exists()) {
|
|
|
|
Utils.highlightTxt.delete();
|
|
|
|
}
|
2024-01-21 07:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mousePressed(MouseEvent e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mouseReleased(MouseEvent e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mouseEntered(MouseEvent e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mouseExited(MouseEvent e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
2024-01-20 14:30:23 +00:00
|
|
|
}
|
|
|
|
|
2024-01-21 09:14:47 +00:00
|
|
|
public static void main(String[] args) {
|
|
|
|
JFrame frame = new JFrame("Analog Clock");
|
|
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
frame.add(new AnalogClock());
|
|
|
|
frame.pack();
|
|
|
|
frame.setVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setHighlight(boolean highlight) {
|
|
|
|
this.highlight = highlight;
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:30:23 +00:00
|
|
|
@Override
|
|
|
|
public void paintComponent(Graphics g) {
|
|
|
|
super.paintComponent(g);
|
|
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
|
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|
|
|
RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
|
|
|
|
|
int side = Math.min(getWidth(), getHeight());
|
|
|
|
int centerX = getWidth() / 2;
|
|
|
|
int centerY = getHeight() / 2;
|
|
|
|
|
|
|
|
GregorianCalendar time = new GregorianCalendar();
|
|
|
|
int second = time.get(Calendar.SECOND);
|
|
|
|
int minute = time.get(Calendar.MINUTE);
|
|
|
|
int hour = time.get(Calendar.HOUR_OF_DAY);
|
|
|
|
|
|
|
|
drawHand(g2d, side / 2 - 10, second / 60.0, 0.5f, Color.RED);
|
2024-01-21 09:14:47 +00:00
|
|
|
drawHand(g2d, side / 2 - 20, minute / 60.0 + second / 60.0 / 60.0, 2.0f,
|
|
|
|
Color.BLUE);
|
|
|
|
drawHand(g2d, side / 2 - 40,
|
|
|
|
hour / 12.0 + minute / 60.0 / 12 + second / 60 / 60 / 12, 4.0f,
|
|
|
|
Color.BLACK);
|
2024-01-20 14:30:23 +00:00
|
|
|
|
|
|
|
// Draw clock numbers and circle
|
|
|
|
drawClockFace(g2d, centerX, centerY, side / 2 - 40);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void drawHand(Graphics2D g2d, int length, double value,
|
|
|
|
float stroke, Color color) {
|
2024-01-21 09:14:47 +00:00
|
|
|
length = length - 4;
|
2024-01-20 14:30:23 +00:00
|
|
|
double angle = Math.PI * 2 * (value - 0.25);
|
|
|
|
int endX = (int) (getWidth() / 2 + length * Math.cos(angle));
|
|
|
|
int endY = (int) (getHeight() / 2 + length * Math.sin(angle));
|
|
|
|
|
2024-01-21 07:18:13 +00:00
|
|
|
g2d.setColor(highlight ? color : FOREGROUND_COLOR);
|
2024-01-20 14:30:23 +00:00
|
|
|
g2d.setStroke(new BasicStroke(stroke));
|
2024-01-20 15:27:13 +00:00
|
|
|
g2d.drawLine(getWidth() / 2, getHeight() / 2, endX, endY);
|
2024-01-20 14:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void drawClockFace(Graphics2D g2d, int centerX, int centerY,
|
|
|
|
int radius) {
|
|
|
|
g2d.setStroke(new BasicStroke(2.0f));
|
2024-01-21 07:18:13 +00:00
|
|
|
g2d.setColor(highlight ? Color.BLACK : FOREGROUND_COLOR);
|
2024-01-21 09:14:47 +00:00
|
|
|
// System.out.println("centerX=" + centerX);
|
|
|
|
// System.out.println("centerY=" + centerY);
|
|
|
|
// System.out.println("radius=" + radius);
|
2024-01-20 15:27:13 +00:00
|
|
|
g2d.drawOval(1, 1, centerX * 2 - 4, centerY * 2 - 4);
|
2024-01-20 14:30:23 +00:00
|
|
|
g2d.drawOval(2, 2, centerX * 2 - 4, centerY * 2 - 4);
|
2024-01-21 09:14:47 +00:00
|
|
|
// g2d.drawOval(3, 3, centerX * 2 - 6, centerY * 2 - 6);
|
|
|
|
// g2d.drawOval(4, 4, centerX * 2 - 8, centerY * 2 - 8);
|
2024-01-20 14:30:23 +00:00
|
|
|
|
|
|
|
for (int i = 1; i <= 12; i++) {
|
|
|
|
double angle = Math.PI * 2 * (i / 12.0 - 0.25);
|
|
|
|
int dx = centerX + (int) ((radius + 20) * Math.cos(angle)) - 4;
|
|
|
|
int dy = centerY + (int) ((radius + 20) * Math.sin(angle)) + 4;
|
|
|
|
|
|
|
|
g2d.setFont(new Font("sans", Font.BOLD, 16));
|
|
|
|
g2d.drawString(Integer.toString(i), dx, dy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|