Results 1 to 12 of 12
  1. #1
    Doggir is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default Need Help with Mouse Listener!!!!

    //Here is my code
    Java 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();
    		}
    }
    /*When I compile I get

    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.

  2. #2
    Doggir is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Doggir View Post
    //Here is my code

    Java 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) ;  [color="red"]// not valid [/color]
    Please help I'm pulling my hair out as I'm not able to figure it out and tis due tonight*/
    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.

  4. #4
    Doggir is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    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

  5. #5
    Doggir is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    public void init()
      {
      	addMouseListener(m);
       offscreenImg = createImage(this.size().width, this.size().height);
       offscreenG = offscreenImg.getGraphics(); 
      }
    Edit: thanks in advance
    Last edited by Doggir; 05-05-2011 at 11:07 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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]

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Doggir View Post
    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
    Perhaps you need to import the classes that can't be found?

  8. #8
    Doggir is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.Image;
    import java.awt.event.*; // just added
    brought it down to 3 errors lol


    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

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    • 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.

  10. #10
    Doggir is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    public void start() 
      {
        if (runner == null); 
    	 {
          runner = new Thread(this);
          runner.start();
        }
      }
    
      public void stop() 
      {
        if (runner != null) 
    	 {
          runner.stop();
          runner = null;
        }
    methods are there at least these are the ones I'm 'trying' to call

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Doggir View Post
    Java Code:
    public void start() 
      {
        if (runner == null); 
    	 {
          runner = new Thread(this);
          runner.start();
        }
      }
    
      public void stop() 
      {
        if (runner != null) 
    	 {
          runner.stop();
          runner = null;
        }
    methods are there at least these are the ones I'm 'trying' to call
    They are not methods of the class that is calling them.

  12. #12
    Doggir is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    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

  1. Mouse Listener
    By Quizerno in forum New To Java
    Replies: 8
    Last Post: 03-17-2011, 05:25 AM
  2. Replies: 4
    Last Post: 02-23-2011, 09:25 PM
  3. Replies: 2
    Last Post: 03-01-2010, 03:19 PM
  4. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 AM
  5. Replies: 6
    Last Post: 03-17-2009, 02:23 AM

Posting Permissions

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