千里冰封
          JAVA 濃香四溢
          posts - 151,comments - 2801,trackbacks - 0
          這也是以前寫的一個東西,全屏下運行,很像是WINDOWS下的屏保,會顯示時鐘,并且會隨機自然地碰撞.全屏狀態(tài)下如需退出,請按小鍵盤上的0退出.
          點擊下載可執(zhí)行的JAR文件,JDK1.5以上
          import java.text.*;
          import java.awt.*;
          import javax.swing.*;
          import java.awt.geom.*;
          import java.util.*;
          import java.awt.event.*;
          /*
           * ScreenTest.java
           *
           * Created on 2006年7月6日, 下午5:23
           *
           * To change this template, choose Tools | Template Manager
           * and open the template in the editor.
           
          */

          /**
           *
           * 
          @author lbf
           
          */
          public class ScreenTest extends JPanel implements Runnable{
              
              
          /** Creates a new instance of ScreenTest */
              
          private String time;
              
          private Rectangle2D rd;
              
          private int x,y,a,b;//座標(biāo)值和方向值
              private int rgb=0;//顏色的值
              private Color color;//字體的顏色
              private int width,height;//屏幕的大小
              public ScreenTest(int width,int height) {
                  
          this.width=width;
                  
          this.height=height;
                  initWindow();
              }
              
          private void initWindow(){
                  x
          =(int)(Math.random()*300);
                  y
          =(int)(Math.random()*500);
                  a
          =1;
                  b
          =1;
                  
          this.setBackground(Color.BLACK);
                  
          this.setOpaque(true);
                  rd
          =new Rectangle2D.Double(10,10,101,10);
                  color
          =Color.YELLOW;
              }
              
          private void doColor(){
                 rgb
          =(int)(Math.random()*0xFFFFFF);
                     color
          =new Color(rgb);
               }
              
          public void paintComponent(Graphics g){
                  g.setColor(Color.BLACK);
                  g.fillRect(
          0,0,width,height);
                  g.setColor(color);
                  g.setFont(
          new Font("楷書",Font.BOLD,150));
                  FontMetrics fm
          =g.getFontMetrics();
                  rd
          =fm.getStringBounds(time,g);
                  g.drawString(time,x,(
          int)(y+rd.getHeight()));
                }
              
          private void doTime(){
                  Calendar cal
          =Calendar.getInstance();
                  DateFormat df
          =DateFormat.getTimeInstance(DateFormat.MEDIUM);
                  Date date
          =cal.getTime();
                  time
          =df.format(date);
                  x
          +=a;
                  y
          +=b;
                  
          double width1=rd.getWidth();
                  
          double height1=rd.getHeight();
                  rd.setRect(x,y,width1,height1);
                  
          if(rd.intersectsLine(width,0,width,height)){
                      doColor();
                      a
          =-1;
                  }
                  
          else if(rd.intersectsLine(0,0,0,height)){
                      doColor();
                      a
          =1;
                  }
                  
          else if(rd.getY()<=-80){
                       doColor();
                       b
          =1;
                  }
                  
          else if(rd.intersectsLine(0,height,width,height)){
                     doColor();
                      b
          =-1;
                  }
                  
              }
              
          public void run(){
                  
          while(true){
                      
          try{
                          Thread.sleep(
          2);
                          doTime();
                          repaint();
                      }
                      
          catch(InterruptedException ie){
                          ie.printStackTrace();
                      }
                  }
              }
             
          public  static void main(String arsg[]){
                  
                  GraphicsDevice gd
          =GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
                  DisplayMode dm
          =gd.getDisplayMode();
                  
          int width=dm.getWidth();
                  
          int height=dm.getHeight();
                  System.out.println(
          "width="+width+"\nheight="+height);
                  ScreenTest st
          =new ScreenTest(width,height);
                  
          final JFrame jf=new JFrame();
                  jf.getContentPane().add(st,BorderLayout.CENTER);
                  jf.setUndecorated(
          true);
                  gd.setFullScreenWindow(jf);
                  
          new Thread(st).start();
                  jf.addKeyListener(
          new KeyAdapter(){
                     
          public void keyReleased(KeyEvent ke){
                         
          if(ke.getKeyCode()==KeyEvent.VK_NUMPAD0)
                             System.exit(
          0);
                          } 
                  });
              }
              
          }



          盡管千里冰封
          依然擁有晴空

          你我共同品味JAVA的濃香.
          posted on 2007-09-03 09:18 千里冰封 閱讀(1576) 評論(1)  編輯  收藏 所屬分類: JAVASE

          FeedBack:
          # re: JAVA模仿屏保
          2008-10-25 22:18 | jiangchanghong
          盡管千里冰封
          依然擁有晴空

          你我共同品味JAVA的濃香.   回復(fù)  更多評論
            
          主站蜘蛛池模板: 辽宁省| 博乐市| 文水县| 华亭县| 铜川市| 泰州市| 元谋县| 安溪县| 乐至县| 华亭县| 大石桥市| 双流县| 青海省| 苏尼特右旗| 平陆县| 海盐县| 瓮安县| 平凉市| 合川市| 镇雄县| 长武县| 吉木乃县| 城步| 云南省| 合川市| 苏州市| 琼结县| 麻城市| 瓦房店市| 且末县| 革吉县| 天长市| 峨边| 依兰县| 吴堡县| 克拉玛依市| 淮阳县| 贡觉县| 屏东县| 文化| 海盐县|