Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
Lab3
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mrsl2000
Lab3
Commits
5b7ab442
Commit
5b7ab442
authored
Mar 11, 2019
by
mrsl2000
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clock ui test
parents
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
261 additions
and
0 deletions
+261
-0
ClockPanel.java
src/org/clock/ui/ClockPanel.java
+155
-0
ClockUI.java
src/org/clock/ui/ClockUI.java
+95
-0
ClockUITest.java
src/test/ClockUITest.java
+11
-0
No files found.
src/org/clock/ui/ClockPanel.java
0 → 100644
View file @
5b7ab442
package
org
.
clock
.
ui
;
import
javax.swing.*
;
import
java.awt.*
;
class
ClockPanel
extends
JPanel
{
/**
* The coordinates used to paint the clock hands.
*/
private
int
xHandSec
,
yHandSec
,
xHandMin
,
yHandMin
,
xHandHour
,
yHandHour
;
/**
* The size of the clock.
*/
private
final
int
HORIZONTAL_SIZE
=
500
;
private
final
int
VERTICAL_SIZE
=
500
;
private
final
static
Color
GRAY_COLOR
=
new
Color
(
160
,
160
,
160
);
private
static
final
Color
BACKGROUND_COLOR
=
new
Color
(
24
,
116
,
205
);
private
int
second
;
private
int
minute
;
private
int
hour
;
/**
* creates clock container, initialize background and size
*/
ClockPanel
()
{
setMinimumSize
(
new
Dimension
(
HORIZONTAL_SIZE
,
VERTICAL_SIZE
));
setMaximumSize
(
new
Dimension
(
HORIZONTAL_SIZE
,
VERTICAL_SIZE
));
setPreferredSize
(
new
Dimension
(
HORIZONTAL_SIZE
,
VERTICAL_SIZE
));
setBackground
(
BACKGROUND_COLOR
);
setLayout
(
null
);
}
/**
* set seconds
*
* @param second second value
*/
void
setSecond
(
int
second
)
{
this
.
second
=
second
;
}
/**
* set minute
*
* @param minute minute value
*/
void
setMinute
(
int
minute
)
{
this
.
minute
=
minute
;
}
/**
* set hour
*
* @param hour hour value
*/
void
setHour
(
int
hour
)
{
this
.
hour
=
hour
;
}
/**
* recalculates the coordinates of the clock hands,
* and repaint everything.
*/
void
draw
()
{
int
secondHandLength
=
HORIZONTAL_SIZE
/
2
-
50
;
xHandSec
=
minToLocation
(
second
,
secondHandLength
).
x
;
yHandSec
=
minToLocation
(
second
,
secondHandLength
).
y
;
int
minuteHandLength
=
HORIZONTAL_SIZE
/
2
-
70
;
xHandMin
=
minToLocation
(
minute
,
minuteHandLength
).
x
;
yHandMin
=
minToLocation
(
minute
,
minuteHandLength
).
y
;
int
hourHandLength
=
HORIZONTAL_SIZE
/
2
-
100
;
xHandHour
=
minToLocation
(
hour
*
5
+
getRelativeHour
(
hour
),
hourHandLength
).
x
;
yHandHour
=
minToLocation
(
hour
*
5
+
getRelativeHour
(
hour
),
hourHandLength
).
y
;
repaint
();
}
/**
* Returns how much the hour hand should be ahead
* according to the minutes value.
* 04:00, return 0.
* 04:12, return 1, so that we move the hour handle ahead of one dot.
*
* @param min The current minute.
* @return The relative offset to add to the hour hand.
*/
private
int
getRelativeHour
(
int
min
)
{
return
min
/
12
;
}
/**
* paint clock hands
*
* @param g don't touch it!
*/
protected
void
paintComponent
(
Graphics
g
)
{
Graphics2D
g2
=
(
Graphics2D
)
g
;
g2
.
setRenderingHint
(
RenderingHints
.
KEY_ANTIALIASING
,
RenderingHints
.
VALUE_ANTIALIAS_ON
);
g2
.
setColor
(
BACKGROUND_COLOR
);
g2
.
fillRect
(
0
,
0
,
getWidth
(),
getHeight
());
// Draw the dots
g2
.
setColor
(
GRAY_COLOR
);
for
(
int
i
=
0
;
i
<
60
;
i
++)
{
int
DISTANCE_DOT_FROM_ORIGIN
=
HORIZONTAL_SIZE
/
2
-
40
;
Point
dotCoordinates
=
minToLocation
(
i
,
DISTANCE_DOT_FROM_ORIGIN
);
g2
.
setColor
((
i
<=
second
?
Color
.
white
:
GRAY_COLOR
));
if
(
i
%
5
==
0
)
{
// big dot
int
DIAMETER_BIG_DOT
=
8
;
g2
.
fillOval
(
dotCoordinates
.
x
-
(
DIAMETER_BIG_DOT
/
2
),
dotCoordinates
.
y
-
(
DIAMETER_BIG_DOT
/
2
),
DIAMETER_BIG_DOT
,
DIAMETER_BIG_DOT
);
}
else
{
// small dot
int
DIAMETER_SMALL_DOT
=
4
;
g2
.
fillOval
(
dotCoordinates
.
x
-
(
DIAMETER_SMALL_DOT
/
2
),
dotCoordinates
.
y
-
(
DIAMETER_SMALL_DOT
/
2
),
DIAMETER_SMALL_DOT
,
DIAMETER_SMALL_DOT
);
}
}
// Draw the clock hands
g2
.
setColor
(
Color
.
white
);
g2
.
drawLine
(
HORIZONTAL_SIZE
/
2
,
VERTICAL_SIZE
/
2
,
xHandSec
,
yHandSec
);
g2
.
setStroke
(
new
BasicStroke
(
3
));
g2
.
drawLine
(
HORIZONTAL_SIZE
/
2
,
VERTICAL_SIZE
/
2
,
xHandMin
,
yHandMin
);
g2
.
setStroke
(
new
BasicStroke
(
5
));
g2
.
drawLine
(
HORIZONTAL_SIZE
/
2
,
VERTICAL_SIZE
/
2
,
xHandHour
,
yHandHour
);
}
/**
* Converts current second/minute/hour to x and y coordinates.
*
* @param timeStep The current second/minute/hour
* @param radius The radius length
* @return the coordinates point
*/
private
Point
minToLocation
(
int
timeStep
,
int
radius
)
{
double
t
=
2
*
Math
.
PI
*
(
timeStep
-
15
)
/
60
;
int
x
=
(
int
)
(
HORIZONTAL_SIZE
/
2
+
radius
*
Math
.
cos
(
t
));
int
y
=
(
int
)
(
VERTICAL_SIZE
/
2
+
radius
*
Math
.
sin
(
t
));
return
new
Point
(
x
,
y
);
}
}
\ No newline at end of file
src/org/clock/ui/ClockUI.java
0 → 100644
View file @
5b7ab442
package
org
.
clock
.
ui
;
/**
* ClockUI is a simple package for drawing ClockUI!
* <p>
* Thanks to Paolo Boschini for his <a href="https://github.com/paoloboschini/analog-clock/blob/master/Clock.java">code</a>!
* <p>
* @author MohammadHossein Hessar
*/
import
java.awt.BorderLayout
;
import
java.awt.Dimension
;
import
javax.swing.JFrame
;
public
class
ClockUI
extends
JFrame
{
private
ClockPanel
container
;
public
ClockUI
(
int
hour
,
int
minute
,
int
second
){
container
=
new
ClockPanel
();
setClock
(
hour
,
minute
,
second
);
add
(
container
,
BorderLayout
.
CENTER
);
setDefaultCloseOperation
(
EXIT_ON_CLOSE
);
setResizable
(
false
);
setSize
(
500
,
500
);
setMinimumSize
(
new
Dimension
(
500
,
500
));
setLocationRelativeTo
(
null
);
pack
();
setVisible
(
true
);
}
public
ClockUI
()
{
this
(
0
,
0
,
0
);
}
/**
* set time to display
* do nothing when values are out of range!
*
* @param hour hour value
* @param minute minute value
* @param second second value
*/
public
void
setClock
(
int
hour
,
int
minute
,
int
second
)
{
if
(
hour
>
24
||
hour
<
0
)
return
;
if
(
minute
<
0
||
minute
>
60
)
return
;
if
(
second
<
0
||
second
>
60
)
return
;
container
.
setHour
(
hour
);
container
.
setMinute
(
minute
);
container
.
setSecond
(
second
);
container
.
draw
();
}
/**
* set hour to display
* do nothing when values are out of range!
*
* @param hour hour value
*/
public
void
setHour
(
int
hour
)
{
if
(
hour
>
24
||
hour
<
0
)
return
;
container
.
setHour
(
hour
);
container
.
draw
();
}
/**
* set minute to display
* do nothing when values are out of range!
*
* @param minute minute value
*/
public
void
setMinute
(
int
minute
)
{
if
(
minute
<
0
||
minute
>
60
)
return
;
container
.
setMinute
(
minute
);
container
.
draw
();
}
/**
* set second to display
* do nothing when values are out of range!
*
* @param second second value
*/
public
void
setSecond
(
int
second
)
{
if
(
second
<
0
||
second
>
60
)
return
;
container
.
setSecond
(
second
);
container
.
draw
();
}
}
src/test/ClockUITest.java
0 → 100644
View file @
5b7ab442
package
test
;
import
org.clock.ui.ClockUI
;
public
class
ClockUITest
{
public
static
void
main
(
String
args
[])
{
ClockUI
clock
=
new
ClockUI
();
clock
.
setHour
(
22
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment