-
Bug
-
Resolution: Fixed
-
P4
-
6-pool
-
b11
-
generic
-
generic
http://download.oracle.com/javase/tutorial/2d/advanced/examples/ShapeMover.java
Some logic in your SHAPEMOVER applet is wrong:
If you start a DRAG outside of the rectangle but release the mouse inside it, the shape IS moved.
Cause: the role of the pressOut variable is wrong. The relevant original code snippets are:
// Handles the event of the user pressing down the mouse button.
public void mousePressed(MouseEvent e){
last_x = rect.x - e.getX();
last_y = rect.y - e.getY();
// Checks whether or not the cursor is inside of the rectangle while the user is pressing the mouse.
if(rect.contains(e.getX(), e.getY())) updateLocation(e);
else {
ShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
pressOut = true;
}
}
// Handles the event of a user dragging the mouse while holding down the mouse button.
public void mouseDragged(MouseEvent e){
if(!pressOut) updateLocation(e);
else ShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
}
// Handles the event of a user releasing the mouse button.
public void mouseReleased(MouseEvent e){
// Checks whether or not the cursor is inside of the rectangle when the user releases the mouse button.
if(rect.contains(e.getX(), e.getY())) updateLocation(e);
else {
ShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
pressOut = false;
}
}
This is CORRECT:
// Handles the event of the user pressing down the mouse button.
public void mousePressed(final MouseEvent e){
last_x = rect.x - e.getX();
last_y = rect.y - e.getY();
// Checks whether or not the cursor is inside of the rectangle
// while the user is pressing the mouse.
if ( rect.contains(e.getX(), e.getY()) ) {
pressOut = false; // ADDED by HS: Indicates that PRESSING WAS INSIDE rect
updateLocation(e);
} else {
SwingShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
pressOut = true;
}
}
// Handles the event of a user dragging the mouse while holding
// down the mouse button.
public void mouseDragged(final MouseEvent e){
if ( !pressOut ) {
updateLocation(e);
} else {
SwingShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
}
}
// Handles the event of a user releasing the mouse button.
public void mouseReleased(final MouseEvent e){
// Checks whether or not the cursor is inside of the rectangle
// when the user releases the mouse button.
if ( rect.contains(e.getX(), e.getY()) ) {
if( !pressOut ){ // ADDED by HS: Only if mouse was pressed INSIDE rect
updateLocation(e); // ADDED by HS update the pos of the rect
} // ADDED by HS
} else {
SwingShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
// pressOut = false; //COMMENTED OUT by HS: Changing is unnecessary
}
}
Of course a better named Boolean like
pressedINSIDE would even make the
code clearer (more positve :)
OK thanks for the many great things you made!
Henk (The Netherlands)
Some logic in your SHAPEMOVER applet is wrong:
If you start a DRAG outside of the rectangle but release the mouse inside it, the shape IS moved.
Cause: the role of the pressOut variable is wrong. The relevant original code snippets are:
// Handles the event of the user pressing down the mouse button.
public void mousePressed(MouseEvent e){
last_x = rect.x - e.getX();
last_y = rect.y - e.getY();
// Checks whether or not the cursor is inside of the rectangle while the user is pressing the mouse.
if(rect.contains(e.getX(), e.getY())) updateLocation(e);
else {
ShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
pressOut = true;
}
}
// Handles the event of a user dragging the mouse while holding down the mouse button.
public void mouseDragged(MouseEvent e){
if(!pressOut) updateLocation(e);
else ShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
}
// Handles the event of a user releasing the mouse button.
public void mouseReleased(MouseEvent e){
// Checks whether or not the cursor is inside of the rectangle when the user releases the mouse button.
if(rect.contains(e.getX(), e.getY())) updateLocation(e);
else {
ShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
pressOut = false;
}
}
This is CORRECT:
// Handles the event of the user pressing down the mouse button.
public void mousePressed(final MouseEvent e){
last_x = rect.x - e.getX();
last_y = rect.y - e.getY();
// Checks whether or not the cursor is inside of the rectangle
// while the user is pressing the mouse.
if ( rect.contains(e.getX(), e.getY()) ) {
pressOut = false; // ADDED by HS: Indicates that PRESSING WAS INSIDE rect
updateLocation(e);
} else {
SwingShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
pressOut = true;
}
}
// Handles the event of a user dragging the mouse while holding
// down the mouse button.
public void mouseDragged(final MouseEvent e){
if ( !pressOut ) {
updateLocation(e);
} else {
SwingShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
}
}
// Handles the event of a user releasing the mouse button.
public void mouseReleased(final MouseEvent e){
// Checks whether or not the cursor is inside of the rectangle
// when the user releases the mouse button.
if ( rect.contains(e.getX(), e.getY()) ) {
if( !pressOut ){ // ADDED by HS: Only if mouse was pressed INSIDE rect
updateLocation(e); // ADDED by HS update the pos of the rect
} // ADDED by HS
} else {
SwingShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
// pressOut = false; //COMMENTED OUT by HS: Changing is unnecessary
}
}
Of course a better named Boolean like
pressedINSIDE would even make the
code clearer (more positve :)
OK thanks for the many great things you made!
Henk (The Netherlands)