How do i add a wav sound in stead of the beep? Here is my code import javax.swin
ID: 3841142 • Letter: H
Question
How do i add a wav sound in stead of the beep? Here is my code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;
class TargetGame extends JFrame implements ActionListener
{
JLabel lb, lb1, jlb;
JPanel jpan, jpan2, jpan3;
JRadioButton jrb1, jrb2;
JOptionPane jp;
ButtonGroup bg;
int hitCount=0;
Random r;
public TargetGame()
{
jpan=new JPanel(new BorderLayout(50,50));
jpan2=new JPanel(new FlowLayout());
jpan3=new JPanel(new FlowLayout());
jrb1=new JRadioButton("Easy");
jrb2=new JRadioButton("Hard");
bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
lb=new JLabel(new ImageIcon("C:\Documents\target.jpg"));
lb.setSize(20, 30);
jlb=new JLabel("Hit count: "+hitCount);
jpan3.add(jrb1);
jpan3.add(jrb2);
jpan3.add(jlb);
jpan3.setBackground(Color.GREEN);
jpan2.add(lb);
jpan2.setBackground(Color.GREEN);
jpan.add(jpan2);
jpan.add(jpan3, BorderLayout.SOUTH);
jpan.setBackground(Color.GREEN);
add(jpan);
jrb1.addActionListener((ActionListener) this);
jrb2.addActionListener((ActionListener) this);
lb.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
Toolkit.getDefaultToolkit().beep();
hitCount++;
jlb.setText("Hit count: "+hitCount);
}
});
setExtendedState(MAXIMIZED_BOTH);
}
public static void main(String args[])
{
TargetGame cg=new TargetGame();
cg.setVisible(true);
cg.setTitle("Hit the Target");
cg.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(jrb1.isSelected())
{
r=new Random();
new Timer(1000,new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
lb.setLocation(r.nextInt(getWidth()-75),r.nextInt(getHeight()-75));
}
}).start();
}
if(jrb2.isSelected())
{
r=new Random();
new Timer(500,new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
lb.setLocation(r.nextInt(getWidth()-75),r.nextInt(getHeight()-75));
}
}).start();
}
}
}
Explanation / Answer
PROGRAM CODE:
package com;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
class TargetGame extends JFrame implements ActionListener
{
JLabel lb, lb1, jlb;
JPanel jpan, jpan2, jpan3;
JRadioButton jrb1, jrb2;
JOptionPane jp;
ButtonGroup bg;
int hitCount=0;
Random r;
Clip clip;
public TargetGame()
{
//reading the sound file and making a clip
//provide the actual path here
File soundFile = new File("C:\...Click.wav");
AudioInputStream stream;
try {
stream = AudioSystem.getAudioInputStream(soundFile);
clip = AudioSystem.getClip();
clip.open(stream);
} catch (UnsupportedAudioFileException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jpan=new JPanel(new BorderLayout(50,50));
jpan2=new JPanel(new FlowLayout());
jpan3=new JPanel(new FlowLayout());
jrb1=new JRadioButton("Easy");
jrb2=new JRadioButton("Hard");
bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
lb=new JLabel(new ImageIcon("C:\Documents\target.jpg"));
lb.setSize(20, 30);
jlb=new JLabel("Hit count: "+hitCount);
jpan3.add(jrb1);
jpan3.add(jrb2);
jpan3.add(jlb);
jpan3.setBackground(Color.GREEN);
jpan2.add(lb);
jpan2.setBackground(Color.GREEN);
jpan.add(jpan2, BorderLayout.NORTH);
jpan.add(jpan3, BorderLayout.SOUTH);
jpan.setBackground(Color.GREEN);
add(jpan);
jrb1.addActionListener((ActionListener) this);
jrb2.addActionListener((ActionListener) this);
lb.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
//Toolkit.getDefaultToolkit().beep();
//playing the clip on mouse click
clip.start();
hitCount++;
jlb.setText("Hit count: "+hitCount);
}
});
setExtendedState(MAXIMIZED_BOTH);
}
public static void main(String args[])
{
TargetGame cg=new TargetGame();
cg.setVisible(true);
cg.setTitle("Hit the Target");
cg.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(jrb1.isSelected())
{
r=new Random();
new Timer(1000,new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
lb.setLocation(r.nextInt(getWidth()-75),r.nextInt(getHeight()-75));
}
}).start();
}
if(jrb2.isSelected())
{
r=new Random();
new Timer(500,new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
lb.setLocation(r.nextInt(getWidth()-75),r.nextInt(getHeight()-75));
}
}).start();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.