Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you need is use a <a href="http://java.sun.com/javase/6/docs/api/java/util/Timer.html" rel="noreferrer">Timer</a> which changes the state of your component ( in this case the cellular growth ) and then call <code>JComponent.repaint()</code> </p> <p>This timer can be <a href="http://java.sun.com/javase/6/docs/api/java/util/Timer.html#cancel%28%29" rel="noreferrer">cancelled</a> to make the pause and then to restart it, you just create a new one:</p> <p>So you could define the following two methods:</p> <pre><code>private Timer timer; ... public void startPaiting() { timer = new Timer(); timer.schedule( new TimerTask(){ public void run(){ changeState(); repaint(); } },0, 10000 ); // 10 s. } public void pause(){ timer.cancel(); } </code></pre> <p>And then in your "Pause/Resume" button invoke this "pause/startPaiting" methods:</p> <pre><code>if( e.getActionCommand().equals("Pause")){ growPanel.pause(); setText("Resume"); } else { growPanel.startPaiting(); setText("Pause"); } </code></pre> <p><a href="http://www.vimeo.com/8849389" rel="noreferrer">http://img512.imageshack.us/img512/7119/capturadepantalla201001.png</a></p> <p>Here's the complete source code to see it running: </p> <pre><code>import javax.swing.*; import java.awt.Graphics; import java.awt.Point; import java.awt.event.*; import java.util.Timer; import java.util.TimerTask; public class Grow { public static void main( String [] args ) { JFrame frame = new JFrame(); final GrowPanel growPanel = new GrowPanel(); frame.add( growPanel ); frame.add( new JPanel(){{ add( new JButton("Pause"){{ addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent e ){ if( e.getActionCommand().equals("Pause")){ growPanel.pause(); setText("Resume"); } else { growPanel.startPaiting(); setText("Pause"); } } }); }});}}, java.awt.BorderLayout.SOUTH ); frame.setSize( 400, 300 ); frame.setVisible( true ); } } class GrowPanel extends JComponent { private int x; private int y; private Timer timer; GrowPanel() { x = 10; y = 10; startPaiting(); } public void startPaiting() { timer = new Timer(); timer.schedule( new TimerTask(){ public void run(){ changeState(); repaint(); } },0, 100 ); // or 10000 which is 10 s. } public void pause(){ timer.cancel(); } public void paintComponent( Graphics g ){ g.fillOval( x, y, 10, 10 ); } private void changeState(){ x+=10; if( x &gt;= 400 ) { y+=10; x = 0; } if( y &gt;= 300 ){ y = 10; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload