⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swbclock.java

📁 是一个关于时种的小例子,写的很短对于刚学习java的新手有着很大的帮助!
💻 JAVA
字号:
import java.awt.*; 
import java.util.*; 
import java.text.*; 
import java.applet.*; 
public class SwbClock extends Applet implements Runnable{ 
 //declare variable 
 private Thread my_thread;  //thread to display the clock 
 private Date current_date;  //  
 private String lastdate,lasttime;   // string of the date 
 private int last_x_s,last_y_s,last_x_m, 
   last_y_m,last_x_h,last_y_h; // the dimension of hands 
 private Color mh_hand_color,s_hand_color,inner_circle_color, 
   outer_circle_color,inner_fill_color,outer_fill_color, 
   date_color,bg_color; 
 private String mh_hand_str,s_hand_str,inner_circle_str, 
   outer_circle_str,inner_fill_str,outer_fill_str, 
   date_str,bg_str; 
 private String font_to_use; 
 private Font font; 
 private SimpleDateFormat formatter; 
 private int R,r,xcenter,ycenter; 
inner_circle_str=param; 
  if( (param=getParameter("outer_fill_str")) != null) 
   outer_fill_str=param; 
  if( (param=getParameter("inner_circle_str")) != null) 
   inner_circle_str=param; 
  if( (param=getParameter("s_hand_str")) != null) 
   s_hand_str=param; 
  if( (param=getParameter("mh_hand_str")) != null) 
   mh_hand_str=param; 
  try{ 
   mh_hand_color=new Color(Integer.parseInt(mh_hand_str,16)); 
   s_hand_color=new Color(Integer.parseInt(s_hand_str,16)); 
   inner_circle_color=new Color(Integer.parseInt(inner_circle_str,16)); 
   outer_circle_color=new Color(Integer.parseInt(outer_circle_str,16)); 
   inner_fill_color=new Color(Integer.parseInt(inner_fill_str,16)); 
   outer_fill_color=new Color(Integer.parseInt(outer_fill_str,16)); 
   date_color=new Color(Integer.parseInt(date_str,16)); 
   bg_color=new Color(Integer.parseInt(bg_str,16)); 
  }catch(IllegalArgumentException e){ 
   e.printStackTrace(); 
  } 
  setBackground(bg_color); 
 } 
 //method: start() 
 public void start(){ 
  my_thread=new Thread(this); 
  my_thread.start(); 
 } 
 //method: stop() 
 public void stop(){ 
  my_thread=null; 
 } 
 //method: run() 
 public void run(){ 
  while(true){ 
   repaint(); 
   try{ 
    my_thread.sleep(100); 
   }catch(InterruptedException e){ 
    e.printStackTrace(); 
   } 
  } 
 } 
 //method: update(Graphics g) 
 public void update(Graphics g){ 
  int xh,yh,xm,ym,xs,ys; 
  int s=0,m=10,h=10; 
  String today; 
  String time; 
  current_date=new Date(); 
   
  formatter.applyPattern("s"); 
  try{ 
   s=Integer.parseInt(formatter.format(current_date)); 
  }catch(NumberFormatException e){ 
   s=0; 
  } 
  formatter.applyPattern("m"); 
  try{ 
   m=Integer.parseInt(formatter.format(current_date)); 
}catch(NumberFormatException e2){ 
   m=10; 
  } 
  formatter.applyPattern("h"); 
  try{ 
   h=Integer.parseInt(formatter.format(current_date)); 
  }catch(NumberFormatException e3){ 
   e3.printStackTrace(); 
  } 
   
  xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 50 + xcenter); 
        ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 50 + ycenter); 
        xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter); 
        ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter); 
        xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 
                   + xcenter); 
        yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 
                   + ycenter); 
        //Get the date to print  
        formatter.applyPattern("EEE-yy-MMM-dd"); 
        today=formatter.format(current_date);  
        formatter.applyPattern("hh:mm:ss"); 
        time=formatter.format(current_date); 
        g.setFont(font); 
  //draw the hand 
  g.setColor(inner_fill_color); 
//先擦除掉已绘各针 
  if(xs!=last_x_s||ys!=last_y_s){ 
   g.drawLine(xcenter,ycenter,last_x_s,last_y_s); 
   g.drawString(lastdate,25,2*ycenter+5); 
g.drawString(lasttime,xcenter-20,ycenter+25); 
  } 
  if(xm!=last_x_m||ym!=last_y_m){ 
   g.drawLine(xcenter,ycenter-1,last_x_m,last_y_m); 
   g.drawLine(xcenter-1,ycenter,last_x_m,last_y_m); 
  } 
  if(xh!=last_x_h||yh!=last_y_h){ 
   g.drawLine(xcenter,ycenter-1,last_x_h,last_y_h); 
   g.drawLine(xcenter-1,ycenter,last_x_h,last_y_h); 
  }  
  //draw the hand 
  g.setColor(mh_hand_color); 
  g.drawLine(xcenter,ycenter-1,xh,yh); 
  g.drawLine(xcenter-1,ycenter,xh,yh); 
  g.drawLine(xcenter,ycenter-1,xm,ym); 
  g.drawLine(xcenter-1,ycenter,xm,ym); 
  g.setColor(s_hand_color); 
  g.drawLine(xcenter,ycenter,xs,ys);   
   
  g.setColor(date_color); 
  g.drawString(today,25,2*ycenter+5); 
  g.drawString(time,xcenter-20,ycenter+25); 
   
  last_x_s = xs; last_y_s = ys; 
        last_x_m = xm; last_y_m = ym; 
        last_x_h = xh; last_y_h = yh; 
        lastdate = today; 
        lasttime = time; 
        current_date = null;        
 } 
 //method: paint(Graphics g) 
 public void paint(Graphics g){ 
  g.setFont(font); 
  //draw the clock 
  g.setColor(outer_fill_color); 
  g.fillArc(xcenter-R,ycenter-R,2*R,2*R,0,360); 
  g.setColor(outer_circle_color); 
  g.drawArc(xcenter-R,ycenter-R,2*R,2*R,0,360); 
  g.setColor(inner_fill_color); 
  g.fillArc(xcenter-r,ycenter-r,2*r,2*r,0,360); 
  g.setColor(inner_circle_color); 
  g.drawArc(xcenter-r,ycenter-r,2*r,2*r,0,360); 
  //draw the number 
  g.setColor(mh_hand_color); 
  g.drawString("12",xcenter-5,ycenter-r+12); 
  g.drawString("3",xcenter+r-9,ycenter+3); 
  g.drawString("6",xcenter-3,ycenter+r-3); 
  g.drawString("9",xcenter-r+5,ycenter+3); 
  //draw the hand 
  g.setColor(s_hand_color); 
  g.drawLine(xcenter,ycenter,last_x_s,last_y_s); 
g.setColor(mh_hand_color); 
  g.drawLine(xcenter,ycenter-1,last_x_m,last_y_m); 
  g.drawLine(xcenter-1,ycenter,last_x_m,last_y_m); 
  g.drawLine(xcenter,ycenter-1,last_x_h,last_y_h); 
  g.drawLine(xcenter-1,ycenter,last_x_h,last_y_h); 
 } 
  
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -