import java.awt.BorderLayout; |
import java.awt.Graphics; |
import java.util.Calendar; |
|
import javax.swing.JApplet; |
import javax.swing.JPanel; |
|
public class JApplet_Clock extends JApplet implements Runnable { |
|
/** |
* |
*/ |
private static final long serialVersionUID = 1L; |
private JPanel jContentPane= null ; |
private Thread thread= null ; |
private int hour; |
private int minute; |
private int second; |
@Override |
public void start() { |
// TODO Auto-generated method stub |
super .start(); |
if ( this .thread== null ){ |
this .thread= new Thread( this ); |
this .thread.start(); |
} |
} |
|
@Override |
public void paint(Graphics arg0) { |
// TODO Auto-generated method stub |
super .paint(arg0); |
int x,y,r= 80 ; |
arg0.fillOval( 94 , 94 , 12 , 12 ); |
for ( int i= 0 ;i< 60 ;i++){ |
x=( int ) ( 100 +r*Math.sin(Math.PI/ 30 *i)); |
y=( int ) ( 100 -r*Math.cos(Math.PI/ 30 *i)); |
if (i% 5 == 0 ){ |
arg0.fillOval(x- 5 , y- 5 , 10 , 10 ); |
} |
else { |
arg0.fillOval(x- 1 , y- 1 , 2 , 2 ); |
} |
} |
|
|
} |
|
@Override |
public void run() { |
// TODO Auto-generated method stub |
Graphics g= this .jContentPane.getGraphics(); |
while ( true ){ |
this .play( this .getCodeBase(), "1" ); |
g.setColor( this .jContentPane.getBackground()); |
drawHMS(g); |
Calendar c=Calendar.getInstance(); |
hour=c.get(Calendar.HOUR); |
minute=c.get(Calendar.MINUTE); |
second=c.get(Calendar.SECOND); |
|
//String str; |
//str=hour+":"+minute+"" |
g.setColor( this .jContentPane.getForeground()); |
drawHMS(g); |
g.fillOval( 94 , 94 , 12 , 12 ); |
try { |
Thread.sleep( 1000 ); |
} catch (InterruptedException e){ |
e.printStackTrace(); |
} |
|
} |
|
} |
|
private void drawHMS(Graphics g) { |
int x; |
int y; |
double cita; |
|
cita=(hour+minute/ 60.0 +second/ 3600.0 )/ 12 * 2 *Math.PI; |
x=( int )( 100 + 55 *Math.sin(cita)); |
y=( int )( 100 - 55 *Math.cos(cita)); |
g.drawLine( 100 , 100 , x, y); |
|
cita=(hour+minute/ 60.0 )/ 60.0 * 2 *Math.PI; |
x=( int )( 100 + 68 *Math.sin(cita)); |
y=( int )( 100 - 68 *Math.cos(cita)); |
g.drawLine( 100 , 100 , x, y); |
|
cita=second/ 60.0 * 2 *Math.PI; |
x=( int )( 100 + 72 *Math.sin(cita)); |
y=( int )( 100 - 72 *Math.cos(cita)); |
g.drawLine( 100 , 100 , x, y); |
} |
public JApplet_Clock(){ |
super (); |
} |
public void init(){ |
this .setSize( 300 , 200 ); |
this .setContentPane(getJContentPane()); |
|
} |
private JPanel getJContentPane(){ |
if (jContentPane== null ){ |
jContentPane= new JPanel(); |
|
jContentPane.setLayout( new BorderLayout()); |
} |
return jContentPane; |
|
} |
|
} |
高级设计师
by: 神马 发表于:2012-09-17 08:33:53 顶(1) | 踩(0) 回复
回复评论