Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Need Help with This Animation Code  (Read 1155 times)
0 Members and 1 Guest are viewing this topic.
Offline TMP72

Junior Newbie





« Posted 2004-10-15 16:44:34 »

Hey Guys,

When I run the following code.  The animation does not seem to work right.  When the button is clicked the circle moves from point a to b, but does not show the movement in between. If I remove the action listener code and button and run the for loop in the go procedure the animation is smooth.

Any suggestions on how to fix this within the context of the code I've written?  I'm sure there are better ways to do this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleAnimation implements ActionListener {

      int x=70;
      int y=70;
      MyDrawPanel drawPanel;
     
     

      public static void main (String [] args) {
           
            SimpleAnimation gui = new SimpleAnimation();
            gui.go ();
      }

      public void go () {
           
            JFrame frame = new JFrame("Tom's Animation Program");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JButton button = new JButton("Click To Start The Show !");
            button.addActionListener (this);
            drawPanel = new MyDrawPanel();
            frame.getContentPane().add(BorderLayout.SOUTH,button);
            frame.getContentPane().add(drawPanel);
            frame.setSize(300,300);
            frame.setVisible(true);
      }
           
     
     

      public void actionPerformed (ActionEvent event){
     
            for (int i=0; i<130; i++) {
           
                  x++;
                  y++;
                  drawPanel.repaint ();
                  try{Thread.sleep(50);
                  }catch (Exception ex){}
            }
     
      }
      class MyDrawPanel extends JPanel{

            public void paintComponent (Graphics g){

                  g.setColor(Color.white);
                  g.fillRect(0,0,this.getWidth(),this.getHeight());

                  g.setColor(Color.green);
                  g.fillOval (x,y,40,40);
            }
      }
}
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #1 - Posted 2004-10-19 12:04:53 »

To explain this problem, a little Java architecture is required.  When the JVM starts, several threads are automatically started.  One is the main execution thread, where you program runs.  Then there is the GarbageCollector thread and the event dispatch thread.  There may be others, but you need to look at the event dispatch thread.  When you register an event listener(eg ActionListener), your method actionPerformed will be called from the event dispatch thread.  No other events can be processed while you are in the actionPerformed method.  Once this method returns, the next event can be processed.  For this reason, most tutorials/books will tell you to keep your processing to a minimum in those methods.  Your problem occurs because painting is also done from the event dispatch thread.  So your constant calls to repaint() inside actionPerformed will not be serviced until actionPerformed exits.  Since your loop is done at this point, you only see the end result.  For this reason, you need to do animation in a separate thread of your own.  I have modified your code to show you an example.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleAnimation extends JPanel implements ActionListener, Runnable {

    int x=70;
    int y=70;
    boolean drawAnim = false;

    public static void main (String [] args) {

        SimpleAnimation gui = new SimpleAnimation();
        gui.go ();
    }

    public void go () {

        Thread t = new Thread(this);
        t.start();
        JFrame frame = new JFrame("Tom's Animation Program");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton("Click To Start The Show !");
        button.addActionListener (this);
        frame.getContentPane().add(BorderLayout.SOUTH,button);
        frame.getContentPane().add(this);
        frame.setSize(300,300);
        frame.setVisible(true);
    }

    public void run() {
        while(true) {
            if(drawAnim) {
                for (int i=0; i<130; i++) {
                    x++;
                    y++;
                    repaint ();
                    try {
                        Thread.sleep(50);
                    }
                    catch (Exception ex){}
                }
                drawAnim = false;
            }
        }
    }

    public void actionPerformed (ActionEvent event){
        drawAnim = true;
    }

    public void paintComponent (Graphics g){
        g.setColor(Color.white);
        g.fillRect(0,0,this.getWidth(),this.getHeight());

        g.setColor(Color.green);
        g.fillOval (x,y,40,40);
    }
}

Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (148 views)
2017-09-30 02:57:34

theagentd (213 views)
2017-09-26 18:23:31

cybrmynd (296 views)
2017-08-02 12:28:51

cybrmynd (285 views)
2017-08-02 12:19:43

cybrmynd (295 views)
2017-08-02 12:18:09

Sralse (288 views)
2017-07-25 17:13:48

Archive (967 views)
2017-04-27 17:45:51

buddyBro (1093 views)
2017-04-05 03:38:00

CopyableCougar4 (1665 views)
2017-03-24 15:39:42

theagentd (1426 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!