I was trying to add a mouseListener and a mouseMotionListener to my game and noticed that I can add them to the Canvas or to the JFrame. Do I add it to both or one of them?
Do I add MouseListeners to the Canvas or to JFrame
112 Views Asked by AudioBubble At
2
There are 2 best solutions below
0
AudioBubble
On
Attach it to the Canvas
You should add the mouse listener to the canvas, there is one reason for it: Coordinates.
If you attach mouse listener to the frame the 0-point of coordinates will be on the left-up corner of JFrame border. It will be hard to calculate the coordinates relative to canvas.
Instead of you can attach mouse listener to Canvas. The coordinates will be better with this. But do not forget to gain focus on canvas after adding listener:
canvas.addMouseMotionListener(motionListener);
canvas.requestFocus();
Related Questions in JAVA
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
- Redirect inside java interceptor
- Push toolbar content below statusbar
- Animation in Java on top of JPanel
- JPA - How to query with a LIKE operator in combination with an AttributeConverter
- Java Assign a Value to an array cell
Related Questions in CANVAS
- http://sigmajs.org/ les mis tutorial - why are my canvases 0 height?
- How to know whether the mousedown event is occured on where we want in canvas?
- Scrollbar for Message Widget in Tkinter
- Resize function canvas loaded on shrinked browser and expanded
- Canvas - Rotate line
- html5 canvas circle palette
- How can I make a button who draws a line
- The canvas.drawpath not work for me
- Touch listener on path android canvas
- html5 canvas with image: save original imagesize
- On iOS, how do I resize a very large image on the client side in a web app?
- Easeljs snap to grid
- a border where you can not go through (canvas game)
- How can I implement a zoom function in a 2D canvas?
- THREE.JS MakeTextSprite properties
Related Questions in JFRAME
- Running batch fine onclick delay
- timer in Jframe restart
- Dont see anything inside my jframe
- Components won't show up on JFrame
- Java Swing Drawing Rectangles
- Use JFrame (or external equivelant) on Android
- JPanels will not show up in JFrame
- While passing a variable to another class in java netbeans automatically generating extra zeroes?
- Why won't my Jbutton do anything on click?
- Java jar File error message Exception in thread "AWT-EventQueue-0"
- Multiple RadioButtons Simulator
- Java repaint() not calling paintComponent
- Changing from JFrame to JApplet
- SwingWorker calls a JFrame Class ..window shows nothing
- Whitespaces on JLabel Arrays causes project to become unaligned
Related Questions in MOUSELISTENER
- Capture moue movements on a WPF window drawn behind icons and dispose the handler on exit
- Mouse click coordinates are always 0
- JApplet does not receive mouse-events
- Shortening a piece of code gives an error; How do I solve this issue?
- Creating a main menu with a mouse listener
- How do you add a mouseListener to a jscrollbar?
- Updating Jframe based on JLabel Click
- How to find array coordinates with Mouse Listener
- Add mouseListener to Labels in Array Loop
- What is the most efficient way to determine the closest element to a mouse?
- Simulate JMenuItem MouseListener on JButton
- Get coordinates of one mouse click
- How to shift focus on the clicked tab
- Java: why left click on a button is faster than right button
- JAVA How to get Tab from JTabbedPane by mousePosition
Related Questions in MOUSEMOTIONLISTENER
- MouseMotionListener doesn't work
- How can I get this imported MouseMotionListener to work again?
- Java applet Displaying Text in window center at Start and following when mouse moves
- mouseMoved event seems to only be getting called once
- Dragging a JPanel using MouseMotionListener
- how to Combine MouseMotionListener and JPanel in java
- My MouseMotionListener is stopping my keyDown from working
- Draw Line While Dragging The Mouse
- keyPressed() method not working after mousePressed()
- How should I split joined lines for my paint app in java
- Why Does Implementing an ActionListener make the applet shrink(virtually)?
- Java AWT: How best to prevent dragged Shape from overlapping another Shape
- Do I add MouseListeners to the Canvas or to JFrame
- Mouse Goes behind part of the panel where it has been drawn on
- MouseMotionListener Is not doing what its supposed to?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
What I recommend is using the Canvas. When you are using a listener of any kind, think of where the actions will occur. Do all of your updates happen on the frame or on the canvas? If it is the latter, use the canvas to handle all of your action listener objects.
Another way to think of it is that the JFrame is just a window holding the implementation of your game. Your graphic updates, keyboard inputs, mouse inputs, and any other functionality is done through the canvas.
For example, compare the JFrame and canvas to this image of Skyrim. The window on the outside (A JFrame Object) has a close/minimize feature and the window holds the game screen (A Canvas Object).