simultaneous graphics and music THREADS with SwingWorker but not working :(
Hello
[I sent this originally to java essentials group but got no answer...]
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: wonderful
wonderful123

