![]() coordinates as the user moves the mouse add a mouse motion listener, and update my custom mouse cursor with the x/y LayeredPane.add(alsXYMouseLabel, JLayeredPane.DRAG_LAYER) ĪtBounds(0, 0, jFrame.getWidth(), jFrame.getHeight()) Im getting raw mouse data: Code: Input::Obj().Mousex Input::Obj().raw-> Input::Obj().Mousey Input::Obj().raw-> Input::Obj().MouseKeyDown Input::Obj(). JLayeredPane layeredPane = jFrame.getRootPane().getLayeredPane() add my component to the DRAG_LAYER of the layered pane (JLayeredPane) create an instance of my custom mouse cursor componentįinal AlsXYMouseLabelComponent alsXYMouseLabel = new AlsXYMouseLabelComponent() It then uses the dist() function to check whether the mouse is inside the. tDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) This code stores the position (center) of the circle and its radius in variables. SwingUtilities.invokeLater(new Runnable() This causes the coordinate origin (0, 0) to be moved to the center of the canvas, rather than being at the top left corner. Public class MouseCursorXYLabel extends JFrame * mouse as the mouse is moved over a Java/Swing application. * mouse cursor that shows the x/y coordinates of the * custom Java mouse cursor, in this case, a ![]() Lookup MouseAdapter to narrow down your code as. The method youll need to implent is mouseClicked. ![]() Sound like the parent container is using a LayoutManager. goes back to the original location once I let go of the mouse button and move the mouse. Also, super.paintComponent () (without the 's') is done at the start of the method not the end. * A Java class to demonstrate how to create a Its similar to how you add button press actions to JButtons. First of all, custom painting is done by overriding the paintComponent () method NOT the paint () method. I think everything in my example Java source code is pretty straightforward, so I've just commented the code, and I won't add much more discussion to it. To create my custom cursor, all I had to do was create a JComponent, and then override the paintComponent method, as shown in the class named AlsXYMouseLabelComponent below. My Java custom mouse cursor to display the mouse x/y coordinatesĬreating a custom Java mouse cursor like this turned out to be pretty simple, though it definitely helps to have a great book like Filthy Rich Clients at your disposal. If the object is currently drawn, the image is adjusted to the new position. Example: someObject.undraw() move(dx,dy) Moves the object dx units in the x direction and dy units in the y direction. If the object is not currently drawn, no action is taken. I can't figure the right way to word that, so it may not make sense, but hopefully this picture of what I've done will help demonstrate my custom Java mouse cursor: undraw() Undraws the object from a graphics window. Java mouse FAQ: How do I create a custom Java mouse cursor (mouse pointer)?įor my Java/Swing XY mouse position/coordinates application I finally dug into Swing and figured out how to create a Java mouse cursor that shows the X/Y position (coordinates) of the mouse cursor at any moment in time.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |