Results 1 to 12 of 12
- 05-05-2011, 10:47 PM #1
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Need Help with Mouse Listener!!!!
//Here is my code
/*When I compile I getJava Code:import java.awt.Graphics; import java.awt.Color; import java.awt.Image; public class Checkers extends java.applet.Applet implements Runnable, MouseListener { MyMouseListenerClass m = new MyMouseListenerClass() ; void addMouseListener (m) ; Thread runner; int xpos; int ypos; Image offscreenImg; Graphics offscreenG; public void init() { offscreenImg = createImage(this.size().width, this.size().height); offscreenG = offscreenImg.getGraphics(); } public void start() { if (runner == null); { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void run() { while (true) { ypos = 100; for (xpos = 5; xpos <= 105; xpos+=4) { repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { } ypos-=4; } while(xpos >= 5) { repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { } xpos-=4; ypos+=4; } } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { // Draw background onto the buffer area offscreenG.setColor(Color.black); offscreenG.fillRect(0,0,100,100); offscreenG.setColor(Color.black); offscreenG.fillRect(100,100,100,100); offscreenG.setColor(Color.gray); offscreenG.fillRect(100,0,100,100); offscreenG.setColor(Color.gray); offscreenG.fillRect(0,100,100,100); // Draw checkers offscreenG.setColor(Color.red); offscreenG.fillOval(xpos,5,90,90); offscreenG.setColor(Color.blue); offscreenG.fillOval(ypos,100,90,90); // Now, transfer the entire buffer onto the screen g.drawImage(offscreenImg,0,0,this); } public void destroy() { offscreenG.dispose(); } } class MyMouseListenerClass extends MouseAdapter { public void mousePressed (MouseEvent e) { this.stop(); } public void mouseReleased (MouseEvent e) { this.start(); } }
Checkers.java:8: <identifier> expected
void addMouseListener (m) ;
^
1 error
Please help I'm pulling my hair out as I'm not able to figure it out and tis due tonight*/Last edited by Doggir; 05-05-2011 at 11:06 PM.
- 05-05-2011, 10:48 PM #2
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
I should note this is the assignement:
a) Write a modified checkers program with class name Checkers using double buffering. Your program must have two top squares and two bottom squares. There should be two checkers of different color. The two checkers always move in opposite directions. When a checker reaches the end of its path, it should start moving backwards in the opposite direction.
b) Enhance your Checkers program above to respond to mouse clicks. The checkers must stop moving whenever the user clicks and holds down the mouse. They must start moving again whenever the mouse is released.
-
Quite simply, that is not valid Java code.
What are you trying to do, create an addMouseListener method? or add a mouse listener to the applet? If you want to do the former, then you need to add a method body with a pair of curly braces and all the code that goes inside of them. If you want to do the latter, than you need to get rid of the void key work as it makes no sense in that spot and call the method in some method, likely the init method of the applet.
- 05-05-2011, 11:02 PM #4
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
I'm trying to do that later... and when i move the addMouseListener() to init() I get a whole bunch more errors:
Checkers.java:5: cannot find symbol
symbol: class MouseListener
public class Checkers extends java.applet.Applet implements Runnable, MouseListener
^
Checkers.java:101: cannot find symbol
symbol: class MouseAdapter
class MyMouseListenerClass extends MouseAdapter
^
Checkers.java:103: cannot find symbol
symbol : class MouseEvent
location: class MyMouseListenerClass
public void mousePressed (MouseEvent e)
^
Checkers.java:107: cannot find symbol
symbol : class MouseEvent
location: class MyMouseListenerClass
public void mouseReleased (MouseEvent e)
^
Checkers.java:17: addMouseListener(java.awt.event.MouseListener) in java.awt.Component cannot be applied to (MyMouseListenerClass)
addMouseListener(m);
^
Checkers.java:105: cannot find symbol
symbol : method stop()
location: class MyMouseListenerClass
this.stop();
^
Checkers.java:109: cannot find symbol
symbol : method start()
location: class MyMouseListenerClass
this.start();
^
Note: Checkers.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
7 errors
- 05-05-2011, 11:05 PM #5
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Edit: thanks in advanceJava Code:public void init() { addMouseListener(m); offscreenImg = createImage(this.size().width, this.size().height); offscreenG = offscreenImg.getGraphics(); }Last edited by Doggir; 05-05-2011 at 11:07 PM.
-
Show the new code. But please use code tags:
The tag [code] goes above your code block, and
the tag [/code] goes below your code block
For e.g.,
[code]
// code goes here
[/code]
-
- 05-05-2011, 11:17 PM #8
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
brought it down to 3 errors lolJava Code:import java.awt.Graphics; import java.awt.Color; import java.awt.Image; import java.awt.event.*; // just added
Checkers.java:6: Checkers is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener
public class Checkers extends java.applet.Applet implements Runnable, MouseListener
^
Checkers.java:106: cannot find symbol
symbol : method stop()
location: class MyMouseListenerClass
this.stop();
^
Checkers.java:110: cannot find symbol
symbol : method start()
location: class MyMouseListenerClass
this.start();
^
Note: Checkers.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
3 errors
-
- You're not implementing all of the methods for the interfaces your class is implementing. Go through their API's to see which ones (the error actually tells you which one).
- You've got two methods, start and stop, that you're calling, that simply don't exist. You can't make up methods and expect them to magically work. If your class needs a start and stop method, you must create it. Otherwise if you want to use pre-existing methods, then you can only use methods that are already in the class or its parent classes.
- 05-05-2011, 11:24 PM #10
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
methods are there at least these are the ones I'm 'trying' to callJava Code:public void start() { if (runner == null); { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; }
-
- 05-05-2011, 11:48 PM #12
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Fixed!!! TY SOOOOO MUCH!!! Ended up going a slightly different route then originally intended but you were A LOT of help!!!
Heres the finished code if anyone is lurking and has similar problem
Java Code:import java.awt.Graphics; import java.awt.Color; import java.awt.Image; import java.awt.event.*; public class Checkers extends java.applet.Applet implements Runnable, MouseListener { Thread runner; int xpos; int ypos; Image offscreenImg; Graphics offscreenG; public void init() { addMouseListener(this); xpos = 5; ypos = 100; offscreenImg = createImage(this.size().width, this.size().height); offscreenG = offscreenImg.getGraphics(); } public void start() { if (runner == null); { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void run() { while (true) { while(xpos <= 105) { repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { } ypos-=4; xpos+=4; } while(xpos >= 5) { repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { } xpos-=4; ypos+=4; } } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { // Draw background onto the buffer area offscreenG.setColor(Color.black); offscreenG.fillRect(0,0,100,100); offscreenG.setColor(Color.black); offscreenG.fillRect(100,100,100,100); offscreenG.setColor(Color.gray); offscreenG.fillRect(100,0,100,100); offscreenG.setColor(Color.gray); offscreenG.fillRect(0,100,100,100); // Draw checkers offscreenG.setColor(Color.red); offscreenG.fillOval(xpos,5,90,90); offscreenG.setColor(Color.blue); offscreenG.fillOval(ypos,100,90,90); // Now, transfer the entire buffer onto the screen g.drawImage(offscreenImg,0,0,this); } public void destroy() { offscreenG.dispose(); } public void mousePressed (MouseEvent e) { this.stop(); } public void mouseReleased (MouseEvent e) { this.start(); } public void mouseClicked(MouseEvent arg0) {} public void mouseEntered(MouseEvent arg0) {} public void mouseExited(MouseEvent arg0) {} }
Similar Threads
-
Mouse Listener
By Quizerno in forum New To JavaReplies: 8Last Post: 03-17-2011, 05:25 AM -
How to add mouse clicked listener on rows of a jtable
By pink123 in forum AWT / SwingReplies: 4Last Post: 02-23-2011, 09:25 PM -
inner class Mouse listener not working in MVC model
By Grant in forum AWT / SwingReplies: 2Last Post: 03-01-2010, 03:19 PM -
Mouse Listener for mouse floating over object?
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-18-2009, 04:34 AM -
change the delay between double click in mouse listener
By itaipee in forum AWT / SwingReplies: 6Last Post: 03-17-2009, 02:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks