reallygorgo
asked on
Cannot see intermediate steps in animation
I am having trouble showing a simple animation in a Java application. I paint several circles separated by repaint and sleep requests but I only see the last one, not the intermediate ones. The attached code displays a single "circle" at (100,500). When the user clicks on the "Move" button I want it to show circles at (200,500), (300,500), and (400,500), separated by short delays. However I only see the last one. Probably a simple misconception on my part... Thanks.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SlowExample{
private Mypanel mypanel;
private JButton button;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(
new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SlowExample eg = new SlowExample();
frame.setContentPane(eg.createContentPane());
frame.setSize(800, 800);
frame.setVisible(true);
}
}
);
}
public JPanel createContentPane(){
JPanel contentPane = new JPanel(new BorderLayout());
mypanel = new Mypanel();
contentPane.add(mypanel, "Center");
button = new JButton("Move");
button.addActionListener(new ButtonHandler(mypanel));
contentPane.add(button, "South");
return contentPane;
}
} // end of class SlowExample
class ButtonHandler implements ActionListener {
private Mypanel mypanel;
public ButtonHandler(Mypanel mypanel) {
this.mypanel = mypanel;
}
public void actionPerformed(ActionEvent e) {
AbstractButton b = (AbstractButton) e.getSource();
if (b.getText().equals("Move") ) {
mypanel.move();
} else
System.err.println("Unknown source "+b.getText());
}
} // end of ButtonHandler
class Mypanel extends JPanel{
private Point p = new Point(100,500);
public void paintComponent(Graphics g) {
super.paintComponent(g);
int height = getSize().height;
g.setColor(Color.red);
g.fillOval(p.x-2, (height - p.y)-2, 8, 8);
}
public void move() {
p = new Point(200,500);
repaint();
try{Thread.sleep(500);} catch (InterruptedException e){};
p = new Point(300,500);
repaint();
try{Thread.sleep(500);} catch (InterruptedException e){};
p = new Point(400,500);
repaint();
}
} // end of Mypanel
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
that looks better
> Can you explain what was going wrong before? I guess I was sleeping the Event Dispatching thread?
correct. one thread handles updating the gui. so when you sleep no updates would occur.
> Can you explain what was going wrong before? I guess I was sleeping the Event Dispatching thread?
correct. one thread handles updating the gui. so when you sleep no updates would occur.
whether thats the best way or not depends on what you are ultimately trying to achieve.
How you have it currently it will spawn a new thread each time the button is pressed.
How you have it currently it will spawn a new thread each time the button is pressed.
ASKER
wrong comment accepted
ASKER
Thanks for your help.
ASKER
> How you have it currently it will spawn a new thread each time the button is pressed
I guess that a more conventional approach would be to organise things up front.. eg. along the lines of the Model-View-Controller pattern.
I guess that a more conventional approach would be to organise things up front.. eg. along the lines of the Model-View-Controller pattern.
ASKER
Open in new window