Results 1 to 6 of 6
Thread: MousePressed problem
- 04-02-2012, 08:52 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
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; }
- 04-02-2012, 09:53 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 04-02-2012, 10:01 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: MousePressed problem
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
- 04-02-2012, 10:20 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: MousePressed problem
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-02-2012, 10:56 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: MousePressed problem
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:






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
- 04-03-2012, 04:05 PM #6
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Problem with images on mousepressed.
By bttc44 in forum New To JavaReplies: 1Last Post: 12-10-2010, 08:39 AM -
MousePressed event help needed(CS106A )
By ccie007 in forum New To JavaReplies: 4Last Post: 11-09-2010, 07:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks