Search code examples
javaswingmouseeventthread-sleepmouse-listeners

Java mouseevent error - "method already defined in class"


I am trying to implement a click and drag method, for which I need mousePressed and mouseReleased events. I will also be using mouseClicked events, so I implemented MouseMotionListener and MouseListener.

However, when I go to write mousePressed, mouseReleased, mouseEntered, mouseExited methods I get the following error:

method mousePressed(MouseEvent) is already defined in class BoingPanel

Here is the entire class:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JPanel;

public class BoingPanel extends JPanel implements MouseMotionListener, MouseListener {

private int width;
private int height;

private int updateRate=40;

ArrayList<Ball> balls;
ArrayList<Line> lines;

private Container box;

private boolean drag = false;

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

public BoingPanel()
{

    balls = new ArrayList<Ball>();

    balls.add(new Ball(100,0));  // adds test ball
    balls.get(0).setXVelocity(5);
    balls.add(new Ball(100,0));
    balls.add(new Ball(400,50));

    lines = new ArrayList<Line>();
    lines.add(new Line(0, height, width, height));


    gameStart();
}

public BoingPanel(int x, int y)
{
    width=x;
    height=y;


    balls = new ArrayList<Ball>();

    balls.add(new Ball(100,0));  // adds test ball
    balls.get(0).setXVelocity(5);
    balls.add(new Ball(100,0));
    balls.add(new Ball(400,50));

    lines = new ArrayList<Line>();


    box = new Container(width, height);


    gameStart();
}

@Override
public void mouseDragged(MouseEvent me) {
}

@Override
public void mouseMoved(MouseEvent e) {
}

@Override
public void mouseClicked(MouseEvent e) {

}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}

 public void gameStart() {

  // Run the game logic in its own thread.
  Thread gameThread = new Thread() {

     public void run() {
        while (true) {
           // Execute one time-step for the game 
           update();
           // Refresh the display
           repaint();
           // Delay and give other thread a chance
           try {

              Thread.sleep(1000 /updateRate);

           } catch (InterruptedException ex) {}
        }
     }
  };
  gameThread.start();  // Invoke GaemThread.run()
}

private void update()
{
    for(Ball a:balls)  //calls balls updated position
    {
        a.update(box);


    }

}

public void paintComponent(Graphics g)
{
    box.paint(g);

    for(Ball a:balls)  //calls balls updated position
    {
        a.paint(g);
    }

    for(Line b:lines)  //draws lines
    {
        b.paint(g);
    }

}

public void actionPerformed(ActionEvent e)
{
    update(); //changes ball position
    repaint(); //refreshes image
}

}

What is causing this error? Thanks!


Solution

  • I would suggest this, just below your variable declarations and before you constructor, isn't helping...

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

    You've basically implemented these methods twice. Just before and after the constructor.

    Simply remove on of these groups of declarations...