2015-08-18 28 views
0

我幾乎準備放棄。在java中使用Swing Timer的兩點之間的動畫線繪圖

我有一個JPanel,繪製了各種線條和圓圈。有三個JButtons,每個按鈕調用不同的動作。當點擊其中一個時,會在面板中添加新的線條和圓圈。我怎樣才能做到這一點,以便當這個新的圓圈被添加時,該線條被緩慢繪製,然後圓圈被添加。它必須是擺動計時器而不是線程。

這是我的主類

import java.awt.BorderLayout; 

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

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class AnimateNode implements ActionListener { 

    NodePane nodePane = new NodePane(); 
    JButton jb1, jb2, jb3; 
    JTextField tf; 

    public static void main(String[] args) { 

     AnimateNode animate = new AnimateNode(); 

    } 

    public AnimateNode() { 

     JFrame frame = new JFrame("Version13");  
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new BorderLayout()); 

     JPanel jp1 = new JPanel();  

     tf = new JTextField(3); 
     jb1 = new JButton("Search"); 
     jb2 = new JButton("Insert"); 
     jb3 = new JButton("Delete"); 
     jb1.addActionListener(this); 
     jb2.addActionListener(this); 
     jb3.addActionListener(this); 
     frame.add(jp1, BorderLayout.SOUTH); 
     jp1.add(tf); 
     jp1.add(jb1); 
     jp1.add(jb2); 
     jp1.add(jb3); 

     nodePane.setPreferredSize(new Dimension(1000, 600)); 
     frame.add(nodePane, BorderLayout.CENTER); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     nodePane.initialise(); 
     frame.setVisible(true); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     String input = tf.getText(); 
     int numbInput = (int) ((Double.parseDouble(input))); 

     if (e.getSource() == jb1) { 
      // impement later 
      tf.setText(""); 
     } 

     if (e.getSource() == jb2) { 
      nodePane.insert(numbInput); 
      tf.setText(""); 

     } 

     if (e.getSource() == jb3) { 
      //implement later 
      tf.setText(""); 

     } 

    } 

} 

在一個單獨的類「NodePane」是其中的paintComponent方法是。它遍歷節點隊列,爲每個節點和它們之間的連線繪製圓。我只希望添加的最後一個節點使用計時器。添加的最後一個節點可以被識別,但我對於如何實現一個定時器非常困惑。

塗料組件的代碼是有點像這樣:

public void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    Graphics2D g2D = (Graphics2D) g.create(); 
     root.level = 0; 
     Queue<NodeForm<T>> queue = new LinkedList<NodeForm<T>>(); 
     queue.add(root); 
     while (!queue.isEmpty()) { 
      NodeForm<T> node = queue.poll(); 
      Point p = node.getLocation(); 
      paintLine(node, g2D); 
      paintNode(node, p, g2D); 
      int level = node.level; 
      NodeForm<T> left = node.getLeft(); 
      NodeForm<T> right = node.getRight(); 
      if (left != null){ 
       left.level = level + 1; 
       queue.add(left); 
      } 
      if (right != null){ 
       right.level = level + 1; 
       queue.add(right); 
      } 
     } 
    } 

我很快就糊塗了!我不希望計時器影響每一行和一圈......只是最後一個。我真的希望有人可以幫助:)

回答

0

爲了減緩繪圖,您需要創建Thread或者您可以使用timer。訣竅在於你畫線和sleepThread一段時間,並繪製較長的線。看起來這條線是慢慢畫出來的。

class painter implements Runnable { 

    @Override 
    public void run() { 

     // the Swing call below must be queued onto the Swing event thread 
     SwingUtilities.invokeLater(new Runnable(){ 
     @Override 
     public void run() { 
      while(cond..){ 
       // OK To make Swing method calls here 
       length_of_line++; 
       yourPanel.repaint(); 
      } 
     } 
     }); 
     try { 
     Thread.sleep(1000); 
     } catch (InterruptedException e) { 
     // .... 
     } 
    } 
} 
+0

謝謝。所以這應該在一個單獨的課堂?當我到達需要動畫的painComponent方法時,我會調用painter.run()? – Ash

+0

@Ash你說得對。修改循環以適合您的目的 – Rahul