Animation - irregular working of MouseListener - java

In my animation one of elements is moving oval. There are two ways to control it. The first is setting x,y position by mouse listener and the second is Key Listener. During first using animation both methods work. But after using stop (clear) button mouse listener doesn't work, but Key method is still working.
Code stop method:
void clear() {
waves.clear();
xz_list.clear();
yz_list.clear();
time_list.clear();
f_list.clear();
time=0;
timer.stop();
repaint();
}
MouseListener method:
void SourcePosition(double v, String d) {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
{
x_o = e.getX();
y_o = e.getY();
repaint();
}
}
});
}
Generally this is application showing Doppler Effect. Start button make setting data from interface, disable interface and start animation and chart. Stop button uses clear() method and enable interface.

Related

How can I difference if a component has been dragged or clicked?

I have a JPanel with many JButton components inside. Is there a way to get the panel mouse event when a button is pressed? Another point of view: How to make button transparent to panel mouse event? I specifically need to capture mousePressed() event of panel.
EDIT
Context:
I'm dragging the panel content through a JScrollPane (actually working), to accomplish that I needed to capture the point where mouse is pressed, so both panel and buttons have MouseListener and MouseMotionListener to capture the point and do other stuff.
Issue:
When I press -> drag -> release the mouse button, if the mouse is still over the button it executes whatever the button does. So I want the mouse listener of the panel to be 'independent' of the button, to remove the mouse listener from the buttons.
EDIT 2
I just realize reading my own issue... that it will make no difference removing MouseListener to the JButton. When pressing the button if the mouse it stil over it, the actionPerformed will be executed anyway...What can I do?
EDIT 3
Edited question title, according to the solution.
Kipping in mind that the event execution order in this case is: mousePressed->mouseDragged->actionPerformed->mouseReleased , I get it working at the moment, adding a boolean:
#Override
public void mousePressed(MouseEvent e) {
origin = new Point(e.getPoint());
}
//each time the user stops dragging set dragged to false
#Override
public void mouseReleased(MouseEvent arg0) {
dragged = false;
}
#Override
public void mouseDragged(MouseEvent e) {
dragged=true;
if(((Component) e.getSource()).getParent().equals(myPanel)
|| e.getSource().equals(myPanel)){
if (origin != null) {
JViewport viewPort = (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, myPanel);
if (viewPort != null) {
int deltaX = origin.x - e.getX();
int deltaY = origin.y - e.getY();
Rectangle view = viewPort.getViewRect();
view.x += deltaX;
view.y += deltaY;
myPanel.scrollRectToVisible(view);
}
}
}
#Override
public void actionPerformed(ActionEvent e){
//stuff do detect the button...
//..in case there is more than one panel, if the component belong to myPanel and dragg is false
if(((Component) e.getSource()).getParent().equals(myPanel)&& dragged==false ){
//do stuff
}
}

Java, If a draggable object touches other object

I have a code for dragging a label width mouse.
lbl_banner.addMouseListener(new MouseAdapter()
{
#Override
public void mousePressed(MouseEvent e) {
//catching the current values for x,y coordinates on screen
x_pressed = e.getX();
y_pressed = e.getY();
}
});
lbl_banner.addMouseMotionListener(new MouseMotionAdapter(){
#Override
public void mouseDragged(MouseEvent e){
//and when the Jlabel is dragged
setLocation(e.getXOnScreen() - x_pressed, e.getYOnScreen() - y_pressed);
}
});
Now, how do I make a function: While I'm dragging this label around the Screen, If the Label by dragging touches other object (label, button,...) to do something.
if(//labelTouchesSomething){//do something}
While this is not technically a dragging but the dynamic move of a component (dragging is the transfer of contents in between components), you can compute the intersection of the current moving component against other components (this may need some navigation inside your hierarchy). May be this can help you: How do I detect the collison of components?. You can also use the methods contains of Component to determine if some coordinates are inside a component or not.

Capturing mouse movement while moused pressed in Java

I'm trying to get the mouse position while pressing the mouse button but it doesn't work.
I'm extending the MouseAdapter and as stated at the Javadoc the mouseMove() is invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.
This is an example class I have created to show you my problem:
public class TestMouse extends MouseAdapter{
int x,y;
boolean pressed;
public void mousePressed(MouseEvent e){
pressed = true;
}
public void mouseReleased(MouseEvent e){
pressed = false;
}
/*
Invoked when the mouse is not pressed only.
*/
public void mouseMoved(MouseEvent e){
x = e.getX();
y = e.getY();
}
/*
I want something like that.
*/
public void mousePressedAndMoved(MouseEvent e){
....
}
}
That's the problem with MouseAdapter, since it's a abstract class and not an interface (MouseMotionListener is the one you need), it gives empty implementations for all the possible events just to avoid you from being forced to override them all, this also implies that you could miss some of these events if you don't read docs.
If you look carefully at documentation though, you will see that you have
public void mouseDragged(MouseEvent e)
that you can override to listen exactly to what you need.

Dynamic Line between two points, click, move mouse, then click

I have to draw a Line in Java. I click one point, then release the mouse key, move the mouse (the end of the line should be where the mouse cursor is (a dynamic preview)) then click the mouse key again to make the line.
I see various questions on here, but most deal with holding a mouse button and dragging the mouse.
My question is, how can I draw a line dynamically using the method above. I am concerned about repainting. I had code earlier and it drew all the lines as I moved the mouse. Is there a way to just have a preview.
You need need to create a application implementing both a MouseEventListener and a MouseMotionListener. Use the MouseEventListener method mouseClicked to check whether the mouse has been clicked and subsequently the MouseMotionListener method MouseMoved to update the other end of the line to your mouse's position. Finally you use the MouseEventListener again to pinpoint the end location of the line.
I hope this helps.
Take a look at:
http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html and http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html.
There's a lot of information missing in your post, so it will be difficult to offer an exact solution, but here is the general idea. Assuming that you need a transparent JComponent that receive mouse events and paint your line preview, the code would look something like this.
public class MyLinePreviewComponent extends JComponent {
Point sourcePoint;
Point destinationPoint;
pubic MyLinePreviewComponent() {
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (sourcePoint == null)
sourcePoint = e.getPoint();
else
sourcePoint = null;
repaint();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
if (sourcePoint != null)
targetPoint = e.getPoint();
repaint();
}
});
}
public void paintComponent(Graphics g) {
if (sourcePoint != null && destinationPoint != null) {
g.setColor(Color.red);
g.drawLine(sourcePoint.x, sourcePoint.y, destinationPoint.x, destinationPoint.y);
}
}
}
Note that I did not run this code.
If the line preview feature has to be added to an existing component, then you will have to repaint the regular content in paintComponent() before paiting the line.

