Results 1 to 3 of 3
  1. #1
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Can't get a drawing to update

    When I click myButton, I want: the first version of myDiagram should display; then there should be a delay; then the second version should display:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Temp extends JPanel{
        private aDiagram myDiagram = new aDiagram();
    
        public Temp()
        {
        }
        
        public void paint(Graphics g){
            super.paint(g);
        }
    
        private void animate(){
            myDiagram.setParameters(10,20,30);
            myDiagram.drawMyDiag();
            try{Thread.sleep(500);}
            catch(InterruptedException e){System.exit(0);}
            repaint();
            myDiagram.setParameters(40,50,60);
            myDiagram.drawMyDiag();
        }
    
        private class myButtonWatcher implements ActionListener {
            public void actionPerformed(ActionEvent a) {
              animate();
            }
        }
        
    } // end of Temp class
    But what happens is that I only see the first version.

    Any ideas/help appreciated. Thanks.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    The code calls the animate from the event dispatch thread (EDT), which repaints and pauses. Given the EDT is responsible for the painting of components it won't do so until animate exits. Solution is to throw the animation into a Thread, SwingWorker, or use a javax.swing.Timer (given what you want to do - animate - a Timer is probably most appropriate).

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I agree with all that doWhile has to say and will add that you should do your painting in the JPanel's paintComponent method, not the paint method. That way your animation will have the advantages of double buffering.

Similar Threads

  1. Drawing an arc
    By berkeleybross in forum Java 2D
    Replies: 10
    Last Post: 12-09-2010, 01:32 AM
  2. How to update my jdk???
    By low224 in forum New To Java
    Replies: 4
    Last Post: 01-04-2009, 04:51 PM
  3. Drawing a map
    By Karp in forum AWT / Swing
    Replies: 4
    Last Post: 11-07-2008, 12:26 PM
  4. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 02:23 AM
  5. Using sql:update tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 11:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •