Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (775)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
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 1250 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  
 
 

 
hadezbladez (45 views)
2018-11-16 13:46:03

hadezbladez (50 views)
2018-11-16 13:41:33

hadezbladez (35 views)
2018-11-16 13:35:35

hadezbladez (20 views)
2018-11-16 13:32:03

EgonOlsen (1902 views)
2018-06-10 19:43:48

EgonOlsen (1937 views)
2018-06-10 19:43:44

EgonOlsen (1287 views)
2018-06-10 19:43:20

DesertCoockie (1734 views)
2018-05-13 18:23:11

nelsongames (1413 views)
2018-04-24 18:15:36

nelsongames (2046 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!