📄 scatterplot.java
字号:
// Draw tick mark
graphics_.drawLine(OFFSET + (axisTemp),
OFFSET,
OFFSET + (axisTemp),
OFFSET - 5 );
// Draw value above tick mark
graphics_.drawString(String.valueOf( (maxX_/NUM_MARKERS)*i ),
(OFFSET + (axisTemp)),
OFFSET - 20);
}
// Draw tick marks on y axis
for (int i=1; i<=NUM_MARKERS; i++)
{
axisTemp = i*axisMarkerDist;
// Draw tick mark
graphics_.drawLine( OFFSET - 5,
(OFFSET + (axisTemp)),
OFFSET,
(OFFSET + (axisTemp)));
// Draw value above tick mark
graphics_.drawString(String.valueOf((maxY_/NUM_MARKERS)*i),
xOrigin_ + X_LABEL_OFFSET,
(OFFSET + (axisTemp)));
}
}
/**
* Provides the ability to return the SPlotPoint that requires highlighting
*
* @return SPlotPoint - the point to be highlighted, or null if none
*/
private SPlotPoint getCurrentHighlight()
{
SPlotPoint plotPoint = null;
SPlotPoint point;
// Go through Vector of points to find the point that requires
// highlighting
for (int i = 0; i<pointVector_.size(); i++)
{
point = (SPlotPoint)pointVector_.elementAt(i);
if (point.getIsHilited() == true)
{
plotPoint = point;
//break out of the loop once the highlighted point is found
break;
}
else
{
plotPoint = null;
}
}
return plotPoint;
}
/**
* Finds the closest scatter plot point to the mouse, every time the mouse
* moves
*
* @param mousePoint The point representing the mouse position
* @return SPlotPoint The closest scatter plot point to the mouse, which
* should be highlighted
*/
private SPlotPoint findClosestPoint( Point mousePoint )
{
SPlotPoint plotPoint;
// The distance between cursor position and given plot point
double distance;
// A large amount - used to isolate the closest plot point to the
// mouse,in the case of the cursor being within the radius of two
// plot points
double minDistance = 300;
SPlotPoint closestPoint = null;
// Holder in loop for points
SPlotPoint currPoint;
for (int i = 0; i<pointVector_.size(); i++)
{
currPoint = (SPlotPoint)pointVector_.elementAt(i);
distance = calcDistance(mousePoint, currPoint);
// If the cursor is on a plot point,
if (distance < OVAL_DIAMETER)
{
// Isolate the closest plot point to the cursor
if (distance < minDistance)
{
closestPoint = currPoint;
// Reset the distance
minDistance = distance;
}
}
}
return closestPoint;
}
/**
* Listener for mouse movement. Detects location of mouse. If it's over
* a point, that point will be highlighted
*
* @param mouseEvent - the mouseEvent generated everytime the mouse moves
*/
public void mouseMoved(MouseEvent mouseEvent)
{
// Holder in loop for points
SPlotPoint currPoint;
SPlotPoint closestPoint = findClosestPoint(mouseEvent.getPoint());
SPlotPoint currHilitePoint = getCurrentHighlight();
currHilitePoint_ = currHilitePoint;
// Mouse is not on a point, but there is a currently highlighted point
// Repaint, setting color of all points to non-highlighted
if ((closestPoint == null) && (currHilitePoint != null))
{
for (int i = 0; i<pointVector_.size(); i++)
{
currPoint = (SPlotPoint)pointVector_.elementAt(i);
currPoint.setIsHilited(false);
}
repaint( ((int)currHilitePoint.x - (OVAL_RADIUS)),
((int)currHilitePoint.y - (OVAL_RADIUS)),
OVAL_REPAINT_SIZE,
OVAL_REPAINT_SIZE );
// remove the highlighting from the map
highlightManager_.setHighlight(0);
}
// If there is a closest point that should be highlighted, paint it as
// highlited
else if ( (closestPoint != null) && (currHilitePoint == null))
{
closestPoint.setIsHilited(true);
repaint( ((int)(closestPoint.x) - (OVAL_RADIUS)),
((int)(closestPoint.y) - (OVAL_RADIUS)),
OVAL_REPAINT_SIZE,
OVAL_REPAINT_SIZE );
// highlight the corresponding state on the map
//problem...must unhighlight state when mouse moves off closestPoint
highlightManager_.setHighlight(closestPoint.getId());
}
// if previous highlighted point exists, then color that
// back to blue
if ((prevHilitePoint_ != null)
&&
(prevHilitePoint_ != currHilitePoint_))
{
repaint( (int)(prevHilitePoint_.x - (OVAL_RADIUS)),
(int)(prevHilitePoint_.y - (OVAL_RADIUS)),
OVAL_REPAINT_SIZE,
OVAL_REPAINT_SIZE );
}
// Store the currently highlighted point as the previous
// highlighted point
prevHilitePoint_ = currHilitePoint_;
}
/**
* This method needed to fulfill the mousemotionlistener interface
*/
public void mouseDragged(MouseEvent mouseEvent)
{
// Do nothing for this
}
/**
* Provides the ability to calculate the distance between two points:
* the current mouse position and a given point
*
* @param a one of the points that defines one end of a line
* whose 'distance' will be calculated
* @param b the point that defines the other end of a line
* whose 'distance' will be calculated
* @return double The distance between the two points
*/
public final double calcDistance( Point a, Point b )
{
return( Math.sqrt( (Math.pow(a.y - b.y, 2)) +
(Math.pow(a.x - b.x, 2)) ) );
}
/**
* Returns the xy coordinates of point currently highlighted
*
* @return Point - the currently highlighted point
*/
public Point getCurrHilitePoint()
{
SPlotPoint tempPoint = null;
SPlotPoint highPoint = null;
// Go through vector of points and return the point (if any) that
// is currently highlighted
for (int i = 0; i<pointVector_.size(); i++)
{
tempPoint = (SPlotPoint)pointVector_.elementAt(i);
if (tempPoint.getIsHilited() == true)
{
highPoint = tempPoint;
}
}
return highPoint;
}
/**
* Provides the ability to listen for changes in the highlighting of the
* map and highlight the corresponding point on the scatter plot
*
* @param highlightChangedEvent - the object that represents what kind of
* change took place (?)
*/
public void highlightChanged(HighlightChangedEvent highlightChangedEvent)
{
// get the currently highlighted state id,
// get the x and y of the point with this id
// repaint the corresponding portion of the component
int hiliteID = highlightManager_.getHighlight();
SPlotPoint thePoint;
// The x and y coordinates of the point to be highlighted
int x,y;
if (hiliteID > 0)
{
// The -1 is there to compensate for vectors starting at index 0
thePoint = (SPlotPoint)pointVector_.elementAt(hiliteID -1);
thePoint.setIsHilited(true);
currHilitePoint_ = thePoint;
// Get the x and y coordinates of the point
x = (int)thePoint.x;
y = (int)thePoint.y;
// Only repaint the point if it has valid data (in this case >0)
if ((x > INVALID_VALUE) || (y > INVALID_VALUE))
{
repaint( ((int)(x) - (OVAL_RADIUS)),
((int)(y) - (OVAL_RADIUS)),
OVAL_REPAINT_SIZE,
OVAL_REPAINT_SIZE );
if (prevHilitePoint_ != null)
{
prevHilitePoint_.setIsHilited(false);
repaint( ((int)prevHilitePoint_.x - (OVAL_RADIUS)),
((int)prevHilitePoint_.y - (OVAL_RADIUS)),
OVAL_REPAINT_SIZE,
OVAL_REPAINT_SIZE );
}
}
prevHilitePoint_ = currHilitePoint_;
}
// If cursor is over a state (and the corresponding plot point is
// highlighted), and then the cursor leaves the map area and enters the
// white area around it (with value -1 in this case),
// we want the highlighted scatter plot point to return to its normal color.
if (hiliteID < INVALID_VALUE)
{
// Unhighlight any highlighted scatter plot point when the cursor
// leaves the map and enters the surrounding white area around it
if (currHilitePoint_ != null)
{
currHilitePoint_.setIsHilited(false);
repaint( ((int)currHilitePoint_.x - (OVAL_RADIUS)),
((int)currHilitePoint_.y - (OVAL_RADIUS)),
OVAL_REPAINT_SIZE,
OVAL_REPAINT_SIZE );
}
}
}
} // EOC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -