Results 1 to 6 of 6
  1. #1
    eagle11 is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default MousePressed problem

    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.

    Java 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;
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: MousePressed problem

    Quote Originally Posted by eagle11 View Post
    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.
    What does 'it bugs' mean? Does your computer explode?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    eagle11 is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: MousePressed problem

    Quote Originally Posted by JosAH View Post
    What does 'it bugs' mean? Does your computer explode?

    kind regards,

    Jos
    If I click anywhere but the sound: The program stops executing and it says EventDispatchThread.run() line: not available
    If I click on the sound it plays the sound and then stops executing and says: The jar file acm.jar has no source attachment. Then there is a button attach source. Then there is public abstract class and some constant variables. Shall I copy it all or is it not important?
    I am using Eclipse as the editor btw.
    Also in the debugger it says: terminated exit value 1

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: MousePressed problem

    Quote Originally Posted by eagle11 View Post
    If I click anywhere but the sound: The program stops executing and it says EventDispatchThread.run() line: not available
    If I click on the sound it plays the sound and then stops executing and says: The jar file acm.jar has no source attachment. Then there is a button attach source. Then there is public abstract class and some constant variables. Shall I copy it all or is it not important?
    Your description of the error doesn't make sense to me, so, yes, a verbatim copy of the error messages is important. b.t.w. does your code compile without errors?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    eagle11 is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: MousePressed problem

    Quote Originally Posted by JosAH View Post
    Your description of the error doesn't make sense to me, so, yes, a verbatim copy of the error messages is important. b.t.w. does your code compile without errors?

    kind regards,

    Jos
    Well it bugs after running and it shows me no error in the code.

    This is what it writes in debugger after I click on the sound:
    Animal [Java Application]
    Animal at localhost:55069
    Thread [AWT-Shutdown] (Running)
    Thread [AWT-Windows] (Running)
    Thread [AWT-EventQueue-0] (Suspended (entry into method mouseClicked in Animal))
    Animal(Program).mouseClicked(MouseEvent) line: 900
    AWTEventMulticaster.mouseClicked(MouseEvent) line: not available
    GCanvas(Component).processMouseEvent(MouseEvent) line: not available
    GCanvas(Component).processEvent(AWTEvent) line: not available
    GCanvas(Container).processEvent(AWTEvent) line: not available
    GCanvas(Component).dispatchEventImpl(AWTEvent) line: not available
    GCanvas(Container).dispatchEventImpl(AWTEvent) line: not available
    GCanvas(Component).dispatchEvent(AWTEvent) line: not available
    LightweightDispatcher.retargetMouseEvent(Component , int, MouseEvent) line: not available
    LightweightDispatcher.processMouseEvent(MouseEvent ) line: not available
    LightweightDispatcher.dispatchEvent(AWTEvent) line: not available
    ProgramFrame(Container).dispatchEventImpl(AWTEvent ) line: not available
    ProgramFrame(Window).dispatchEventImpl(AWTEvent) line: not available
    ProgramFrame(Component).dispatchEvent(AWTEvent) line: not available
    EventQueue.dispatchEvent(AWTEvent) line: not available
    EventDispatchThread.pumpOneEventForFilters(int) line: not available
    EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: not available
    EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: not available
    EventDispatchThread.pumpEvents(int, Conditional) line: not available
    EventDispatchThread.pumpEvents(Conditional) line: not available
    EventDispatchThread.run() line: not available
    Thread [DestroyJavaVM] (Running)
    Thread [Java Sound Event Dispatcher] (Running)
    Thread [Direct Clip] (Running)


    And it also opens a new file in the eclipse saying this:
    MousePressed problem-filepart1.pngMousePressed problem-filepart2.pngMousePressed problem-filepart3.pngMousePressed problem-filepart4.pngMousePressed problem-filepart5.pngMousePressed problem-filepart6.png

    I am sorry its in the pictures I couldn't copy it.




    And the other version if I click anywhere else it says in the debugger this:
    Animal [Java Application]
    Animal at localhost:55065
    Thread [AWT-Shutdown] (Running)
    Thread [AWT-Windows] (Running)
    Thread [AWT-EventQueue-0] (Suspended (exception NullPointerException))
    EventDispatchThread.run() line: not available
    Thread [DestroyJavaVM] (Running)
    Thread [AWT-EventQueue-0] (Suspended (entry into method mouseClicked in Animal))
    Animal(Program).mouseClicked(MouseEvent) line: 900
    AWTEventMulticaster.mouseClicked(MouseEvent) line: not available
    GCanvas(Component).processMouseEvent(MouseEvent) line: not available
    GCanvas(Component).processEvent(AWTEvent) line: not available
    GCanvas(Container).processEvent(AWTEvent) line: not available
    GCanvas(Component).dispatchEventImpl(AWTEvent) line: not available
    GCanvas(Container).dispatchEventImpl(AWTEvent) line: not available
    GCanvas(Component).dispatchEvent(AWTEvent) line: not available
    LightweightDispatcher.retargetMouseEvent(Component , int, MouseEvent) line: not available
    LightweightDispatcher.processMouseEvent(MouseEvent ) line: not available
    LightweightDispatcher.dispatchEvent(AWTEvent) line: not available
    ProgramFrame(Container).dispatchEventImpl(AWTEvent ) line: not available
    ProgramFrame(Window).dispatchEventImpl(AWTEvent) line: not available
    ProgramFrame(Component).dispatchEvent(AWTEvent) line: not available
    EventQueue.dispatchEvent(AWTEvent) line: not available
    EventDispatchThread.pumpOneEventForFilters(int) line: not available
    EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: not available
    EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: not available
    EventDispatchThread.pumpEvents(int, Conditional) line: not available
    EventDispatchThread.pumpEvents(Conditional) line: not available
    EventDispatchThread.run() line: not available
    And eclipse opens a new file saying:
    EventDispatchThread.run() line: not available
    source not found

  6. #6
    eagle11 is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: MousePressed problem

    I figured it out. :) I still don't know where the problem was but putting try/catch in the mousePressed method seemed to help and also change the if statement so it compares using "==" not ".equals"

    Thank you Jos for helping :)

Similar Threads

  1. Problem with images on mousepressed.
    By bttc44 in forum New To Java
    Replies: 1
    Last Post: 12-10-2010, 08:39 AM
  2. MousePressed event help needed(CS106A )
    By ccie007 in forum New To Java
    Replies: 4
    Last Post: 11-09-2010, 07:29 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •