Skip to main content

about mouseEvent in javaFX_NB_daily version 2008-07-08_02-32-47

2 replies [Last post]
expsan
Offline
Joined: 2008-06-12

to drag rectangle not work rightly. please tell me what's wrong!!

import javafx.ext.swing.Frame;
import javafx.ext.swing.Canvas;
import javafx.scene.geometry.Rectangle;
import javafx.scene.paint.Color;
import javafx.input.MouseEvent;

Frame {
title: "MyApplication"
width: 200
height: 200

visible: true

content: Canvas {
content: [
Rectangle {
var mx=0.0
var my=0.0
x: bind mx, y: bind my
width: 50, height: 50
fill: Color.BLACK
onMouseDragged: function( e: MouseEvent ):Void {
mx+=e.getDragX();
my+=e.getDragY();
}
}
]

}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
indy
Offline
Joined: 2003-06-10

> to drag rectangle not work rightly. please tell me
> what's wrong!!

> onMouseDragged: function( e: MouseEvent
> ):Void {
> mx+=e.getDragX();
> my+=e.getDragY();
> }
> ]

e.getDragX() does not return the relative position to the last onMouseDragged Event instead it returns the relative position to the MousePressed Event. So it's necessary to store that positiion to calculater the relative position.

var rect: Rectangle = Rectangle {
var dragStartX: Number;
var dragStartY: Number;

width : 100
height: 100
fill : Color.BLUE
cursor: Cursor.MOVE

onMousePressed: function (e: MouseEvent) {
dragStartX = rect.x;
dragStartY = rect.y;
}// onMousePressed

onMouseDragged: function (e: MouseEvent) {
rect.x = dragStartX + e.getDragX();
rect.y = dragStartY + e.getDragY();
}// onMouseDragged
}// Rectangle

tschuess
[|8:)

expsan
Offline
Joined: 2008-06-12

thank's for your reply!! it works very well!