Board logo

标题: 无意中发现的好玩代码——摇动的Java对话框 [打印本页]

作者: hyrz    时间: 2010-6-19 23:04     标题: 无意中发现的好玩代码——摇动的Java对话框

无意中发现的好玩代码——摇动的Java对话框 由Linux系统中文网(Linux521.com)编辑收集整理,除Linux521注明原创文章外,其版权归原作者所有。如果您在学习中遇到问题欢迎在下面的评论中留言,我们会尽全力解答您的问题。
  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 Main {

  JDialog dialog;

  Point naturalLocation;

  Timer shakeTimer;

  public Main(JDialog d) {

  dialog = d;

  }

  public void startShake() {

  final long startTime;

  naturalLocation = dialog.getLocation();

  startTime = System.currentTimeMillis();

  shakeTimer = new Timer(5, new ActionListener() {

  public void actionPerformed(ActionEvent e) {

  double TWO_PI = Math.PI * 2.0;

  double SHAKE_CYCLE = 50;

  long elapsed = System.currentTimeMillis() - startTime;

  double waveOffset = (elapsed % SHAKE_CYCLE) / SHAKE_CYCLE;

  double angle = waveOffset * TWO_PI;

  int SHAKE_DISTANCE = 10;

  int shakenX = (int) ((Math.sin(angle) * SHAKE_DISTANCE) naturalLocation.x);

  dialog.setLocation(shakenX, naturalLocation.y);

  dialog.repaint();

  int SHAKE_DURATION = 1000;

  if (elapsed >= SHAKE_DURATION)

  stopShake();

  }

  });

  shakeTimer.start();

  }

  public void stopShake() {

  shakeTimer.stop();

  dialog.setLocation(naturalLocation);

  dialog.repaint();

  }

  public static void main(String[] args) {

  JOptionPane pane = new JOptionPane("your message",JOptionPane.ERROR_MESSAGE, JOptionPane.OK_OPTION);

  JDialog d = pane.createDialog(null, "title");

  Main dec = new Main(d);

  d.pack();

  d.setModal(false);

  d.setVisible(true);

  dec.startShake();

  }

  }
作者: shoutian2008    时间: 2010-6-20 00:22

沙发
偶的
呵呵
作者: 骇客    时间: 2010-6-20 11:05

谢了,顶~~~~~~~~~~~




欢迎光临 【3.A.S.T】网络安全爱好者 (http://3ast.com/) Powered by Discuz! 7.2