nhay59
asked on
class not abstract and does not override abstract method problem
HI,
Back again. I'm now trying to extend my graphics program to allow a user to drag the PacMan drawing around the window to a new location. I have added the required code, but get the following error when I try to compile it,
PacMan_clickstest is not abstract and does not override abstract method mouseExited(java.awt.event .MouseEven t) in java.awt.event.MouseListen er
Why would it throw up this error, and what is the best way to correct this problem?
My current code is as follows,
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class PacMan_clickstest extends JPanel implements MouseListener, MouseMotionListener{
public static int mode = 0;
private static JButton rightButton;
private static JButton leftButton;
int last_x, last_y;
Rectangle rect;
boolean pressOut = false;
public PacMan_clickstest(){
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
last_x = e.getX();
last_y = e.getY();
pressOut = rect.contains(e.getX(), e.getY());
}
public void mouseDragged(MouseEvent e){
if ( !pressOut)
updateLocation(e);
}
public void mouseReleased(MouseEvent e) {}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
//public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void updateLocation(MouseEvent e) {
rect.translate(e.getX() - last_x, e.getY() - last_y);
last_x = e.getX();
last_y = e.getY();
repaint();
}
});
}
public void paintComponent(Graphics g) {
Dimension d = getSize();
Graphics2D g2 = (Graphics2D)g;
int size = 100;
Ellipse2D.Double head =
new Ellipse2D.Double(0, 0, size, size);
Ellipse2D.Double eye =
new Ellipse2D.Double(size/2-1, size/5-1,
size/10, size/10);
GeneralPath mouth = new GeneralPath();
mouth.moveTo(size, size/4);
mouth.lineTo(size/8, size/2);
mouth.lineTo(size, size*3/4);
mouth.closePath();
Area pacman = new Area(head);
pacman.subtract(new Area(eye));
pacman.subtract(new Area(mouth));
g2.setPaint(Color.yellow);
g2.fill(pacman);
g2.setPaint(Color.black);
g2.draw(pacman);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Drawing stuff");
Container contentPane = frame.getContentPane();
JPanel panel = new JPanel();
PacMan_clickstest pacman = new PacMan_clickstest();
leftButton = new JButton("Leftt");
//leftButton.addActionList ener(pacma n);
panel.add(leftButton);
rightButton = new JButton("Right");
//rightButton.addActionLis tener(pacm an);
panel.add(rightButton);
contentPane.add(panel, BorderLayout.SOUTH);
contentPane.add(pacman, BorderLayout.CENTER);
frame.setSize(600, 600);
frame.setVisible(true);
frame.addWindowListener(ne w WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
Any advice or help appreciated. Many thanks
Back again. I'm now trying to extend my graphics program to allow a user to drag the PacMan drawing around the window to a new location. I have added the required code, but get the following error when I try to compile it,
PacMan_clickstest is not abstract and does not override abstract method mouseExited(java.awt.event
Why would it throw up this error, and what is the best way to correct this problem?
My current code is as follows,
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class PacMan_clickstest extends JPanel implements MouseListener, MouseMotionListener{
public static int mode = 0;
private static JButton rightButton;
private static JButton leftButton;
int last_x, last_y;
Rectangle rect;
boolean pressOut = false;
public PacMan_clickstest(){
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
last_x = e.getX();
last_y = e.getY();
pressOut = rect.contains(e.getX(), e.getY());
}
public void mouseDragged(MouseEvent e){
if ( !pressOut)
updateLocation(e);
}
public void mouseReleased(MouseEvent e) {}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
//public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void updateLocation(MouseEvent e) {
rect.translate(e.getX() - last_x, e.getY() - last_y);
last_x = e.getX();
last_y = e.getY();
repaint();
}
});
}
public void paintComponent(Graphics g) {
Dimension d = getSize();
Graphics2D g2 = (Graphics2D)g;
int size = 100;
Ellipse2D.Double head =
new Ellipse2D.Double(0, 0, size, size);
Ellipse2D.Double eye =
new Ellipse2D.Double(size/2-1,
size/10, size/10);
GeneralPath mouth = new GeneralPath();
mouth.moveTo(size, size/4);
mouth.lineTo(size/8, size/2);
mouth.lineTo(size, size*3/4);
mouth.closePath();
Area pacman = new Area(head);
pacman.subtract(new Area(eye));
pacman.subtract(new Area(mouth));
g2.setPaint(Color.yellow);
g2.fill(pacman);
g2.setPaint(Color.black);
g2.draw(pacman);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Drawing stuff");
Container contentPane = frame.getContentPane();
JPanel panel = new JPanel();
PacMan_clickstest pacman = new PacMan_clickstest();
leftButton = new JButton("Leftt");
//leftButton.addActionList
panel.add(leftButton);
rightButton = new JButton("Right");
//rightButton.addActionLis
panel.add(rightButton);
contentPane.add(panel, BorderLayout.SOUTH);
contentPane.add(pacman, BorderLayout.CENTER);
frame.setSize(600, 600);
frame.setVisible(true);
frame.addWindowListener(ne
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
Any advice or help appreciated. Many thanks
ASKER
Hi,
Thanks for the reply. Yes, my mistake. It should have been uncommented, I just forgot to remove the comment marks whilst pasting the code accross. However, it still does not make a difference to the problem with this current code.
Any ideas people?
Thanks
Thanks for the reply. Yes, my mistake. It should have been uncommented, I just forgot to remove the comment marks whilst pasting the code accross. However, it still does not make a difference to the problem with this current code.
Any ideas people?
Thanks
same error or a different one?
you also need to implement the methods for MouseMotionListener
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
I've sorted out that problem by changing the code around slightly. I was being a bit daft about the whole thing. I do, however, have another quick question.
How do I change the pointer for the object to be redrawn from 'rect' to the PacMan object defined within the paintComponent() method. I trying to get the PacMan object to be redrawn when a user drags it around the window. This refers specifically to the mousePressed() and updateLocation() methods.
My current code is as follows,
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;
public class PacMan_clickstest extends JPanel implements MouseListener, MouseMotionListener{
public static int mode = 0;
private static JButton rightButton;
private static JButton leftButton;
int last_x, last_y;
Rectangle rect;
boolean pressOut = false;
public PacMan_clickstest(){
addMouseListener(this);
addMouseMotionListener(thi s);
}
public void mousePressed(MouseEvent e) {
last_x = e.getX();
last_y = e.getY();
pressOut = rect.contains(e.getX(), e.getY());
}
public void mouseDragged(MouseEvent e){
if ( !pressOut)
updateLocation(e);
//repaint();
}
public void mouseReleased(MouseEvent e) {}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void updateLocation(MouseEvent e) {
rect.translate(e.getX() - last_x, e.getY() - last_y);
last_x = e.getX();
last_y = e.getY();
repaint();
}
public void paintComponent(Graphics g) {
Dimension d = getSize();
Graphics2D g2 = (Graphics2D)g;
int size = 100;
Ellipse2D.Double head =
new Ellipse2D.Double(0, 0, size, size);
Ellipse2D.Double eye =
new Ellipse2D.Double(size/2-1, size/5-1,
size/10, size/10);
GeneralPath mouth = new GeneralPath();
mouth.moveTo(size, size/4);
mouth.lineTo(size/8, size/2);
mouth.lineTo(size, size*3/4);
mouth.closePath();
Area pacman = new Area(head);
pacman.subtract(new Area(eye));
pacman.subtract(new Area(mouth));
g2.setPaint(Color.yellow);
g2.fill(pacman);
g2.setPaint(Color.black);
g2.draw(pacman);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Drawing stuff");
Container contentPane = frame.getContentPane();
JPanel panel = new JPanel();
PacMan_clickstest pacman = new PacMan_clickstest();
leftButton = new JButton("Leftt");
//leftButton.addActionList ener(pacma n);
panel.add(leftButton);
rightButton = new JButton("Right");
//rightButton.addActionLis tener(pacm an);
panel.add(rightButton);
contentPane.add(panel, BorderLayout.SOUTH);
contentPane.add(pacman, BorderLayout.CENTER);
frame.setSize(600, 600);
frame.setVisible(true);
frame.addWindowListener(ne w WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
Any help or advice really appreciated. Many thanks.
I've sorted out that problem by changing the code around slightly. I was being a bit daft about the whole thing. I do, however, have another quick question.
How do I change the pointer for the object to be redrawn from 'rect' to the PacMan object defined within the paintComponent() method. I trying to get the PacMan object to be redrawn when a user drags it around the window. This refers specifically to the mousePressed() and updateLocation() methods.
My current code is as follows,
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;
public class PacMan_clickstest extends JPanel implements MouseListener, MouseMotionListener{
public static int mode = 0;
private static JButton rightButton;
private static JButton leftButton;
int last_x, last_y;
Rectangle rect;
boolean pressOut = false;
public PacMan_clickstest(){
addMouseListener(this);
addMouseMotionListener(thi
}
public void mousePressed(MouseEvent e) {
last_x = e.getX();
last_y = e.getY();
pressOut = rect.contains(e.getX(), e.getY());
}
public void mouseDragged(MouseEvent e){
if ( !pressOut)
updateLocation(e);
//repaint();
}
public void mouseReleased(MouseEvent e) {}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void updateLocation(MouseEvent e) {
rect.translate(e.getX() - last_x, e.getY() - last_y);
last_x = e.getX();
last_y = e.getY();
repaint();
}
public void paintComponent(Graphics g) {
Dimension d = getSize();
Graphics2D g2 = (Graphics2D)g;
int size = 100;
Ellipse2D.Double head =
new Ellipse2D.Double(0, 0, size, size);
Ellipse2D.Double eye =
new Ellipse2D.Double(size/2-1,
size/10, size/10);
GeneralPath mouth = new GeneralPath();
mouth.moveTo(size, size/4);
mouth.lineTo(size/8, size/2);
mouth.lineTo(size, size*3/4);
mouth.closePath();
Area pacman = new Area(head);
pacman.subtract(new Area(eye));
pacman.subtract(new Area(mouth));
g2.setPaint(Color.yellow);
g2.fill(pacman);
g2.setPaint(Color.black);
g2.draw(pacman);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Drawing stuff");
Container contentPane = frame.getContentPane();
JPanel panel = new JPanel();
PacMan_clickstest pacman = new PacMan_clickstest();
leftButton = new JButton("Leftt");
//leftButton.addActionList
panel.add(leftButton);
rightButton = new JButton("Right");
//rightButton.addActionLis
panel.add(rightButton);
contentPane.add(panel, BorderLayout.SOUTH);
contentPane.add(pacman, BorderLayout.CENTER);
frame.setSize(600, 600);
frame.setVisible(true);
frame.addWindowListener(ne
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
Any help or advice really appreciated. Many thanks.
you can use the 'rect' variable in your paintComponewnt() method
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
uncomment that
you need to implement all interface methods