5 Replies - 2048 Views - Last Post: 01 June 2014 - 12:24 PM Rate Topic: -----

#1 totalnewbie1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-May 14

is not abstract and does not override abstract method - what to do!

Posted 07 May 2014 - 02:33 AM

Hi, trying to follow this simple example of canvas component and mouselistener from a book. i did the code to the letter but it still comes out with this problem with abstract methods(i suppose), and the book hasnt mentioned anything about those so far. help anyone?

/*

 CanvasMouseTest

* Tests the Canvas component and MouseListener interface

*/


import java.awt.*;

import java.awt.event.*;

public class CanvasMouseTest extends GUIFrame implements MouseListener {

	
	public CanvasMouseTest() {

		super("Canvas and MouseListener Test");

		Canvas canv = new Canvas();

		canv.setSize(200, 200);

		canv.addMouseListener(this);

		canv.setBackground(Color.red);

		add(canv);

		pack();

		setVisible(true);

		}


		public void mousePressed(MouseEvent event){

			Point p = event.getPoint();

			System.out.println("Mouse Pressed at: " + p);

			Graphics g = ((Canvas)event.getSource()).getGraphics();

			g.fillOval(p.x, p.y, 10, 10);

		}

		public static void main(String args[]) {



			new CanvasMouseTest();

		}

	}



Is This A Good Question/Topic? 0
  • +

Replies To: is not abstract and does not override abstract method - what to do!

#2 x68zeppelin80x   User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: is not abstract and does not override abstract method - what to do!

Posted 07 May 2014 - 02:45 AM

If you are going to use the MouseListener interface and CanvasMouseTest is not abstract, you must implement ALL the following methods:

// Invoked when the mouse button has been clicked on a component.
mouseClicked(e:MouseEvent):void 
// Invoked when the mouse enters a component.
mouseEntered(e:MouseEvent):void
// Invoked when the mouse exits a component.
mouseExited(e:MouseEvent):void 
// Invoked when a mouse button has been pressed on a component.
mousePressed(e:MouseEvent):void
// Invoked when a mouse button has been released on a component.
mouseReleased(e:MouseEvent):void 


Please see: java.awt.event.MouseListener

The easiest way to get around this is to just add these four lines to the end of CanvasMouseTest:

public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }

This post has been edited by x68zeppelin80x: 07 May 2014 - 02:48 AM

Was This Post Helpful? 3
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11776
  • View blog
  • Posts: 19,987
  • Joined: 19-March 11

Re: is not abstract and does not override abstract method - what to do!

Posted 07 May 2014 - 07:08 AM

View Postx68zeppelin80x, on 07 May 2014 - 04:45 AM, said:

If you are going to use the MouseListener interface and CanvasMouseTest is not abstract, you must implement ALL the following methods:


This is correct. An interface declaration is sort of like a contract: it's a promise to the compiler that this class knows how to perform certain actions. If you don't define the required methods, you're not fulfilling your side of the contract, and the compiler won't accept the class.
In return, the compiler will know that you can treat this class as a MouseListener - regardless of what else it knows how to do - because it knows how to respond to certain mouse events.

Quote

The easiest way to get around this is to just add these four lines to the end of CanvasMouseTest:

public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }


The reason this works is that the compiler has methods to call in case it gets a mouseClicked event, or a mouseEntered, or a mouseExited, or a mouseReleased. The fact that these methods do nothing is not important, because the compiler assumes you know what you're doing that that you want nothing to happen there - but it has to be explicitly told this.
Was This Post Helpful? 2
  • +
  • -

#4 totalnewbie1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-May 14

Re: is not abstract and does not override abstract method - what to do!

Posted 01 June 2014 - 11:37 AM

View Postx68zeppelin80x, on 07 May 2014 - 02:45 AM, said:

If you are going to use the MouseListener interface and CanvasMouseTest is not abstract, you must implement ALL the following methods:

// Invoked when the mouse button has been clicked on a component.
mouseClicked(e:MouseEvent):void 
// Invoked when the mouse enters a component.
mouseEntered(e:MouseEvent):void
// Invoked when the mouse exits a component.
mouseExited(e:MouseEvent):void 
// Invoked when a mouse button has been pressed on a component.
mousePressed(e:MouseEvent):void
// Invoked when a mouse button has been released on a component.
mouseReleased(e:MouseEvent):void 


Please see: java.awt.event.MouseListener

The easiest way to get around this is to just add these four lines to the end of CanvasMouseTest:

public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }


yeh that did it! thanks!
Was This Post Helpful? 0
  • +
  • -

#5 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: is not abstract and does not override abstract method - what to do!

Posted 01 June 2014 - 12:06 PM

View Postx68zeppelin80x, on 07 May 2014 - 09:45 AM, said:

The easiest way to get around this is to just add these four lines to the end of CanvasMouseTest:

public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }


The easiest way to get around that is to use a MouseAdapter object and only override the methods you need, unless you really need your class to be of type MouseListener.

In this case, I don't really see the need for this class to be a MouseListener.

This post has been edited by farrell2k: 01 June 2014 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: is not abstract and does not override abstract method - what to do!

Posted 01 June 2014 - 12:24 PM

If you have another class whose sole responsibility is handling the mouse events, then you can make it extend the MouseAdapter class.

Instead of making each method abstract and forcing you to implement them all, you can just override the ones you are interested in. The rest will be left to their default blank implementations in MouseAdapter.

Of course you can't use that in your code as is as you are already extending another class. However, ideally you would have some other class (possibly anonymous or internal to your current one) to handle the events which can make use of MouseAdapter. Just makes your code a little cleaner :)

Edit: Ah farrell2k beat me to it!

This post has been edited by Ryano121: 01 June 2014 - 12:24 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1