Hello Can someone please help me with this assignment You ha

Hello! Can someone please help me with this assignment?

You have a GUI that keeps track of a list of pre-specified \"special\" points. If the user\'s mouse moves over one of these points, the point lights up. Write the code that would go inside of the handler/listener below. Use the references and also assume the existence of the method listed below (which you can invoke- you do not need to write).

specialPointList // an ArrayList of the pre-specified \"special\" points; the type is ArrayList<Point>

assume this method exists: public void lightUpAPoint(Point p)

Your code will go inside the handler method:

public void mouseMoved(MouseEvent event) {

}

Solution

As it is not mentioned in question how the Point and MouseEvent classes are defined I am assuming both of them has two variable attributes (x,y) that stores the location of point and mouse respectively.
For this we can use distance formula to check proximity with each point. Either a distance threshold value is used or we can do pinpoint accurate lightUps. If a threshold value is used we can even lightup if the user is close to a certain point instead of just always over it exactly. I am using a threashold value for a better usecase. You can just set it to 0 if you want. Then using distance formula we can find mouse-point proximity.

You can read more about distance formula here: http://www.mathwarehouse.com/algebra/distance_formula/index.php

----------------------------code------------------------------

public void mouseMoved(MouseEvent event){

float threshold = 0.5f; //set it 0 for pinpoint accuracy

for(Point p:specialPointList){

float xx = (p.x - event.x) * (p.x - event.x);

float yy = (p.y - event.y) * (p.y - event.y);

float distance = Math.sqrt(xx+yy);

if(distance<threshold){

lightUpAPoint(p);

}

}

}

Hello! Can someone please help me with this assignment? You have a GUI that keeps track of a list of pre-specified \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site