Java Reference
In-Depth Information
34
public int getMinute() {
35
return minute;
36 }
37
38 /** Set a new minute */
39 public void setMinute( int minute) {
40 this .minute = minute;
41 repaint();
42 }
43
44
repaint panel
/** Return second */
45
public int getSecond() {
46
return second;
47 }
48
49 /** Set a new second */
50 public void setSecond( int second) {
51 this .second = second;
52 repaint();
53 }
54
55 @Override /** Draw the clock */
56
repaint panel
protected void paintComponent(Graphics g) {
override paintComponent
57
super .paintComponent(g);
58
59 // Initialize clock parameters
60 int clockRadius =
61 ( int )(Math.min(getWidth(), getHeight()) * 0.8 * 0.5 );
62
int xCenter = getWidth() / 2 ;
63
int yCenter = getHeight() / 2 ;
64
65 // Draw circle
66 g.setColor(Color.BLACK);
67 g.drawOval(xCenter - clockRadius, yCenter - clockRadius,
68 2 * clockRadius, 2 * clockRadius);
69 g.drawString( "12" , xCenter - 5 , yCenter - clockRadius + 12 );
70 g.drawString( "9" , xCenter - clockRadius + 3 , yCenter + 5 );
71 g.drawString( "3" , xCenter + clockRadius - 10 , yCenter + 3 );
72 g.drawString( "6" , xCenter - 3 , yCenter + clockRadius - 3 );
73
74 // Draw second hand
75 int sLength = ( int )(clockRadius * 0.8 );
76 int xSecond = ( int )(xCenter + sLength *
77 Math.sin(second * ( 2 * Math.PI / 60 )));
78 int ySecond = ( int )(yCenter - sLength *
79 Math.cos(second * ( 2 * Math.PI / 60 )));
80 g.setColor(Color.red);
81 g.drawLine(xCenter, yCenter, xSecond, ySecond);
82
83 // Draw minute hand
84 int mLength = ( int )(clockRadius * 0.65 );
85 int xMinute = ( int )(xCenter + mLength *
86 Math.sin(minute * ( 2 * Math.PI / 60 )));
87 int yMinute = ( int )(yCenter - mLength *
88 Math.cos(minute * ( 2 * Math.PI / 60 )));
89 g.setColor(Color.blue);
90 g.drawLine(xCenter, yCenter, xMinute, yMinute);
91
92
// Draw hour hand
93
int hLength = ( int )(clockRadius * 0.5 );
 
Search WWH ::




Custom Search