[英]animate line drawing between two points in java using a Swing Timer
I am almost ready to give up. 我几乎准备放弃。
I have a JPanel, with various lines and circles drawn on it. 我有一个JPanel,上面画有各种线条和圆圈。 There are three JButtons, each button invokes a different action.
有三个JButton,每个按钮调用一个不同的动作。 When one of these is clicked, a new line and circle is added to the panel.
单击其中之一时,新的直线和圆将添加到面板中。 How can I make it so that when this new circle is added, that the line is slowly drawn, and then the circle is added.
我如何做到这一点,以便在添加新圆时慢慢画线,然后添加圆。 It has to be with a swing timer rather than a thread.
它必须与一个摆动计时器而不是线程一起使用。
This is my main class 这是我的主班
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("");
}
}
}
In a separate class "NodePane" is where the paintComponent method is. 在单独的类“ NodePane”中是paintComponent方法所在的位置。 It iterates through a queue of nodes drawing circles for each and lines between them.
它遍历节点队列,为每个节点绘制圆圈并在它们之间绘制线条。 I only want the last node added to use a timer.
我只希望添加的最后一个节点使用计时器。 The last node added can be identified, but I am very confused as to how I implement a timer.
可以确定添加的最后一个节点,但是我对如何实现计时器感到非常困惑。
the code for paint component is a little like this : paint组件的代码有点像这样:
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);
}
}
}
I am soon confused! 我很快就困惑了! I don't want the timer to effect every line and circle... just the last one.
我不希望计时器影响每一行和每一圈……只是最后一个。 I really hope someone can help :)
我真的希望有人能提供帮助:)
For slowing down the drawing you need to create a Thread
or you may use a timer
. 为了减慢绘图速度,您需要创建一个
Thread
,也可以使用timer
。 The trick is that you draw the line and sleep
the Thread
for some interval and draw longer line . 诀窍是您绘制线并让
Thread
sleep
一段时间,然后绘制更长的线。 It seems that line is slowly drawn. 看来这条线是慢慢画的。
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) {
// ....
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.