Home Segments Top Top Previous Next

780: Mainline

Specifically, for the sake of illustration, suppose that you want the movie model to have its values set such that the meter pointer moves to the place on the meter where you click your mouse.

First, you must determine the value that the meter would be displaying if the dial were under the mouse at the point of click. Then, because a movie's rating is the sum of the values of the script, acting, and direction instance variables, you divide the value returned from the meter view by 3, and use that result as the argument of the movie model's setters.

The getX and getY methods, called with the mouse event as the target, supply the coordinates of the mouse at the point of click, relative to the meter component on which the mouse is clicked. The mouseClicked method uses round, a class method of the Math class, described in Segment 734.

public void mouseClicked (MouseEvent e) { 
 int x = e.getX(); 
 int y = e.getY(); 
 int v = (int) Math.round(getMeter().getValueAtCoordinates (x, y)  
               / 3.0); 
 getMovie().setScript(v); 
 getMovie().setActing(v); 
 getMovie().setDirection(v); 
}