대학수업/프로그램설계방법론

[숙제#3] 프로그램 설계 방법론(아날로그 시계)

MIRIP 2022. 9. 30. 23:59
반응형

언어 : JAVA

과제 : 아날로그 시계에 초가 지남에 따라 동심원이 커지도록 프로그래밍 하시오.

숙제#1과 숙제 #2는 이미 제출한 관계로 작성하지 않겠다.

import java.awt.Color;
import java.awt.Graphics;
import java.time.LocalTime;

import javax.swing.*;

public class ClockWriter extends JPanel{
	
	private final int SIZE;
	private final int MARGIN;
	private int diameter;
	
    public ClockWriter(int _s, int _rate) {
    	SIZE = _s;
    	MARGIN = SIZE/_rate;
        JFrame frame = new JFrame();
        frame.setTitle("Clock");
        frame.setSize(SIZE+50, SIZE+150);
        frame.getContentPane().add(this);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLUE);
        g.drawString("TIME IS GOLD", 105, 50);
        g.setColor(Color.LIGHT_GRAY);
        g.fillOval(25, 100, SIZE, SIZE);
        //현재 시각
        LocalTime now = LocalTime.now();
        int radius = SIZE/2;
        int x1 = 25 + radius;
        int y1 = 100 + radius;
        //동심원
        diameter = SIZE*now.getSecond()/60;
        int inc = (SIZE-diameter)/2;
        g.setColor(Color.pink);
        g.fillOval(25+inc, 100+inc, diameter, diameter);
        
        //분침
        radius -=30;
        double minute_angle = (now.getMinute()-15) * Math.PI / 30;
        int x2 = x1 + (int)(radius*Math.cos(minute_angle));
        int y2 = y1 + (int)(radius*Math.sin(minute_angle));
        g.setColor(Color.red);
        g.drawLine(x1, y1, x2, y2);
        //시침
        radius -= 30;
        double hour_angle = (now.getHour() - 3) * Math.PI / 6 + now.getMinute() * Math.PI / 30/12;
        x2 = x1 + (int)(radius*Math.cos(hour_angle));
        y2 = y1 + (int)(radius*Math.sin(hour_angle));
        g.setColor(Color.yellow);
        g.drawLine(x1, y1, x2, y2);
    }
	public static void main(String[] args) {
		new ClockWriter(250, 5);
		return;
	}
}
728x90
반응형