Java Reference
In-Depth Information
40.
public void
setCoordinates(
int
x,
int
y){
41.
XCoord.setText(""+x);
42.
YCoord.setText(""+y);
43.
}
44.
45.
public void
setInOut(String str){
46.
inOut.setText(str);
47.
}
48.
49.
public void
incrementClickCount(){
50.
clickCount++;
51.
counts.setText(Integer.toString(clickCount));
52.
}
53.
54.
public void
setNoOfCircles(
int
n){
noOfCirc.setText(Integer.toString(n));
55.
}
56.
}
57.
7.4
■
The control part
Of the two listeners only
MyMouseListener
is changed by adding more commands
to method
mouseClicked
. Depending on whether the left or right button was
pressed a circle is added using the methods
addCircle(e.getX(),e.getY())
or
deleted using
removeNearestCircle(e.getX(),e.getY())
of class
Interactive
Panel
. The information about which mouse button was clicked is extracted from
the
MouseEvent
object
e
using methods from the class
SwingUtilities
.Ifamouse
has three or more buttons, any button that is not the left one is considered as a
right button. In any case the number of circles is updated using
[StatusPanel].setNoOfCircles([InteractivePanel].getNoOfCircles())
The texts in brackets have to be replaced by the appropriate variable names with-
out brackets. In order for the listener to have access to that panel the constructor
of
MyMouseListener
now receives a reference to the interactive panel.
File:
its/InteractiveGraphic/MyMouseListener.java
package
its.InteractiveGraphic;
1.
2.
3.
import
java.awt.event.MouseEvent;
import
java.awt.event.MouseListener;
4.
import
javax.swing.SwingUtilities;
5.
6.
Search WWH ::
Custom Search