simultaneous graphics and music THREADS with SwingWorker but not working :(
Hello
please tell what is wrong with my definitions ofSwingWorker
probably just a small basic problem... i am running out of time, so your help is very valuable.
My Idea is to havegraphics activities in the main thread, so calledEvent dispatching thread
To avoid slow down i try toplay simultaneously music in another thread, so calledworker thread
I am a newbie, soplease 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.*;
publicclass ThreadTestingextends JPanelimplements MouseListener{// implements MouseListener
privatestatic JPanel panel;
privatestatic JLabel label;
privatestatic JPanel panel2;
privatestatic JLabel label2;
privatestatic ImageIcon icon;
privatestatic ImageIcon icon2;
privatestaticint laskuri;
//private static JPanel container = new JPanel();
privatestatic JFuguePlayer playWithJFugue;
publicstaticvoid main(String[] args)throws IOException
{
javax.swing.SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
createAndShowMyGUI();
}
});
}
privatestaticvoid createAndShowMyGUI(){
JFrame frame =new JFrame("Saiekokeilu");
frame.setExtendedState(Frame.MAXIMIZED_BOTH);// HUOM. miksi tm 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);
}
privatestaticvoid playMucicInWorkerThread(){
final SwingWorker worker =new SwingWorker(){
Toolkit.getDefaultToolkit().beep();
return 1;//return value not used by this program
}
publicvoid 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);
}
publicvoid mousePressed(MouseEvent e){
label.setIcon(icon2);
playMusicInWorkerThread();
}
publicvoid mouseReleased(MouseEvent e){
label.setIcon(icon);
playMusicInWorkerThread();
}
publicvoid mouseEntered(MouseEvent e){
}
publicvoid mouseExited(MouseEvent e){
}
publicvoid mouseClicked(MouseEvent e){
}
}// end class ThreadTesting
Message was edited by: wonderful123
wonderful123
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 tryprivate 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.
dwga at 2007-7-29 11:26:01 >

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 tm 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