在战牛刀(不停震动的Dialog)

效果图没滴..因为他不停震动=.= 我截下来只是个静态图片
所以就不发了...
运行效果自己看

 

package DZDialog;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class DialogEarthquakeCenter extends Object implements Runnable{

  
// 在这里我把常量说一下..下面计算时你们自己看..我就不一个一个说了
         
//Shake Distance 是摇动距离的意思
         
//Shake Cycle 是摇动周期的意思
         
//Shake Duration  摇动期间
         
//***** Upadate 这个看不懂的话 下面的你就别看了
            public static final int SHAKE_DISTANCE = 10;
            
public static final double SHAKE_CYCLE = 50;
            
public static final int SHAKE_DURATION = 1000;
            
public static final int SHAKE_UPDATE = 2;

            
private JDialog dialog;
            
private Point naturalLocation;
            
private long startTime;
            
private Timer shakeTimer;
            
            
private final double TWO_PI = Math.PI * 2.0;

            
public DialogEarthquakeCenter (JDialog d) {
                dialog 
= d;
            }


            
public void startShake( ) {
                naturalLocation 
= dialog.getLocation( );
                startTime 
= System.currentTimeMillis( );
                shakeTimer 
=
                   
new Timer(SHAKE_UPDATE,
                             
new ActionListener( ) {

                              
public void actionPerformed (ActionEvent e) {    
                               
//将当前系统的系统时间 以毫秒的形式付给elapsed
                                  long elapsed = System.currentTimeMillis();
                                  
                                  
double waveOffset = (elapsed % SHAKE_CYCLE) /
                                      SHAKE_CYCLE; 
                                  
double angle = waveOffset * TWO_PI;

                                  
int shakenX = (int) ((Math.sin (angle) *
                                                        SHAKE_DISTANCE) 
+
                                                       naturalLocation.x); 
                                  dialog.setLocation (shakenX, naturalLocation.y);               
                                  dialog.repaint( );

                                  
if (elapsed >= SHAKE_DURATION)                                                  stopShake( );
                              }

                         }

                         );
            shakeTimer.start( );
            
            
//如果不开下面这个线程的话 窗口只震动一次
            
//如果有这个线程 就是Thread.sleep(100)运行一次Timer
            
//每运行Timer一次 窗口就会震动一次 我让线程while(true)就是死循环 让他不停的震动
            Thread t = new Thread(this);
            t.start();
        }


        
public void stopShake( ) {
            shakeTimer.stop( );
            dialog.setLocation (naturalLocation);
            dialog.repaint( );
        }


        
        
public static void main (String[] args) {
            JOptionPane pane 
=
                
new JOptionPane ("这几天.心情很郁闷..汶川的事.让我沉静了很久..这到底是谁的错?" +'\n'+
                           
"难道老天没有感情吗??.震吧..震吧." +'\n'+
                           
"连我的Swing程序也一起震了吧",
                                 JOptionPane.ERROR_MESSAGE, JOptionPane.OK_OPTION);
            
            JDialog d 
= pane.createDialog (null"痛心的汶川");
            
            DialogEarthquakeCenter dec 
= new 

            DialogEarthquakeCenter (d);
            d.pack( );
            d.setModal (
false);
            d.setVisible(
true);
            dec.startShake( );

            
// wait (forever) for a non-null click and then quit
            while (pane.getValue( ) == JOptionPane.UNINITIALIZED_VALUE ) 
               
try { Thread.sleep(100); } 
               
catch (InterruptedException ie) {}
      
      }
 
               System.exit(
0); 
            }


  
public void run() {
   
while(true){
    
try{
    Thread.sleep(
100);
    }
catch(Exception e){
    }

    shakeTimer.start();
   }

  }
 

}



 

posted on 2008-05-20 13:10 相信 阅读(470) 评论(2)  编辑  收藏 所属分类: Swing文章

评论

# re: 在战牛刀(不停震动的Dialog) 2011-03-24 10:34 venusdrogon

楼主,我发现我开始崇拜你了   回复  更多评论   

# re: 在战牛刀(不停震动的Dialog) 2011-03-24 10:35 相信

我只是一只在飞翔的菜鸟....  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

公告

不显示applet

常用链接

留言簿(16)

我参与的团队

随笔档案

文章分类

文章档案

新闻档案

相册

swingchina 专业搞Swing的网站

搜索

最新评论

阅读排行榜

评论排行榜