Is not abstract and does not override abstract method! Guidance wh

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 23766 Views - Last Post: 16 May 2010 - 04:35 PM Rate Topic: -----

#1 Guest_Jayden*


Reputation:

Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 10:14 AM

Hi basically I'm getting an override abstract method error. It is stating that there is something wrong with the public class. However I seem to not see what the problem. This is my error;

java:9: DrawPanel is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener
public class DrawPanel extends JPanel implements MouseListener

And this is my code.

import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

public class DrawPanel extends JPanel implements MouseListener
	{
		Connect4Model model;
		int newCol;
		int newRow;

		public int numCols;
		public int numRows;

		public DrawPanel(int nc, int nr)
		{
			numCols = nc;
			numRows = nr;
			addMouseListener(this);
		}
		int getCol(int x)
		{
			return x*newCol/getWidth();
		}
		int getRow(int y)
		{
			return y*newRow/getHeight();
		}

		public void mouseReleased(MouseEvent event)
		{
		}
		public void mousePressed(MouseEvent event)
		{
		}
		public void mouseClicked(MouseEvent event)
		{
			int thisCol = getCol(event.getX());
			model.go(thisCol);

			if(model.autoplay == 1)
			{
				System.out.println("Two Player game");
			}
			else if(model.autoplay == 2)
			{

				thisCol = model.getSuggestedMove(model.getNextPlayer());
				model.go(thisCol);
			}
			else if(model.autoplay == 3)
			{
				thisCol = model.getSuggestedMove(model.getNextPlayer());
				model.go(thisCol);
			}

			repaint();

		}
}


Is This A Good Question/Topic? 0

Replies To: Is not abstract and does not override abstract method! Guidance wh

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 10:20 AM

Since your class implements MouseListener it must override every one of the methods in that interface. Even if it won't be used, you need to provide it, so just make it empty:

public void mouseExited(MouseEvent event)
{
}


Was This Post Helpful? 0
  • +
  • -

#3 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 10:20 AM

here you are implementing the MouseListener interface in the class DrawPanel
this means that you must either you have to implement all the methods of the MouseListener interface in the same class
or you have to mark the class as abstract and then implement some or all of the methods into the
first concrete subclass of that extends the DrawPanel class
hopes this hint helps you
Implement the MouseExited() method.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Jayden*


Reputation:

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 10:29 AM

View PostKYA, on 16 May 2010 - 09:20 AM, said:

Since your class implements MouseListener it must override every one of the methods in that interface. Even if it won't be used, you need to provide it, so just make it empty:

public void mouseExited(MouseEvent event)
{
}



I tried that method you said to do.I did do it and it gave me the same error but was basically asking me to do the same for mouseEntered. So added the following method;

public void mouseEntered(MouseEvent event)
		{
		}


Having added that the program then threw the following error;

DrawPanel.java:41: illegal character: \28
public void mouseEntered(MouseEvent event)

Sorry to be a nuisance.
Was This Post Helpful? 0

#5 Guest_Jayden*


Reputation:

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 10:32 AM

View Postpdkharkar, on 16 May 2010 - 09:20 AM, said:

here you are implementing the MouseListener interface in the class DrawPanel
this means that you must either you have to implement all the methods of the MouseListener interface in the same class
or you have to mark the class as abstract and then implement some or all of the methods into the
first concrete subclass of that extends the DrawPanel class
hopes this hint helps you
Implement the MouseExited() method.


I did the hint that you gave me and then when i compiled it, it threw up the same error basically asking to do the same for MouseEntered which i did and it then threw up the following error;


public void mouseEntered(MouseEvent event)
{
}
Was This Post Helpful? 0

#6 Guest_Jayden*


Reputation:

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 10:32 AM

View Postpdkharkar, on 16 May 2010 - 09:20 AM, said:

here you are implementing the MouseListener interface in the class DrawPanel
this means that you must either you have to implement all the methods of the MouseListener interface in the same class
or you have to mark the class as abstract and then implement some or all of the methods into the
first concrete subclass of that extends the DrawPanel class
hopes this hint helps you
Implement the MouseExited() method.


I did the hint that you gave me and then when i compiled it, it threw up the same error basically asking to do the same for MouseEntered which i did and it then threw up the following error;


DrawPanel.java:41: illegal character: \28
public void mouseEntered(MouseEvent event)
Was This Post Helpful? 0

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 10:34 AM

No worries, updated code listing please.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Jayden*


Reputation:

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 10:36 AM

View PostKYA, on 16 May 2010 - 09:34 AM, said:

No worries, updated code listing please.


Heres the updated code

import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

