Added analog clock
This commit is contained in:
parent
3913a32d5d
commit
4f4732fee6
91
src/main/java/rvc/timecalc/AnalogClock.java
Normal file
91
src/main/java/rvc/timecalc/AnalogClock.java
Normal file
@ -0,0 +1,91 @@
|
||||
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;
|
||||
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 {
|
||||
|
||||
public AnalogClock() {
|
||||
setPreferredSize(new Dimension(400, 300));
|
||||
setBackground(new Color(240,240,240));
|
||||
new Timer(1000, e -> repaint()).start();
|
||||
}
|
||||
|
||||
@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);
|
||||
drawHand(g2d, side / 2 - 20, minute / 60.0, 2.0f, Color.BLUE);
|
||||
drawHand(g2d, side / 2 - 40, hour / 12.0, 4.0f, Color.BLACK);
|
||||
|
||||
// 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) {
|
||||
length= length - 4;
|
||||
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));
|
||||
|
||||
g2d.setColor(Color.LIGHT_GRAY);
|
||||
g2d.setStroke(new BasicStroke(stroke));
|
||||
g2d.drawLine(getWidth() / 2 - 4, getHeight() / 2 + 4, endX - 4, endY + 4);
|
||||
}
|
||||
|
||||
// Added method to draw the clock face and numbers
|
||||
private void drawClockFace(Graphics2D g2d, int centerX, int centerY,
|
||||
int radius) {
|
||||
g2d.setStroke(new BasicStroke(2.0f));
|
||||
g2d.setColor(Color.LIGHT_GRAY);
|
||||
System.out.println("centerX=" + centerX);
|
||||
System.out.println("centerY=" + centerY);
|
||||
System.out.println("radius=" + radius);
|
||||
g2d.drawOval(1, 1, centerX * 2 - 2, centerY * 2 - 2);
|
||||
g2d.drawOval(2, 2, centerX * 2 - 4, centerY * 2 - 4);
|
||||
// g2d.drawOval(3, 3, centerX * 2 - 6, centerY * 2 - 6);
|
||||
// g2d.drawOval(4, 4, centerX * 2 - 8, centerY * 2 - 8);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -19,7 +19,7 @@ public class Main {
|
||||
JOptionPane.PLAIN_MESSAGE,
|
||||
null,
|
||||
null,
|
||||
""
|
||||
"7:00"
|
||||
);
|
||||
String overTime = (String) JOptionPane.showInputDialog(
|
||||
null,
|
||||
|
@ -79,7 +79,7 @@ public class TimeCalc {
|
||||
text.setBackground(new Color(238,238,238));
|
||||
window.add(text);
|
||||
|
||||
window.setSize(550,350);
|
||||
window.setSize(800,350);
|
||||
window.setLayout(null);
|
||||
window.setVisible(true);
|
||||
window.setTitle("Time Calc");
|
||||
@ -94,6 +94,9 @@ public class TimeCalc {
|
||||
exitButton.addActionListener(e -> System.exit(0));
|
||||
restartButton.addActionListener(e -> {window.setVisible(false); stopBeforeEnd = true;});
|
||||
|
||||
AnalogClock analogClock = new AnalogClock();
|
||||
analogClock.setBounds(550,10,200, 200);
|
||||
window.add(analogClock);
|
||||
|
||||
StringBuilder sb = null;
|
||||
while (true) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user