Skip to content

Instantly share code, notes, and snippets.

@nguthiru
Created July 21, 2024 15:58
Show Gist options
  • Save nguthiru/bad594bf06d3a6c1b840ee51d24eb4c2 to your computer and use it in GitHub Desktop.
Save nguthiru/bad594bf06d3a6c1b840ee51d24eb4c2 to your computer and use it in GitHub Desktop.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.Calendar;
public class WallClockAnimation extends JPanel {
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
private static final int CLOCK_RADIUS = 150;
private double scaleFactor = 1.0;
private boolean growing = true;
public WallClockAnimation() {
Timer timer = new Timer(1000, e -> {
updateTransformations();
repaint();
});
timer.start();
}
private void updateTransformations() {
// Scaling transformation
if (growing) {
scaleFactor += 0.01;
if (scaleFactor >= 1.2) growing = false;
} else {
scaleFactor -= 0.01;
if (scaleFactor <= 1.0) growing = true;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int centerX = WIDTH / 2;
int centerY = HEIGHT / 2;
// Apply scaling transformation
g2d.translate(centerX, centerY);
g2d.scale(scaleFactor, scaleFactor);
g2d.translate(-centerX, -centerY);
// Draw clock face
g2d.setColor(Color.WHITE);
g2d.fillOval(centerX - CLOCK_RADIUS, centerY - CLOCK_RADIUS, CLOCK_RADIUS * 2, CLOCK_RADIUS * 2);
g2d.setColor(Color.BLACK);
g2d.drawOval(centerX - CLOCK_RADIUS, centerY - CLOCK_RADIUS, CLOCK_RADIUS * 2, CLOCK_RADIUS * 2);
// Draw hour markers
for (int i = 0; i < 12; i++) {
double angle = Math.toRadians(i * 30 - 90);
int x1 = (int) (centerX + (CLOCK_RADIUS - 20) * Math.cos(angle));
int y1 = (int) (centerY + (CLOCK_RADIUS - 20) * Math.sin(angle));
int x2 = (int) (centerX + CLOCK_RADIUS * Math.cos(angle));
int y2 = (int) (centerY + CLOCK_RADIUS * Math.sin(angle));
g2d.drawLine(x1, y1, x2, y2);
}
// Get current time
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
// Draw clock hands
drawHand(g2d, centerX, centerY, hour * 30 + minute / 2, CLOCK_RADIUS * 0.5, 6, Color.BLACK);
drawHand(g2d, centerX, centerY, minute * 6, CLOCK_RADIUS * 0.7, 4, Color.BLUE);
drawHand(g2d, centerX, centerY, second * 6, CLOCK_RADIUS * 0.9, 2, Color.RED);
}
private void drawHand(Graphics2D g2d, int centerX, int centerY, double angle, double length, int width, Color color) {
// Rotation transformation
AffineTransform oldTransform = g2d.getTransform();
g2d.rotate(Math.toRadians(angle), centerX, centerY);
// Translation transformation
g2d.setColor(color);
g2d.setStroke(new BasicStroke(width));
g2d.drawLine(centerX, centerY, centerX, centerY - (int) length);
g2d.setTransform(oldTransform);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Wall Clock Animation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.add(new WallClockAnimation());
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment