-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCounter.java
41 lines (41 loc) · 939 Bytes
/
Counter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// A counter application, a NetBeans project
package counter;
/*@author Saket*/
import java.applet.*;
import java.awt.*;
public class Counter extends Applet implements Runnable
{
int count;
Thread t;
public void init()
{
count=0;
t=new Thread(this);
t.start();
}
public void run()
{
try
{
while(true)
{
repaint();
Thread.sleep(1000);
++count;
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
public void paint(Graphics g)
{
g.setFont(new Font("serif",Font.BOLD,36));
FontMetrics fm=g.getFontMetrics();
String str=" "+count;
Dimension d=getSize();
int x=d.width/2-fm.stringWidth(str)/2;
g.drawString(str,x,d.height/2);
}
}