Hi, need help, completely confused....


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Hi, need help, completely confused....

  1. #1
    Join Date
    Apr 2005
    Posts
    5

    Hi, need help, completely confused....

    Hi, I'm trying to work with threads to produce an animation that paints an oval onto the screen in 2 alternate places. One on the screen at a time. The thing I am having trouble with is using a mouse listener to change the sleep time of the threads. I want to use the left mouse to reduce the delay, and the right to increase the delay. This is my code so far...

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
        
        public class AnimApp extends JComponent implements Runnable {
            Image[] images = new Image[2];
            int frame = 0;
            int x = 100;
            int y = 100;
            int x_con = x;
            int delay;
            public void paint(Graphics g) {
              
                    g.fillOval(x, y,10,10);
                }
            
        
            public void run() {
    
                try {
                    while (true) {
                      
              if (x == x_con) {
              
              x=(x_con/2);   
     
           
              }else if (x == x_con/2) {
           
                x=x+(x_con/2);
          
        }
        
                        // Causes the paint() method to be called
                        repaint();
        
                        // Wait
                        
                        Thread.sleep(delay);
                    }
                } catch (Exception e) {
                }
            }
        
            public void test() {
                AnimApp app = new AnimApp();
                JFrame frame = new JFrame();
                frame.addMouseListener(
                new MouseAdapter(){
                public void mouseClicked(MouseEvent e){  
                         
               System.out.println("Yes");
              //Get a new animation thread and start
               delay = delay+500;
    
                   
            }//
    
          }//end new MouseAdapter
        )
                frame.add(app);
                frame.setSize(300, 300);
                frame.setTitle("Hello");
                frame.setVisible(true);
        
                (new Thread(app)).start();
            }
        }
    The method test calls the class, I cant for some reason get it to work if change it to a constructor. I'm just completely lost.. HELP!!

    Steve

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Here is my fixup, among other thing I use the getContentPane() method for
    adding components to the JFrame, and I set the delay to an initial value (not zero).
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class AnimApp
        extends JComponent
        implements Runnable {
      Image[] images = new Image[2];
      int frame = 0;
      int x = 100;
      int y = 100;
      int x_con = x;
      int delay=300;
      Thread runner=null;
      public AnimApp() {
        JFrame frame = new JFrame();
        //frame.setLayout(new GridLayout());
        frame.addMouseListener(
            new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isRightMouseButton(e)) {
              delay +=100;
              if (delay < 100) delay=100;
            } else {
              delay -= 100;
            }
    
          } //
    
        } //end new MouseAdapter
        );
    
        frame.getContentPane().add(this);
    
        frame.setSize(300, 300);
        frame.setTitle("Hello");
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            if (runner!=null && runner.isAlive())
              runner.interrupt();
          }
        });
    
        runner=new Thread(this);
        runner.start();
    
    
      }
      public void paint(Graphics g) {
    
        g.fillOval(x, y, 10, 10);
      }
    
      public void run() {
    
        try {
          while (true) {
    
            if (x == x_con) {
    
              x = (x_con / 2);
    
            }
            else if (x == x_con / 2) {
    
              x = x + (x_con / 2);
    
            }
    
            // Causes the paint() method to be called
            repaint();
    
            // Wait
    
            Thread.sleep(delay);
          }
        }
        catch (Exception e) {
        }
      }
    
      public  static void main(String [] args) {
        AnimApp app = new AnimApp();
    
      }
    
    }
    eschew obfuscation

  3. #3
    Join Date
    Apr 2005
    Posts
    5
    Quote Originally Posted by sjalle
    Here is my fixup, among other thing I use the getContentPane() method for
    adding components to the JFrame, and I set the delay to an initial value (not zero).
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class AnimApp
        extends JComponent
        implements Runnable {
      Image[] images = new Image[2];
      int frame = 0;
      int x = 100;
      int y = 100;
      int x_con = x;
      int delay=300;
      Thread runner=null;
      public AnimApp() {
        JFrame frame = new JFrame();
        //frame.setLayout(new GridLayout());
        frame.addMouseListener(
            new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isRightMouseButton(e)) {
              delay +=100;
              if (delay < 100) delay=100;
            } else {
              delay -= 100;
            }
    
          } //
    
        } //end new MouseAdapter
        );
    
        frame.getContentPane().add(this);
    
        frame.setSize(300, 300);
        frame.setTitle("Hello");
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            if (runner!=null && runner.isAlive())
              runner.interrupt();
          }
        });
    
        runner=new Thread(this);
        runner.start();
    
    
      }
      public void paint(Graphics g) {
    
        g.fillOval(x, y, 10, 10);
      }
    
      public void run() {
    
        try {
          while (true) {
    
            if (x == x_con) {
    
              x = (x_con / 2);
    
            }
            else if (x == x_con / 2) {
    
              x = x + (x_con / 2);
    
            }
    
            // Causes the paint() method to be called
            repaint();
    
            // Wait
    
            Thread.sleep(delay);
          }
        }
        catch (Exception e) {
        }
      }
    
      public  static void main(String [] args) {
        AnimApp app = new AnimApp();
    
      }
    
    }

    Thanks very much that is brilliant. I've been so stuck for ages!!!!

    Steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center