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

    Default trouble animating

    myThing is a rectangle, which I want to see move horizontally a bit. But this doesn't happen; the rectangle just flickers a bit in its original position, which is where it stays put.
    Would appreciate any help; thanks.

    Java Code:
    public class MovingThing extends JFrame
    {
    ...
      private Thing myThing = new Thing();
    
    ...
        public void paint(Graphics g)
        {
            super.paint(g);
            myThing.paint(g);
        }
    
        private class TryThisButtonWatcher implements ActionListener
        {
          ActionListener moveHorizontally = new ActionListener()
          {
            public void actionPerformed(ActionEvent a)
            {
                for (int i=0; i<10; i++)
                {
                  myThing.setParams(100+5*i,300,100,50);
                  repaint();
                }
            }
          };
            Timer myTimer = new Timer(1000, moveHorizontally);
            public void actionPerformed(ActionEvent a)
            {
                myTimer.start();
            }
        } // end of: private class TryThisButtonWatcher implements ActionListener
    
    ...
    ...
    }
    
    
    public class Thing extends JComponent
    {
        private int one, two, three, four;
    
        public Thing()
        {
        }
        
        public void paint(Graphics g)
        {
            super.paint(g);
            g.drawRect(one, two, three, four);
        }
        
        public void setParams(int a, int b, int c, int d)
        {
            one = a; two = b; three = c; four = d;
        }
    
    } // end of: public class Thing extends JComponent

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

    Default

    For me at least there are too many details unknown here. You should try to create a very small compilable and runnable program that demonstrates your problem. It won't need a JButton, just a JPanel to do your drawing in (you should paint in the JPanel's paintComponent method, never the JFrame's paint method). In short, an SSCCE.

    Luck!

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

  4. #4
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Thanks, you're right, good advice. I will go away and think about this, and, in isolating my problem in an SSCCE, I might be able to resolve this.

Similar Threads

  1. Trouble with paintComponent()
    By ShirlyPunk in forum AWT / Swing
    Replies: 17
    Last Post: 03-13-2010, 04:27 PM
  2. GUI Trouble
    By rvgsd in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 12:10 AM
  3. Here comes trouble... :-)
    By sargehendricks in forum Introductions
    Replies: 1
    Last Post: 04-23-2009, 03:18 PM
  4. trouble with actionPerformed
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-26-2008, 02:18 AM
  5. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-21-2008, 11:52 PM

Tags for this Thread

Posting Permissions

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