Witam. To na początek kod
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import sun.audio.*;
public class EventClass
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override public void run()
{
ViewFrame frame=new ViewFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class ViewFrame extends JFrame
{
public ViewFrame()
{
this.setTitle("Zdarzenia");
//Ustawiamy rozmiar ramki
Dimension size=ViewFrame.getFrameSize();
this.setSize(size.width/2, size.height/2);
this.setLocationByPlatform(true);
//Wgrywamu do programu dzwiek
try
{
InputStream input=new FileInputStream(new File("laught.wav"));
voice=new AudioStream(input);
}
catch(IOException e)
{
e.printStackTrace();
}
//Tworzymy przyciski
this.buttonPanel=new ViewPanel();
JButton start=new JButton("Start");
JButton stop=new JButton("Stop");
//Dodajemy sluchacza do przyciskow
start.addActionListener(new ActionListener()
{
@Override public void actionPerformed(ActionEvent event)
{
try
{
AudioData date=ViewFrame.this.voice.getData();
ViewFrame.this.stream=new ContinuousAudioDataStream(date);
AudioPlayer.player.start(stream);
}
catch(IOException e)
{
e.printStackTrace();
}
}
});
stop.addActionListener(new ActionListener()
{
@Override public void actionPerformed(ActionEvent event)
{
AudioPlayer.player.stop(ViewFrame.this.stream);
}
});
//Dodanie przyciskow do panelu
this.buttonPanel.add(start);
this.buttonPanel.add(stop);
this.add(buttonPanel);
}
//Zwraca obiekt typu Dimension. Wykorzystamy by ustawic rozmiar ramki
public static Dimension getFrameSize()
{
Toolkit kit=Toolkit.getDefaultToolkit();
return kit.getScreenSize();
}
private ViewPanel buttonPanel;
private AudioStream voice;
private ContinuousAudioDataStream stream;
}
class ViewPanel extends JPanel
{
public ViewPanel()
{
try
{
this.img=ImageIO.read(new File("jaba2.jpg"));
}
catch(IOException e)
{
e.printStackTrace();
}
}
@Override public void paintComponent(Graphics g)
{
//Ustawienie rozmiaru obrazka tak by pokryl cala ramke
Dimension imageSize=ViewFrame.getFrameSize();
int x=imageSize.width /2;
int y=imageSize.height /2;
g.drawImage(this.img,0, 0, x, y, null);
}
private Image img;
}
Przypisałem do buttonów dwie akcje. Sprawa wygląda tak. Odpalam dźwięk potem stopuje. po ponownym odpaleniu nie smiga. Co jest nie tak?