mouse-listeners

how are the appropriate methods of MouseMotionListener in Java Swing?

不羁岁月 提交于 2019-12-12 02:35:38
问题 consider this class : public class mycomponent extends JComponent { public mycomponent(){ addMouseMotionListener(new MouseMotionHandler()); } class MouseMotionHandler implements MouseMotionListener{ public void mouseMoved(MouseEvent event){ //do something } public void mouseDragged(MouseEvent event){ //do something } } } Now Lets say a mouse drag event occurs. How does the MouseMotionHandler knows which method to call. of the two methods implemented. Or how is the method to be called resolved

Change JLabel Background on MouseRelease Event

混江龙づ霸主 提交于 2019-12-12 00:45:30
问题 I am trying to write a Java program that when you click on a certain element in the JFrame, the background of a JLabel is changed. The function that the event handling method calls to change the background does not work when called from the event, however it does work whenever you call the method somewhere independent of any event. Does anyone know how you would go about changing the background on MouseRelease event, since using an event handling method like so doesn't work? private void

Can't filter MouseEvent.MOUSE_CLICKED in AWT EventQueue

无人久伴 提交于 2019-12-11 20:12:09
问题 I need in my own AWT EventQueue filtering mouse double clicks, so I do: public class AppEventQueue extends EventQueue { @Override protected void dispatchEvent(AWTEvent event) { super.dispatchEvent(event); if(event instanceof MouseEvent) { MouseEvent mouseEvent = (MouseEvent) event; if(mouseEvent.getModifiers() == MouseEvent.MOUSE_CLICKED) { //do something } } } } Problem I have is that it looks that click with mouse is generating int value 16, MouseEvent.MOUSE_CLICKED has value 500. Am I

How to get X,Y coordinates of an image in a JLabel

爷,独闯天下 提交于 2019-12-11 18:09:26
问题 I have an image of a map placed as the icon of a JLabel . I am using following code to get the X,Y coordinates of the location where the mouse is clicked. I have put this code in the MouseClick event of the JLabel . Point point = MouseInfo.getPointerInfo().getLocation(); double X = point.getX(); double Y = point.getY(); but the coordinates depend on the location of the JFrame form. If the form is moved the coordinates change. Is there anyway I can freeze the JFrame ? Or Is there anyway I can

registering mouse handler but handler not inline, in javafx

隐身守侯 提交于 2019-12-11 03:38:24
问题 I have an app in JavaFX that is getting a bit large, and I want to keep the code readable. I have a LineChart that I want to have zoom functionality built in, that occurs on a mouseclick. I know I need to register a mouse listener to the chart. What I cannot figure out from Oracle examples, ie as written here: http://docs.oracle.com/javafx/2/events/handlers.htm is how to NOT have my handler defined inline to the registering. In other words, I want the body of the handler (which is many lines

scope of the mouse adapter

白昼怎懂夜的黑 提交于 2019-12-10 19:28:01
问题 I am wonder what the scope of the MouseAdapter is in this case. class foo extends JPanel() { private JMenu edit = new JMenu(); public foo() { this.edit.getItem(0).addMouseListener(new MouseAdapter(){ @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 1) { edit.getItem(0).setEnabled(true); } } }); } } I thought the MouseAdapter has access to the variable edit because the newly declared MouseAdapter is an inner class of the class foo . However, it can't find the

MouseListener called multiple times

青春壹個敷衍的年華 提交于 2019-12-10 17:38:54
问题 I am using this code to get the X and Y coordinates of an image placed as icon of a jLable. This method to get the coordinates was suggested by an answer to this question. private void lblMapMouseClicked(java.awt.event.MouseEvent evt) { lblMap.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { double X = e.getX(); double Y = e.getY(); System.out.println("X: " + X + "Y: " + Y ); } }); } When I run this public void mouseClicked(MouseEvent e) { } gets called multiple

Getting the colour of where you have clicked on a JPanel?

放肆的年华 提交于 2019-12-06 11:25:39
I only want a method to activate if the pixel that is clicked is white. How would I implement this? Trying to look for a method that returns the colour at a coord, but I can't find one. Depends. If you have got a BufferedImage (or some other Image) you can use its getRGB methods (or getRaster().getPixel ). If you haven't, you can use JPanel's createImage methods and use the returned image to get the pixel data. 来源: https://stackoverflow.com/questions/2623351/getting-the-colour-of-where-you-have-clicked-on-a-jpanel

How do I get my image to follow my mouse?

情到浓时终转凉″ 提交于 2019-12-04 19:55:23
How do I get my image to follow my mouse anywhere on the screen? The below code makes the image move along the x axis. import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; public class PlayerTwo implements KeyListener, MouseListener, MouseMotionListener{ public static int PLAYER_HEIGHT = 15; public static int PLAYER_WIDTH = 15; private Image p2Image = null; private static int x = 0; private static

Dragging Image using MouseDrag method

こ雲淡風輕ζ 提交于 2019-12-04 07:09:57
问题 I'm trying to code where an image can be dragged using the mouseDragged method, but I can't seem to successfully do it with my current code. My aim is to ensure that when I place drag on every image in the applet, it follows the cursor. /* Using drag mouseEvent, all objects in the app can be dragged... */ package finals_two; import java.applet.*; import java.awt.*; import java.awt.event.*; /* 10/16/2015 * @author michael.pabilona */ public class Finals_Two extends Applet implements