I have a problem with the mousePressed method. In the Canvas I have 5 Images so far. A sound image and four images of animals (options). When user clicks on sound it should play sound if they click on button if its the right one it just congrats.
The problem is that when I click on anything but the sound it just bugs. If I click on sound image it plays the sound and then bugs.
Originally I had this in mouseClicked method but it bugged even more so I changed it.
Thanks in advance.
Code:import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
import acm.io.*;
import acm.program.*;
import acm.util.*;
import acm.graphics.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class Animal extends GraphicsProgram {
// Name of the file with the rounds
private final static String FILENAME="Animal.txt";
private final static String SOUNDPIC="Sound.png";
//window size
public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 500;
// Dimensions of game board
private static final int WIDTH = APPLICATION_WIDTH;
private static final int HEIGHT = APPLICATION_HEIGHT;
//Offset between buttons
private static final int X_OFFSET= 40;
private static final int Y_OFFSET= 40;
//options size
private static final int O_WIDTH= 60;
private static final int O_HEIGHT= 60;
public void run() {
//open file
BufferedReader rd=openfile(FILENAME);
CreateSoundPic();
//sets the initial position for each option;
PrepareOptions();
try {
while (true) {
String Line=rd.readLine();
if (Line==null) break;
soundname= Getsound(Line);
WriteOptions(Line);
}
}
catch (IOException ex) {
throw new ErrorException(ex);
}
addMouseListeners();
}
private BufferedReader openfile(String file){
BufferedReader rd=null;
try {
rd= new BufferedReader( new FileReader(file));
}
catch (IOException ex) {
throw new ErrorException(ex);
}
return rd;
}
private String Getsound( String line) {
StringTokenizer tokenizer= new StringTokenizer(line);
return tokenizer.nextToken();
}
private void PlaySound(String sound) {
String soundname= sound.toLowerCase() + ".au";
AudioClip AnimalClip = MediaTools.loadAudioClip(soundname);
AnimalClip.play();
}
private void WriteOptions( String Line) {
StringTokenizer tokenizer= new StringTokenizer(Line);
int i=2;
correct=i;
int l=0;
while(tokenizer.hasMoreTokens()) {
SetImage(tokenizer.nextToken(),(i+l)%4);
l++;
}
}
private void PrepareOptions() {
Options[0]= new GImage("bear.png");
Options[1]= new GImage("bear.png");
Options[2]= new GImage("bear.png");
Options[3]= new GImage("bear.png");
Options[0].setSize(O_WIDTH,O_HEIGHT);
Options[0].setLocation(WIDTH/2-X_OFFSET/2-O_WIDTH,HEIGHT/2);
Options[1].setSize(O_WIDTH,O_HEIGHT);
Options[1].setLocation(WIDTH/2+X_OFFSET/2,HEIGHT/2);
Options[2].setSize(O_WIDTH,O_HEIGHT);
Options[2].setLocation(WIDTH/2-X_OFFSET/2-O_WIDTH,HEIGHT/2+O_HEIGHT+ Y_OFFSET);
Options[3].setSize(O_WIDTH,O_HEIGHT);
Options[3].setLocation(WIDTH/2+X_OFFSET/2,HEIGHT/2+O_HEIGHT+ Y_OFFSET);
}
private void SetImage(String picture,int option) {
if (picture.equals("bear")) {
Options[option].setImage(picture +".png");
Options[option].setSize(O_WIDTH,O_HEIGHT);
}
else {
Options[option].setImage(picture +".jpg");
Options[option].setSize(O_WIDTH,O_HEIGHT);
}
add(Options[option]);
}
private void CreateSoundPic() {
Sound= new GImage(SOUNDPIC);
Sound.scale(0.5);
Sound.setLocation(WIDTH/2-Sound.getWidth()/2,HEIGHT/4-Sound.getHeight()/2);
add(Sound);
}
public void mousePressed(MouseEvent e) {
if(getElementAt(e.getX(), e.getY()).equals(Sound)) {
PlaySound(soundname);
}
else if (getElementAt(e.getX(), e.getY()).equals(Options[correct])) {
add(new GLabel("You did it",0,0));
}
}
private GImage Sound;
private GImage[] Options= new GImage [4];
private RandomGenerator rgen = RandomGenerator.getInstance();
private String soundname;
private int correct=0;
}

