2013-01-23 92 views
0

我想知道是否有任何庫或其他類似BUZZ動畫的JFrames! yahoo messenger中的動畫,如果沒有現成的庫這樣做,那麼可能的算法是什麼呢?如何製作JFrame動畫?

回答

3

你可以創建你的自己的Buzz方法爲你工作的框架克。看看下面給出的代碼。

編輯 正如MadProgrammer和DavidKroukamp建議的那樣,我已經更改了代碼以符合標準。 :)

import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.SwingUtilities; 
import java.awt.BorderLayout; 
import java.awt.Point; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

class BuzzFrame extends JFrame 
{ 
    private JButton buzz = new JButton("BUZZ ME!!"); 
    public BuzzFrame() 
    { 
     super("BUZZ Frame!!"); 
    } 
    public void prepareGUI() 
    { 
     buzz.addActionListener(new BuzzActionListener(this)); 
     setSize(300,200); 
     getContentPane().add(buzz,BorderLayout.NORTH); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
    public static void main(String st[]) 
    { 
     SwingUtilities.invokeLater (new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       BuzzFrame bFrame = new BuzzFrame(); 
       bFrame.prepareGUI(); 
       bFrame.setVisible(true); 
      } 
     }); 
    } 
} 
class BuzzActionListener implements ActionListener 
{ 
    private JFrame frame; 
    private Point currLocation; 
    private int iDisplaceXBy = 5; 
    private int iDisplaceYBy = -10; 
    public BuzzActionListener(JFrame frame) 
    { 
     this.frame = frame; 
    } 
    @Override 
    public void actionPerformed(ActionEvent evt) 
    { 
     currLocation = frame.getLocationOnScreen(); 
     fireBuzzAction(); 
    } 
    private void fireBuzzAction() 
    { 
     SwingUtilities.invokeLater (new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       Point position1 = new Point(currLocation.x + iDisplaceXBy , currLocation.y + iDisplaceYBy); 
       Point position2 = new Point(currLocation.x - iDisplaceXBy , currLocation.y - iDisplaceYBy); 
       for (int i = 0; i < 20 ; i++) 
       { 
        frame.setLocation(position1); 
        frame.setLocation(position2); 
       } 
       frame.setLocation(currLocation); 
      } 
     }); 
    } 
} 
+0

耶不是一個好的做法,但強調的是理念;) –

+2

(@DavidKroukamp我會讓他修理給人一種贊成票之前那些「壞想法」 ......但我是一個憤世嫉俗老混蛋;)) – MadProgrammer

+0

@MadProgrammer非常真實....(不是憤世嫉俗的部分哈哈:P)我正在考慮它.... Vishal K,請你糾正代碼,以便我可以再次投票... ...我現在只能看到'Thread.sleep' ......並且混合了其他不好的做法,問題非常突出。 –

0

我這樣做有一個簡單的函數:

public void ZUMBIDO() { 
 
     toFront(); 
 

 
     new Thread(new Runnable() { 
 

 
      int milisegundos = 50; 
 
      int posiciones = 20; 
 

 
      public void arriba() { 
 

 
       Point pos = getLocation(); 
 
       pos.translate(0, posiciones); 
 
       setLocation(pos); 
 

 
      } 
 

 
      public void abajo() { 
 
       Point pos = getLocation(); 
 
       pos.translate(0, -posiciones); 
 
       setLocation(pos); 
 
      } 
 

 
      public void derecha() { 
 
       Point pos = getLocation(); 
 
       pos.translate(posiciones, 0); 
 
       setLocation(pos); 
 
      } 
 

 
      public void izquierda() { 
 
       Point pos = getLocation(); 
 
       pos.translate(-posiciones, 0); 
 
       setLocation(pos); 
 
      } 
 

 
      public void proceso() { 
 
       try { 
 
        arriba(); 
 
        Thread.sleep(milisegundos); 
 
        derecha(); 
 
        Thread.sleep(milisegundos); 
 
        abajo(); 
 
        Thread.sleep(milisegundos); 
 
        izquierda(); 
 
        Thread.sleep(milisegundos); 
 
       } catch (InterruptedException ex) { 
 
        Logger.getLogger(cliente.class.getName()).log(Level.SEVERE, null, ex); 
 
       } 
 
      } 
 

 
      @Override 
 
      public void run() { 
 

 
       for (int i = 0; i <= 5; i++) { 
 
        proceso(); 
 
       } 
 

 
      } 
 

 
     }).start(); 
 

 
    }

您可以用下一個代碼,它與回聲概念一個簡單的聊天動作觀看。

https://github.com/MikeSoft/Sistemas_Operativos_Clase/blob/master/chat/src/sistemas/operativos/cliente.java