5 Replies Latest reply: Jul 18, 2007 5:22 PM by 807605 RSS

    simultaneous graphics and music THREADS with SwingWorker but not working :(

    807605
      Hello

      please tell what is wrong with my definitions of SwingWorker
      probably just a small basic problem... i am running out of time, so your help is very valuable.

      My Idea is to have graphics activities in the main thread, so called Event dispatching thread

      To avoid slow down i try to play simultaneously music in another thread, so called worker thread

      I am a newbie, so please tell in detail what are the lines that i should change in my code.

      Thank you very much for your kind help!!!!

      (p.s. later i will try to play midi music, here in my test application i just play 'beep' for simplicity.)
      import java.awt.*;
          import java.awt.event.*;
          import java.awt.datatransfer.*;
          import javax.swing.*;
      
      import java.io.*;
      import java.util.Scanner;
      
          import java.lang.Math.*;
      
           
      public class ThreadTesting extends JPanel implements MouseListener  {   // implements MouseListener
      
               private static JPanel panel;
               private static JLabel label;
               private static JPanel panel2;
               private static JLabel label2;
      
               private static ImageIcon icon;
               private static ImageIcon icon2;
               private static int laskuri;
      
             
        //     private static JPanel container = new JPanel();
               
      private static JFuguePlayer playWithJFugue;
      
      
      
         public static void main(String[] args) throws IOException
         { 
      
      
             javax.swing.SwingUtilities.invokeLater(new Runnable() {
      
                public void run() {
                   createAndShowMyGUI();
                }
             });
         }
      
      
      
      
         private static void createAndShowMyGUI() {
      
      
             JFrame frame = new JFrame("Saiekokeilu");
             frame.setExtendedState(Frame.MAXIMIZED_BOTH); // HUOM. miksi t�m� ei toimi?
      
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
             JComponent newContentPane = new ThreadTesting();
              newContentPane.setOpaque(true); //content panes must be opaque
              frame.setContentPane(newContentPane);
              
              //Display the window.
              frame.pack();
              frame.setVisible(true);
          }
      
      
      
      
      private static void playMucicInWorkerThread() {
          final SwingWorker worker = new SwingWorker() {
      
      
       Toolkit.getDefaultToolkit().beep();
      
              return 1; //return value not used by this program
              }
      
           
              public void finished() {     //Runs on the event-dispatching thread.
              }
          };
          worker.start();
      }
      
      
      
      
         public ThreadTesting() {
      
             super(new GridLayout(0,1));
      
      
             icon = new ImageIcon("num_2.gif");              
             label = new JLabel();
             label.setIcon(icon);
      
             icon2 = new ImageIcon("num_5.gif");         
             label2 = new JLabel();
             label2.setIcon(icon2);
      
      
             panel = new JPanel();
      
             panel.add(label);
      
             add(panel);
      
             label.addMouseListener(this);
      
      
      
         } 
          
      
          public void mousePressed(MouseEvent e) {        
      
        label.setIcon(icon2);
      
      
      playMusicInWorkerThread(); 
      
      
      
          }
      
      
          
          public void mouseReleased(MouseEvent e) {
      
      label.setIcon(icon);
      
      
      playMusicInWorkerThread();
      
          }
          
          public void mouseEntered(MouseEvent e) {
          }
          
          public void mouseExited(MouseEvent e) {
          }
          
          public void mouseClicked(MouseEvent e) {
          }
      
      
      
      }   // end class ThreadTesting
      Message was edited by: wonderful123
      wonderful123
        • 1. Re: simultaneous graphics and music THREADS with SwingWorker but not workin
          807605
          Try:
          private static void playMucicInWorkerThread() {
              SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
                  public Void doInBackground() {
                      Toolkit.getDefaultToolkit().beep();
                      return null;
                  }
              };
              worker.execute();
          }
          Although you might note that this will only beep once, if you want more try
          private static void playMucicInWorkerThread() {
              SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
                  public Void doInBackground() {
                      while(true) {
                          Toolkit.getDefaultToolkit().beep();
                          try {
                              Thread.sleep(1000);
                          } catch (InterruptedException e) {
                              // Do nothing
                          }
                      }
                      return null;
                  }
              };
              worker.execute();
          }
          This will run while the application is running, beeping every 1 second.
          • 2. Re: simultaneous graphics and music THREADS with SwingWorker but not workin
            807605
            thank you very much!

            I made the recommended changes but unfortunately I still get two error messages:

            ThreadTesting.java:119: cannot find symbol
            symbol : method playMusic InWorkerThread()
            location: class ThreadTesting
            playMusicInWorkerThread();

            ThreadTesting.java:132: cannot find symbol
            symbol : method playMusic InWorkerThread()
            location: class ThreadTesting
            playMusicInWorkerThread();


            I would be really delighted if someone could still give advice to correct these two errors.
            Thank you!

            Here is the updated verson of the source code (still malfunction):
            import java.awt.*;
                import java.awt.event.*;
                import java.awt.datatransfer.*;
                import javax.swing.*;
            
            import java.io.*;
            import java.util.Scanner;
            
                import java.lang.Math.*;
            
                 
            public class ThreadTesting extends JPanel implements MouseListener  {   // implements MouseListener
            
                     private static JPanel panel;
                     private static JLabel label;
                     private static JPanel panel2;
                     private static JLabel label2;
            
                     private static ImageIcon icon;
                     private static ImageIcon icon2;
                     private static int laskuri;
            
                   
              //     private static JPanel container = new JPanel();
                     
            private static JFuguePlayer playWithJFugue;
            
            
            
               public static void main(String[] args) throws IOException
               { 
            
            
                   javax.swing.SwingUtilities.invokeLater(new Runnable() {
            
                      public void run() {
                         createAndShowMyGUI();
                      }
                   });
               }
            
            
            
            
               private static void createAndShowMyGUI() {
            
            
                   JFrame frame = new JFrame("Saiekokeilu");
                   frame.setExtendedState(Frame.MAXIMIZED_BOTH); // HUOM. miksi t�m� ei toimi?
            
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
                   JComponent newContentPane = new ThreadTesting();
                    newContentPane.setOpaque(true); //content panes must be opaque
                    frame.setContentPane(newContentPane);
                    
                    //Display the window.
                    frame.pack();
                    frame.setVisible(true);
                }
            
            
            
            
            private static void playMucicInWorkerThread() {
                SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
                    public Void doInBackground() {
                        Toolkit.getDefaultToolkit().beep();
                        return null;
                    }
                };
                worker.execute();
            }
            
            
            
            
            
            
               public ThreadTesting() {
            
                   super(new GridLayout(0,1));
            
            
                   icon = new ImageIcon("num_2.gif");              
                   label = new JLabel();
                   label.setIcon(icon);
            
                   icon2 = new ImageIcon("num_5.gif");         
                   label2 = new JLabel();
                   label2.setIcon(icon2);
            
            
                   panel = new JPanel();
            
                   panel.add(label);
            
                   add(panel);
            
                   label.addMouseListener(this);
            
            
            
               } 
            
                
            
                public void mousePressed(MouseEvent e) {        
            
              label.setIcon(icon2);
            
            
            playMusicInWorkerThread(); 
            
            
            
                }
            
            
                
                public void mouseReleased(MouseEvent e) {
            
            label.setIcon(icon);
            
            
            playMusicInWorkerThread();
            
                }
                
                public void mouseEntered(MouseEvent e) {
                }
                
                public void mouseExited(MouseEvent e) {
                }
                
                public void mouseClicked(MouseEvent e) {
                }
            
            
            }   // end class ThreadTesting
            • 3. Re: simultaneous graphics and music THREADS with SwingWorker but not workin
              807605
              Those errors are because you're calling playMusicInWorkerThread but you've named the method playMucicInWorkerThread (note the c instead of s).
              • 4. Re: simultaneous graphics and music THREADS with SwingWorker but not workin
                807605
                Thank you for your fantastic help!
                I got my program running! I am so happy!!
                T                                                  !
                   h                                           u
                       a                               o
                            n                 y 
                                    k
                -wonderful-