how would you detect if the mouse cursor is inside a JFrame in java?

How would you write a method to detect if the mouse cursor is inside a JFrame in java? The method should return true if it is inside or else false.
Thanks, Andrew
Assuming that the mouseEntered and mouseExited events are not sufficient (this was the case for me as I wanted to avoid extra calls to mouseExited when the mouse cursor entered a button's bounds within a panel), I came up with this short check to add to the beginning of my mouseEntered and mouseExited event handlers:
public static boolean isMouseWithinComponent(Component c)
{
Point mousePos = MouseInfo.getPointerInfo().getLocation();
Rectangle bounds = c.getBounds();
bounds.setLocation(c.getLocationOnScreen());
return bounds.contains(mousePos);
}
To expand on the comment in the original posting you can use the MouseInfo class to get the current location of the mouse. Then you compare this location with the bounds on the frame to return the appropriate value.
You should add a mouse listener and react to the mouseEntered-Event:
JFrame.addMouseListener( new MouseAdapter() {
public void mouseEntered( MouseEvent e ) {
// your code here
}
} );
Add a mouse listener to your JFrame, and look for mouseEntered and mouseExited events.
frame.addMouseListener(new MouseListener() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
// do your action here
}
public void mouseExited(java.awt.event.MouseEvent evt) {
// do your action here
}
});

Categories

Resources