Last active
August 29, 2023 13:13
-
-
Save kishida/c495ea132ffac387dd4815ba9d3ca991 to your computer and use it in GitHub Desktop.
ChatGPTに作ってもらった時計
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
import java.util.Calendar; | |
class ClockPanel extends JPanel { | |
private final Timer timer; | |
private final int DELAY = 1000; // 1秒ごとに更新 | |
private Point initialClick; // ドラッグの開始地点を保存する | |
public ClockPanel() { | |
timer = new Timer(DELAY, ae -> repaint()); | |
timer.start(); | |
addMouseListener(new MouseAdapter() { | |
@Override | |
public void mousePressed(MouseEvent e) { | |
initialClick = e.getPoint(); | |
getComponentAt(initialClick); | |
} | |
}); | |
addMouseMotionListener(new MouseMotionAdapter() { | |
@Override | |
public void mouseDragged(MouseEvent e) { | |
// ドラッグ中にウィンドウの位置を更新 | |
int thisX = getTopLevelAncestor().getLocation().x; | |
int thisY = getTopLevelAncestor().getLocation().y; | |
// Determine how much the mouse moved since the initial click | |
int xMoved = e.getX() - initialClick.x; | |
int yMoved = e.getY() - initialClick.y; | |
// Move window to this position | |
int X = thisX + xMoved; | |
int Y = thisY + yMoved; | |
getTopLevelAncestor().setLocation(X, Y); | |
} | |
}); | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
drawClock(g); | |
} | |
private void drawMinuteMarks(Graphics g, int x, int y, int minute, double length) { | |
double angle = Math.toRadians(-minute * 6 + 90); // 描画のための角度を調整 | |
int endX = x + (int) (length * Math.cos(angle)); | |
int endY = y - (int) (length * Math.sin(angle)); | |
g.setColor(Color.BLACK); | |
if (minute % 5 == 0) { // 5の倍数であるかどうかを確認 | |
g.fillOval(endX - 4, endY - 4, 8, 8); // 5分ごとの点の大きさを8x8にする | |
} else { | |
g.fillOval(endX - 2, endY - 2, 4, 4); // 通常の1分ごとの点の大きさを4x4にする | |
} | |
} | |
private void drawClock(Graphics g) { | |
int centerX = getWidth() / 2; | |
int centerY = getHeight() / 2; | |
int clockRadius = Math.min(centerX, centerY) - 10; | |
Calendar now = Calendar.getInstance(); | |
int hour = now.get(Calendar.HOUR); | |
int minute = now.get(Calendar.MINUTE); | |
int second = now.get(Calendar.SECOND); | |
// 時針 | |
drawHand(g, centerX, centerY, hour % 12 * 30 + minute * 0.5, clockRadius * 0.5, 6); | |
// 分針 | |
drawHand(g, centerX, centerY, minute * 6 + second * 0.1, clockRadius * 0.65, 4); | |
// 秒針 | |
drawHand(g, centerX, centerY, second * 6, clockRadius * 0.75, 2); | |
// 中心の点 | |
g.setColor(Color.BLACK); | |
g.fillOval(centerX - 3, centerY - 3, 6, 6); | |
// 1分ごとの点 (5分ごとに強調) | |
for (int i = 0; i < 60; i++) { | |
drawMinuteMarks(g, centerX, centerY, i, clockRadius * 0.92); // 0.92は点を描く位置の半径を調整するための係数 | |
} | |
} | |
private void drawHand(Graphics g, int x, int y, double angle, double length, int thickness) { | |
angle = Math.toRadians(-angle + 90); | |
int endX = x + (int) (length * Math.cos(angle)); | |
int endY = y - (int) (length * Math.sin(angle)); | |
Graphics2D g2d = (Graphics2D) g; | |
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
g2d.setStroke(new BasicStroke(thickness, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); | |
g2d.drawLine(x, y, endX, endY); | |
} | |
public static void main(String[] args) { | |
JFrame frame = new JFrame("アナログ時計"); | |
ClockPanel clockPanel = new ClockPanel(); | |
frame.setUndecorated(true); // タイトルバーを消す | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setSize(400, 400); | |
frame.setLayout(new BorderLayout()); | |
frame.add(clockPanel, BorderLayout.CENTER); | |
frame.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment