[英]How to make JFrame Animation?
我想知道是否有任何庫或其他東西可以像BUZZ一樣對JFrames進行動畫處理! 如果沒有現有的庫可以執行yahoo Messenger中的動畫,那么執行該操作的可能算法是什么?
您可以為要處理的幀創建自己的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);
}
});
}
}
我用一個簡單的功能做到這一點:
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(); }
您可以在下一個代碼中觀看它的運行,它是一個帶有回聲概念的簡單聊天。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.