I have a program that tracks the mouse with MouseEvent.MOVED in javafx and whenever i press and hold the mouse button the tracking stops.
I have tried to switch events from addEventFilter to addEventHandler. Adding another Event, MouseEvent.DRAGED. But it wont even register an event until i disable the code from MouseEvent.Moved. I have tried to combine these but nothing seems to work. Help is very much appreciated.
EventHandler<MouseEvent> tracking = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e){
double x = e.getSceneX();
double y = e.getSceneY();
if((x + size < 400) && (y - circle.getRadius() > 1)){
switch (value){
case 0 :
circle.setCenterX(x);
circle.setCenterY(y);
break;
case 1 :
rec.setLayoutX(x);
rec.setLayoutY(y);
break;
case 2 :
pol.getPoints().clear();
pol.getPoints().addAll(new Double[]{x - size, y, x + size, y, x, y + size});
break;
}
}
}
};
EventHandler<MouseEvent> test = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e){
System.out.print("test: ");
}
};
pane1.addEventHandler(MouseEvent.MOUSE_MOVED, tracking);
pane1.addEventFilter(MouseEvent.MOUSE_DRAGGED, test);
Okey i found why it diden´t work. The MouseEvent was actually being activated on the object that was tracking the mouse. So when i clicked the mouse it created a drag event on that object that woulden´t end until i released the mouse button, thanks for the help :)
edit: I still dont get why it dident work from the beginning. Shouldent the eventHandler method capture the event when it bubbles up?
Related
I'm watching The Coding Train's Processing Tutorials. In 5.4, he makes this line of code that is supposed to have an ellipse then move when clicked, but stop when clicked again. But for me it just says Debugger Halted. Here is my code:
float x = 100;
boolean going = false;
void setup() {
size(400, 300);
}
void draw() {
background(0);
fill(255);
ellipse(x, 150, 24, 24);
if (going) {
x = x + 2;
}
}
void mousePressed() {
going = true;
}
My guess is you're running in debug mode with a break point set.
You probably want to disable debug mode by clicking the butterfly button in the upper-right corner, or you want to find the break point and remove it by clicking the line number with a diamond icon.
The title explains it.
So I have an application that fades to 1/2 opacity when it is not moused over and full when it is. This application is StageStyle.TRANSPARENT and moves around when the user drags their mouse.
I am wondering if there is a way to make it so that the mouse has to drag a certain (10) amount of pixels before the drag event is triggered?
Here is the code
moveWindowTemp dragDelta = new moveWindowTemp();
primaryScene.addEventFilter(MouseEvent.MOUSE_PRESSED, mouseEvent -> {
dragDelta.x = primaryStage.getX() - mouseEvent.getScreenX();
dragDelta.y = primaryStage.getY() - mouseEvent.getScreenY();
});
primaryScene.addEventFilter(MouseEvent.MOUSE_DRAGGED, mouseEvent -> {
primaryStage.setX(mouseEvent.getScreenX() + dragDelta.x);
primaryStage.setY(mouseEvent.getScreenY() + dragDelta.y);
isBeingMoved = true;
});
primaryScene.addEventFilter(MouseEvent.MOUSE_RELEASED, mouseEvent -> {
primaryStage.setX(mouseEvent.getScreenX() + dragDelta.x);
primaryStage.setY(mouseEvent.getScreenY() + dragDelta.y);
isBeingMoved = false;
});
...
public class moveWindowTemp {
double x, y;
}
If I understand your question correctly you could simply set the event as being consumed in your event filter. https://docs.oracle.com/javase/8/javafx/events-tutorial/filters.htm#BCFDCCEJ
I have a program that accepts a user's mouse click and uses the coordinates of the mouse to draw a series of hexagons. I have drawn on my JPanel a red rectangle that is supposed to represent the bounds the user is allowed to click in, so to make sure the user does, I've included a warning message that pops up whenever the user clicks out of bounds. It says, "Cannot click outside red rectangle. Try again."
After the message pops up, I want the user to be able to dismiss the message and try again, but whenever the user clicks "OK" or "X" in the upper-right corner, the message appears again, and I can't seem to close the pop-up box. Is there another way I can inform the user while letting them try again?
Below is my code for a MouseListener I've implemented for my JPanel, hexPanel.
hexPanel.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
boolean clicked = false;
int left = HexDrawingPanel.iX(-rWidth/2), right = HexDrawingPanel.iX(rWidth/2);
int top = HexDrawingPanel.iY(rHeight/2), bot = HexDrawingPanel.iY(-rHeight/2);
while(!clicked) {
if(!(e.getX() > right || e.getX() < left ||
e.getY() < top || e.getY() > bot))
clicked = true;
else
JOptionPane.showMessageDialog(frame, "Must click inside red rectangle. Try again.");
}
HexDrawingPanel.radius = (float)Math.sqrt(Math.pow(e.getX() - left, 2) + Math.pow(e.getY() - top, 2));
hexPanel.repaint();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
Your while-loop is blocking the Event Dispatching Thread, meaning that's impossible for any new mouse clicks from ever occurring...
It might be better to do something more like...
if(!(e.getX() > right || e.getX() < left ||
e.getY() < top || e.getY() > bot)) {
HexDrawingPanel.radius = (float)Math.sqrt(Math.pow(e.getX() - left, 2) + Math.pow(e.getY() - top, 2));
hexPanel.repaint();
} else
JOptionPane.showMessageDialog(frame, "Must click inside red rectangle. Try again.");
}
There are a couple of ways to do this:
You could make the listener stateful, setting a global flag before the option pane is displayed and ignoring mouse clicks until the flag is unset.
You could put a JLabel above the box and show (colored?) status messages there instead of using a dialog.
I am coding a Java game that uses mouse actions. I have a point msc in my main class - it gets changed whenever I press the mouse and gets set to (0, 0) whenever I release the mouse. For the buttons, they check if msc is inside their rectangle, and if so, call click.
One of the buttons is supposed to toggle a boolean; when I click on it though, it switches true and false very fast because msc gets updated every time paintComponent is called.
Here is the code for the button click method:
if (button.contains(Screen.msc)) {
beenClicked = true;
this.width = this.width - 2;
this.height = this.height - 2;
this.x = this.x + 1;
this.y = this.y + 1;
g.setColor(currColor);
textColor = Color.YELLOW;
}
but that is not where the problem is I think. Here is the code that changes msc:
public void mouseReleased(MouseEvent e) {
Screen.msc = new Point(0, 0);
}
public void mousePressed(MouseEvent e) {
Screen.msc = new Point((e.getX()) - ((Frame.size.width - Screen.myWidth) / 2), e.getY() - ((Frame.size.height - (Screen.myHeight)) - (Frame.size.width - Screen.myWidth) / 2));
}
and the code for what happens when the specific toggle button is clicked:
if (toggleToolTips.clicked()) {
if (Screen.canDrawTooltip) {
Screen.canDrawTooltip = false;
} else if(!Screen.canDrawTooltip){
Screen.canDrawTooltip = true;
}
}
The problem is every time I go and click the button, the boolean switches back and forth real fast. When I hold it, it just rapidly and continually switches. I would like to make it so that I click once and it switches once.
Your setting the boolean beenClicked but are not checking it anywhere. I would suggest trying
if (!beenClicked && toggleToolTips.clicked())
{
Screen.canDrawTooltip = !Screen.canDrawTooltip);
}
And setting beenClicked back to false somewhere such as mouseReleased.
I have a map applet, and I have a JLabel following the mouse, whenever the mouse goes over a city the JLable displays the the name of the city and population.
I use the mouseMotionListener's MouseMoved method for that, but i want the label to be there only if the mouse stays still for a couple of seconds above the city.
I dont know if its because I been working on this code for days now, but i cant seem to think of a solution for this using the MouseMoved method, i tried using timers but that didnt work for me (mayb i just did it wrong cause my brain is burned out)
so is there a mouse listener for the mouse staying still? or do you have any recommendations?
here is more or less what I got
public void mouseMoved(MouseEvent evt) {
int x = evt.getX();
int y = evt.getY();
boolean aboveCity = false;
mouseover.setBounds(x+20, y-10, 200, 20); //mouseover is a JLabel
for (int i=0;i<cityCounter;i++){
if (city[i].containsPoint(x,y){
name = city[i].getName();
population = city[i].getPopulation();
aboveCity = true;
}
}
if(aboveCity){
mouseover.setText(name + ", " + population);
}
else{
mouseover.setText("");
}
}
Use a Java javax.swing.Timer. Each time the mouse moves, reset the timer. When the timer goes off, the mouse has been "still" for as long as your timer was set for.