public class DrawPanel extends JPanel implements MouseListener
	{
		Connect4Model model;
		int newCol;
		int newRow;

		public int numCols;
		public int numRows;

		public DrawPanel(int nc, int nr)
		{
			numCols = nc;
			numRows = nr;
			addMouseListener(this);
		}
		int getCol(int x)
		{
			return x*newCol/getWidth();
		}
		int getRow(int y)
		{
			return y*newRow/getHeight();
		}
		public void mouseExited(MouseEvent event)
		{
		}
		public void mouseEntered(MouseEvent event)
		{
		}
		public void mouseReleased(MouseEvent event)
		{
		}

		public void mousePressed(MouseEvent event)
		{
		}

		public void mouseClicked(MouseEvent event)
		{
			int thisCol = getCol(event.getX());
			model.go(thisCol);

			if(model.autoplay == 1)
			{
				System.out.println("Two Player game");
			}
			else if(model.autoplay == 2)
			{

				thisCol = model.getSuggestedMove(model.getNextPlayer());
				model.go(thisCol);
			}
			else if(model.autoplay == 3)
			{
				thisCol = model.getSuggestedMove(model.getNextPlayer());
				model.go(thisCol);
			}

			repaint();

		}

}

Was This Post Helpful? 0

#9 Matteo1988  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 11-February 10

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 10:44 AM

  @Override
    public void mousePressed(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

...and it will be ok:)
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Jayden*


Reputation:

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 10:50 AM

View PostMatteo1988, on 16 May 2010 - 09:44 AM, said:

  @Override
    public void mousePressed(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

...and it will be ok:)


Correct me if I'm wrong but have you just made the methods so that when I compile it, it overrides it?
Was This Post Helpful? 0

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 10:52 AM

Actually the error indicates there is some encoding issues in the actual source file itself and the compiler is complaining that it can't read it.

\28 is the File Separator


The encoding needs to be the same for the file as whatever javac is expecting/using.
Was This Post Helpful? 0
  • +
  • -

#12 Guest_Jayden*


Reputation:

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 10:52 AM

View PostJayden, on 16 May 2010 - 09:50 AM, said:

View PostMatteo1988, on 16 May 2010 - 09:44 AM, said:

  @Override
    public void mousePressed(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

...and it will be ok:)


Correct me if I'm wrong but have you just made the methods so that when I compile it, it overrides it?


Because the @Override hides the method correct?
Was This Post Helpful? 0

#13 Guest_Jayden*


Reputation:

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 11:02 AM

If im getting the same error as asked before would i need to do the same @Override method?


import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

public class Connect4View extends JFrame
{
	DrawPanel canvas;
	Connect4Model model;
	ControlPanel myControlPanel, JRadioButton;

	int newCol;
	int newRow;


	public static void main(String[] args)
	{
		Connect4View w = new Connect4View();
		w.setVisible(true);
	}

	public Connect4View()
	{
		setTitle("Connect4 solution");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(600,220);
		setLocation(300,300);

		int number_of_columns = 7;
		int number_of_rows = 9;

		model = new Connect4Model();
		canvas = new DrawPanel(number_of_columns,number_of_rows);
		myControlPanel = new ControlPanel();


		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		add(canvas, BorderLayout.CENTER);
		add(myControlPanel, BorderLayout.SOUTH);
 	}
/*	class ControlPanel extends JPanel implements ActionListener
	{
		JButton resetButton;
		JRadioButton twoPlayerButton;
		JRadioButton autoRedButton;
		JRadioButton autoYellowButton;
		JTextField numRows;
		JTextField numCols;

		public ControlPanel()
		{

			resetButton = new JButton("Reset Board");
			add(resetButton);
			resetButton.addActionListener(this);

			twoPlayerButton = new JRadioButton("Two Player Game");
			twoPlayerButton.addActionListener(this);
			add(twoPlayerButton);
			twoPlayerButton.setSelected(true);

			autoRedButton = new JRadioButton("Red Button");
			autoRedButton.addActionListener(this);
			add(autoRedButton);

			autoYellowButton = new JRadioButton("Yellow Button");
			autoYellowButton.addActionListener(this);
			add(autoYellowButton);

			ButtonGroup group = new ButtonGroup();
			group.add(twoPlayerButton);

			ButtonGroup group2 = new ButtonGroup();
			group.add(autoRedButton);

			ButtonGroup group3 = new ButtonGroup();
			group.add(autoYellowButton);

			numRows = new JTextField(4);
			add(numRows);
			numRows.addActionListener(this);
			numRows.setText("9");
			newCol = Integer.parseInt(numRows.getText());


			numCols = new JTextField(4);
			add(numCols);
			numCols.addActionListener(this);
			numCols.setText("7");
			newRow = Integer.parseInt(numCols.getText());

			model.reset(newCol, newRow);
			canvas.repaint();
	}*/

