Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,29 +1,27 @@
|
|||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.util.Calendar;
|
||||
import static java.lang.Math.*;
|
||||
import java.time.LocalTime;
|
||||
import javax.swing.*;
|
||||
|
||||
class Clock extends JPanel {
|
||||
|
||||
final float degrees06 = (float) Math.toRadians(6);
|
||||
final float degrees06 = (float) (PI / 30);
|
||||
final float degrees30 = degrees06 * 5;
|
||||
final float degrees90 = degrees30 * 3;
|
||||
|
||||
final int size = 550;
|
||||
final int spacing = 20;
|
||||
final int size = 590;
|
||||
final int spacing = 40;
|
||||
final int diameter = size - 2 * spacing;
|
||||
final int x = diameter / 2 + spacing;
|
||||
final int y = diameter / 2 + spacing;
|
||||
final int cx = diameter / 2 + spacing;
|
||||
final int cy = diameter / 2 + spacing;
|
||||
|
||||
public Clock() {
|
||||
setPreferredSize(new Dimension(size, size));
|
||||
setBackground(Color.white);
|
||||
|
||||
new Timer(1000, new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
repaint();
|
||||
}
|
||||
new Timer(1000, (ActionEvent e) -> {
|
||||
repaint();
|
||||
}).start();
|
||||
}
|
||||
|
||||
|
|
@ -34,46 +32,50 @@ class Clock extends JPanel {
|
|||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
g.setColor(Color.black);
|
||||
g.drawOval(spacing, spacing, diameter, diameter);
|
||||
drawFace(g);
|
||||
|
||||
Calendar date = Calendar.getInstance();
|
||||
int hours = date.get(Calendar.HOUR);
|
||||
int minutes = date.get(Calendar.MINUTE);
|
||||
int seconds = date.get(Calendar.SECOND);
|
||||
final LocalTime time = LocalTime.now();
|
||||
int hour = time.getHour();
|
||||
int minute = time.getMinute();
|
||||
int second = time.getSecond();
|
||||
|
||||
float angle = degrees90 - (degrees06 * seconds);
|
||||
float angle = degrees90 - (degrees06 * second);
|
||||
drawHand(g, angle, diameter / 2 - 30, Color.red);
|
||||
|
||||
float minsecs = (minutes + seconds / 60.0F);
|
||||
float minsecs = (minute + second / 60.0F);
|
||||
angle = degrees90 - (degrees06 * minsecs);
|
||||
drawHand(g, angle, diameter / 3 + 10, Color.black);
|
||||
|
||||
float hourmins = (hours + minsecs / 60.0F);
|
||||
float hourmins = (hour + minsecs / 60.0F);
|
||||
angle = degrees90 - (degrees30 * hourmins);
|
||||
drawHand(g, angle, diameter / 4 + 10, Color.black);
|
||||
}
|
||||
|
||||
private void drawFace(Graphics2D g) {
|
||||
g.setStroke(new BasicStroke(2));
|
||||
g.setColor(Color.white);
|
||||
g.fillOval(spacing, spacing, diameter, diameter);
|
||||
g.setColor(Color.black);
|
||||
g.drawOval(spacing, spacing, diameter, diameter);
|
||||
}
|
||||
|
||||
private void drawHand(Graphics2D g, float angle, int radius, Color color) {
|
||||
int x2 = x + (int) (radius * Math.cos(angle));
|
||||
int y2 = y + (int) (radius * Math.sin(-angle)); // flip y-axis
|
||||
int x = cx + (int) (radius * cos(angle));
|
||||
int y = cy - (int) (radius * sin(angle));
|
||||
g.setColor(color);
|
||||
g.drawLine(x, y, x2, y2);
|
||||
g.drawLine(cx, cy, x, y);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
JFrame f = new JFrame();
|
||||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
f.setTitle("Clock");
|
||||
f.setResizable(false);
|
||||
f.add(new Clock(), BorderLayout.CENTER);
|
||||
f.pack();
|
||||
f.setLocationRelativeTo(null);
|
||||
f.setVisible(true);
|
||||
}
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JFrame f = new JFrame();
|
||||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
f.setTitle("Clock");
|
||||
f.setResizable(false);
|
||||
f.add(new Clock(), BorderLayout.CENTER);
|
||||
f.pack();
|
||||
f.setLocationRelativeTo(null);
|
||||
f.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue