Question: I have to create a Mouse Listener in Java that is going to read the location of where the mouse is clicked. But i keep

I have to create a Mouse Listener in Java that is going to read the location of where the mouse is clicked. But i keep getting errors.

That is my code:

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.AbstractAction;

import javax.swing.Action;

import javax.swing.Timer;

/**

* Do not modify this file without permission from your TA.

**/

public class Controller {

private Model model;

private View view;

Action update;

Timer t;

public Controller(){

view = new View((ActionListener) new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

model.toggleAnimation();

if (!model.Stopped) {

t.stop();

}

else {

t.restart();

}

}

}

);

abstract class Mouse implements MouseListener {

//Mouse m = new Mouse();

//view.addMouseListener(m)

(THAT'S WHERE I'M GETTING AN ERROR) view = new view ((MouseListener) new MouseListener() {

//list = new MouseListener(this);

// addMouseListener(this);

// Mouse = new Mouse((MouseListener) new MouseListener() {

@Override

public void mouseClicked(MouseEvent e) {

int x = e.getX();

int y = e.getY();

}

@Override

public void mouseEntered(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseExited(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mousePressed(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseReleased(MouseEvent arg0) {

// TODO Auto-generated method stub

}

});

}

model = new Model(view.getWidth(), view.getHeight(), view.getImageWidth(), view.getImageHeight());

update = new AbstractAction() {

public void actionPerformed(ActionEvent event) {

//increment the x and y coordinates, alter direction if necessary

model.updateLocationAndDirection();

//update the view

view.update(model.getX(), model.getY(), model.getDirect());

}

};

}

//run the simulation

public void start(){

t = new Timer(3,update);

t.start();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!