비주얼 프로그래밍
6주차 C# 아날로그 시계(1)
23615038 이서경
2024. 4. 17. 00:30
더보기

# 아날로그 시계만들기

도구 상자에서
컨테이너 -> Panel 1개
메뉴 및 도구 모음 -> MenuStrip 1개
배치 합니다
private 중심점, 반지름, 시치의 길이, 분침, 초침을 필드로 만들어 냅니다.
또한 클라이언트, 시계 사이즈를 선언 합니다.
public partial class Form1 : Form
{
// 필드<맴버 변수>
Graphics g;
bool aClock_Flag = true; //아날로그 시계
private Point center; //중심점
private int radius; //반지름
private int hourHand; // 시침의 길이
private int minHand; // 분침의 길이
private int secHand; //초침의 길이
private const int clientSize = 300; //client의 크기
private const int clockSize = 200; // 시계의 크기
this.text로 Form의 이름을 정해주고
바탕색을 설정해 줍니다
//생성자 함수
public Form1()
{
InitializeComponent();
this.ClientSize = new Size(clientSize, clientSize + menuStrip1.Height);
this.Text = "My Form CLock";
//panel 지정
panel1.BackColor = Color.WhiteSmoke;
panel1.Width = clientSize +1;
panel1.Height = clientSize +1;
panel1.Location = new Point(
clientSize / 2 - clockSize / 2,
clientSize / 2 - clockSize / 2 + menuStrip1.Height);
g = panel1.CreateGraphics(); // panel1에 그려주는 그래픽 객체
aClockSetting(); // 아날로그 시계 세팅
dClockSetting(); // 디지털 시계 세팅
TimerSetting(); // 타이머 세팅
}
aClockSetting()
center와 radius 먼저 설정을 하고
hourhand, minhand, sechand 얼마큼 움직일 것인지 설정해줍니다.
private void aClockSetting() // 아날로그 시계 세팅
{
center = new Point(clientSize / 2, clockSize / 2);
radius = clientSize / 2;
hourHand = (int)(radius * 0.45);
minHand = (int)(radius * 0.55);
secHand = (int)(radius * 0.65);
}
TimerSetting()
1초에 한 번씩 시계를 그려 주기 위해 T_Tick() 함수를 생성합니다.
private void TimerSetting() //타이머 세팅
{
Timer t = new Timer();
t.Enabled = true;
t.Interval = 1000;
t.Tick += T_Tick;
}
private void T_Tick(object sender, EventArgs e)
{
throw new NotImplementedException();
}
위의 form 코드에 아래의 코드를 추가해 줍니다
private Font fDate;
private Font tFont;
private Brush bDate;
private Brush bTime;
private Point panelCenter;
위의 코드에서 panel을 지정해 줍니다
//panel 지정
panel1.BackColor = Color.WhiteSmoke;
panel1.Width = clientSize +1;
panel1.Height = clientSize +1;
panel1.Location = new Point(
clientSize / 2 - clockSize / 2,
clientSize / 2 - clockSize / 2 + menuStrip1.Height);
# 디지털 시계
private void T_Tick(object sender, EventArgs e)
{
DateTime c = DateTime.Now; // 현재 시간
else // 디지털 시계
{
string date = DateTime.Today.ToString("D");
string time = String.Format("{0:D2};{1:D2}:{2:D3}", c.Hour, c.Minute, c.Second);
g.DrawString(date, fDate, bDate, new Point(10, 80));
g.DrawString(time, fTime, bTime, new Point(10, 120));
}
#아날로그 시계
if (aClockFlag == true) // 아날로그 시계 ( 시작할때 true로 설정해줌)
{
DrawClockFace();
//시계바늘 각도
double radHr = (c.Hour % 12 + c.Minute / 60.0) * 30 * Math.PI / 180;
double radMin = (c.Minute + c.Second / 60.0) * 6 * Math.PI / 180;
double radSec = c.Second * 6 * Math.PI / 180;
DrawHands(radHr, radMin, radSec);
DrawClockFace와 DrawHands의 메서드를 생성
//DrawClockFace 코드
private void DrawClockFace()
{
const int penWidth = 30;
Pen p = new Pen(Brushes.LightGray, penWidth);
g.DrawEllipse(p, penWidth/2, penWidth / 2,
clockSize - penWidth , clockSize-penWidth); // (사각형 지정)
}
public partial class Form1 : Form // this
{
// 필드(멤버변수)
private Graphics g;
private bool aClockFlag = true; // 아날로그 시계 (false- 디지털 시계)
# 지금까지 한 코드 정리
public partial class Form1 : Form
{
// 필드<맴버 변수>
Graphics g;
bool aClock_Flag = true; //아날로그 시계
private Point center; //중심점
private int radius; //반지름
private int hourHand; // 시침의 길이
private int minHand; // 분침의 길이
private int secHand; //초침의 길이
private const int clientSize = 300; //client의 크기
private const int clockSize = 200; // 시계의 크기
private Font fDate;
private Font fTime;
private Brush bDate;
private Brush bTime;
private Point panelCenter;
//생성자 함수
public Form1()
{
InitializeComponent();
this.ClientSize = new Size(clientSize, clientSize + menuStrip1.Height);
this.Text = "My Form CLock";
//panel 지정
panel1.BackColor = Color.WhiteSmoke;
panel1.Width = clientSize +1;
panel1.Height = clientSize +1;
panel1.Location = new Point(
clientSize / 2 - clockSize / 2,
clientSize / 2 - clockSize / 2 + menuStrip1.Height);
g = panel1.CreateGraphics(); // panel1에 그려주는 그래픽 객체
aClockSetting(); // 아날로그 시계 세팅
dClockSetting(); // 디지털 시계 세팅
TimerSetting(); // 타이머 세팅
}
private void aClockSetting()
{
center = new Point(clientSize / 2, clockSize / 2);
radius = clientSize / 2;
hourHand = (int)(radius * 0.45);
minHand = (int)(radius * 0.55);
secHand = (int)(radius * 0.65);
}
private void dClockSetting()
{
Font fDate = new Font("맑은 고딕", 12, FontStyle.Bold);
Font fTime = new Font("맑은 고딕", 32,
FontStyle.Bold | FontStyle.Italic);
Brush bDate = Brushes.SkyBlue;
Brush bTime = Brushes.SteelBlue;
}
private void TimerSetting()
{
Timer t = new Timer();
t.Enabled = true;
t.Interval = 1000;
t.Tick += T_Tick;
}
private void T_Tick(object sender, EventArgs e)
{
DateTime c = DateTime.Now; //현재시간
panel1.Refresh();//패널을 지운다
if (aClock_Flag == true) // 아날로그
{
DrawClockFace();
//시계 바늘 각도
double radHr = (c.Hour % 12 + c.Minute / 60.0) * 30 * Math.PI / 180;
double radMin = (c.Minute + c.Second / 60.0) * 6 * Math.PI / 180;
double radSec = c.Second * 6 * Math.PI / 180;
DrawHands(radHr, radMin, radSec);
}
else // 디지털 시계
{
string date = DateTime.Today.ToString("D");
string time = string.Format("{0:D2}:{1:D2}:{2:D2}",
c.Hour, c.Minute, c.Second);
g.DrawString(date, fDate, bDate, new Point(5, 70));
g.DrawString(time, fTime, bTime, new Point(5, 100));
}
}
private void DrawClockFace()
{
const int penWidth = 30;
Pen p = new Pen(Brushes.Blue, penWidth);
g.DrawEllipse(p, penWidth/2, penWidth/3,
clockSize - penWidth, clockSize - penWidth);
}
private void DrawHands(double radHr, double radMin, double radSec)
{
}
}
}