		public void actionPerformed(ActionEvent event)
		{

			if(event.getSource()==twoPlayerButton)
			{
				System.out.println("Selected Two Player");
				model.setAutoplay(1);
				System.out.println("Two Player game");
			}
			else if(event.getSource()==autoRedButton)
			{
				System.out.println("Selected Red Button");
				model.setAutoplay(2);
				System.out.println("Computer Playing Yellow");

			}
			else if(event.getSource()==autoYellowButton)
			{
				System.out.println("Selected Yellow Button");
				model.setAutoplay(3);
				System.out.println("Computer Playing Red ");
				int thisCol;
				thisCol = model.getSuggestedMove(model.getNextPlayer());
				model.go(thisCol);
			}
			else if(event.getSource()==resetButton)
			{
				newCol = Integer.parseInt(numRows.getText());
				newRow = Integer.parseInt(numCols.getText());

				model.reset(newCol, newRow);
				System.out.println("Colum is " + numCols.getText());
				System.out.println("Rows is " + numRows.getText());
				canvas.repaint();
			}
			canvas.repaint();
		}
	}
	/*class DrawPanel extends JPanel implements MouseListener
	{
		public int numCols;
		public int numRows;

		public DrawPanel(int nc, int nr)
		{
			numCols = nc;
			numRows = nr;
			addMouseListener(this);
		}
		int getCol(int x)
		{
			return x*newCol/getWidth();
		}
		int getRow(int y)
		{
			return y*newRow/getHeight();
		}

		public void mouseReleased(MouseEvent event)
		{
		}
		public void mousePressed(MouseEvent event)
		{
		}
		public void mouseClicked(MouseEvent event)
		{
			int thisCol = getCol(event.getX());
			model.go(thisCol);

			if(model.autoplay == 1)
			{
				System.out.println("Two Player game");
			}
			else if(model.autoplay == 2)
			{

				thisCol = model.getSuggestedMove(model.getNextPlayer());
				model.go(thisCol);
			}
			else if(model.autoplay == 3)
			{
				thisCol = model.getSuggestedMove(model.getNextPlayer());
				model.go(thisCol);
			}
			//model.firstGo();
			repaint();

		}*/
		public void mouseEntered(MouseEvent event)
		{
		}
		public void mouseExited(MouseEvent event)
		{
		}
		Rectangle getRect(int thisCol, int thisRow)
		{
			// if input is out of range, return "null"
			if(thisCol <0 || thisRow < 0)
				return null;
			if(thisCol>=newCol || thisRow>=newRow)
				return null;

			// otherwise, make and return the Rectangle
			int w = getWidth()/newCol;
			int h = getHeight()/newRow;

			int x = thisCol*w;
			int y = thisRow*h;

			Rectangle myRect = new Rectangle(x,y,w,h);
			return myRect;
		}

		public void paint(Graphics g)
		{
			g.setColor(Color.gray);
			g.fillRect(0,0,getWidth(), getHeight());
			g.setColor(Color.black);


			Graphics2D g2 = (Graphics2D)g;
			// we'll use Graphics2D for it's "draw" method -
			// neater than the Graphics "drawRect" suppled
			// (which you could also use)

			for (int i = 0;i<newCol;i++)
				for(int j = 0;j<newRow;j++)
					g2.draw(getRect(i,j));

			for (int thisCol = 0; thisCol < model.getNumCols(); thisCol ++)
			{
				int num_of_counters = model.getNumCounters(thisCol);
				//System.out.println("col " + thisCol + " has " + num_of_counters);
				for (int counter=0; counter < num_of_counters; counter ++)
				{
					int colour = model.getCounter(thisCol, counter);
					if(colour==Connect4Column.RED_COUNTER)
						g2.setColor(Color.red);
					else if(colour==Connect4Column.YELLOW_COUNTER)
						g2.setColor(Color.yellow);

					Rectangle r = getRect(thisCol, newRow-counter-1);
					if (r != null)
						g2.fillOval(r.x, r.y, r.width, r.height);
				}
			}
		}
	}

 }



Was This Post Helpful? 0

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 11:06 AM

The @Override is an Annotation that specifies that a method has been overridden. It really isn't necessary to override a method, as you just have to redefine a method from a superclass or interface with the correct header and param list to override it.

Also, did you copy the code from a word processing program like MS-Word, Corel Paradox, etc.? That could be the root cause of your error about the illegal character. Programs like the ones listed use special hidden characters that cause problems if you copy code from them into your compiler.
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Is not abstract and does not override abstract method! Guidance wh

Posted 16 May 2010 - 11:08 AM

What is the offending line(s)? What method(s) is/are not being implemented? What specific errors are you getting from your compiler? What is the abstract class or interface you are using?

We need more information from you before we can help. Also, please post your question in the body of your post, not the title where it gets cut off.

Also, topics merged. Please avoid duplicate posting. :)

This post has been edited by macosxnerd101: 16 May 2010 - 11:16 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2