Results 1 to 5 of 5
Like Tree2Likes
  • 2 Post By camickr

Thread: Using threads for animation in a non-main class.

  1. #1
    wikemol is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Using threads for animation in a non-main class.

    I am trying to create an abstract class that will control any JComponent with the arrow keys when extended, but I don't know how. Right now, I havent added a KeyAdapter for the arrow keys, but I have added a run method that calls another method called "move" which is supposed to print the word "hello" in the output every 33 milliseconds. Once I quit the application, all of the "hello"s hit me at once, but they are not timed. Here is my code so far.

    Main Class:

    Java Code:
    package shoot;
    
    import javax.swing.JFrame;
    
    class Main extends Thread{
        
        JFrame frame = new JFrame("Shooter");
        
        public static void main(String[] args){
            Main main = new Main();
            main.init();
            
            
        }
        
        private void init(){
            Thread thread = new Thread(this);
            thread.start();
            Ship ship = new Ship();
            frame.setVisible(true);
            frame.setSize(500, 500);
            frame.getContentPane().add(ship);
            ship.run();
            
        }
        
        
        
    }
    Ship Class:

    Java Code:
    package shoot;
    
    import java.awt.Graphics;
    
    
    public class Ship extends Controllable{
        
        
        
        @Override public void paintComponent(Graphics g){
            g.fillRect(20, 0, 10, 10);
            g.fillRect(0, 10, 50, 30);
            repaint();
        }
    
        @Override
        public void move() {
            System.out.print("hello");
        }
    
        
        
        
    }
    Controllable Class:

    Java Code:
    package shoot;
    
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    public abstract class Controllable extends JPanel implements Runnable{
        
        int sleepTime = 33;
        
        public Controllable(){
            
        }
        
        @Override public void run() {
            while(true){
                try {
                    move();
                    Thread.sleep(33);
                } catch (InterruptedException ex) {
                    System.out.print("Thread in class Controls Interrupted \n   at line 17");
                }
            }
        }
        
        public abstract void move();
        
        public int getSleepTime(){
            return sleepTime;
        }
        
        
        @Override public abstract void paintComponent(Graphics g);
        
        
    }
    Any ideas?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Using threads for animation in a non-main class.

    1) NEVER invoke repaint() from within the paintComponent() method. This will cause a loop.

    2) Don't use Threads for animation. See: How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    3) Don't use KeyEvents to handle the arrow keys. See: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    DarrylBurke and Fubarable like this.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Using threads for animation in a non-main class.

    Moved from 'New to Java'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    wikemol is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: Using threads for animation in a non-main class.

    Quote Originally Posted by camickr View Post
    1) NEVER invoke repaint() from within the paintComponent() method. This will cause a loop.

    2) Don't use Threads for animation. See: How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    3) Don't use KeyEvents to handle the arrow keys. See: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    Thanks, but that leaves me with another question. What are threads used for?

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Using threads for animation in a non-main class.

    You use a Thread to execute long running tasks so you don't block the Swing GUI from repainting itself. Read the section from the above tutorial on "Concurrency" which explains more about threads.

Similar Threads

  1. Variables from another class won't assign to main class
    By erthbound0 in forum New To Java
    Replies: 3
    Last Post: 12-15-2011, 10:07 PM
  2. Replies: 0
    Last Post: 10-23-2011, 10:49 PM
  3. Could not find or load main class BubbleSort.class
    By blaqkout in forum New To Java
    Replies: 5
    Last Post: 09-12-2011, 07:54 PM
  4. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  5. How should an Animation class be implemented?
    By kiregad in forum New To Java
    Replies: 9
    Last Post: 06-18-2010, 07:39 AM

Posting Permissions

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