Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is what I have so far I would like to add a timer and a miss count. Please

ID: 3845015 • Letter: H

Question

Here is what I have so far I would like to add a timer and a miss count. Please help. The timer is a 1 minute countdown to end game.

package targetgame;

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()
{

File soundFile = new File("C:\Users\Documents\gun-gunshot-01.wav");
AudioInputStream stream;
try {
stream = AudioSystem.getAudioInputStream(soundFile);
clip = AudioSystem.getClip();
clip.open(stream);
} catch (UnsupportedAudioFileException | IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
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:\Users\Documents\bigbuck.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)
{
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("Monster Buck Hunt");
cg.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e)
{   
if(jrb1.isSelected())
{
r=new Random();
new Timer(1500,new ActionListener()   
{
public void actionPerformed(ActionEvent ae)
{
lb.setLocation(r.nextInt(getWidth()-65),r.nextInt(getHeight()-65));
}
}).start();
}
if(jrb2.isSelected())
{
r=new Random();
new Timer(500,new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
lb.setLocation(r.nextInt(getWidth()-65),r.nextInt(getHeight()-65));
}
}).start();

}
}
}

Explanation / Answer

first of all compile this program with java -d . TargetGame.java

run this program java targetgame.Targetgame

you will get expected output

small chages in mouseClicked event

take ImageIcon i1=new ImageIcon("bigbuck.jpg);

jlb.setForeground(Color.blue);

package targetgame;

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()

{

File soundFile = new File("PARROT.AU");

AudioInputStream stream;

try {

stream = AudioSystem.getAudioInputStream(soundFile);

clip = AudioSystem.getClip();

clip.open(stream);

} catch (UnsupportedAudioFileException | IOException e) {

e.printStackTrace();

} catch (LineUnavailableException e) {

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("photo.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)

{

clip.start();

ImageIcon i1=new ImageIcon("photo.jpg");

hitCount++;

jlb.setForeground(Color.blue);

jlb.setText("Hit count: "+hitCount);

}

});

setExtendedState(MAXIMIZED_BOTH);

}

public static void main(String args[])

{

TargetGame cg=new TargetGame();

cg.setVisible(true);

cg.setTitle("Monster Buck Hunt");

cg.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent e)

{   

if(jrb1.isSelected())

{

r=new Random();

new Timer(1500,new ActionListener()   

{

public void actionPerformed(ActionEvent ae)

{

lb.setLocation(r.nextInt(getWidth()-65),r.nextInt(getHeight()-65));

}

}).start();

}

if(jrb2.isSelected())

{

r=new Random();

new Timer(500,new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

lb.setLocation(r.nextInt(getWidth()-65),r.nextInt(getHeight()-65));

}

}).start();

}

}

}