Java mouse operations - java

I am writing a game in Java, and need to have mouse interaction. I was going to use MouseAdapter, but I've looked into it some, and it does not seem to have any means of determining the location of the mouse pointer without a click or action being done... What would be the recommended way of doing this?
A couple questions:
Would the mouse "location" refer to the location of the mouse in relation to the bounds of the monitor, the bounds of the game, or would it be represented as movements relative to a previous location?
How would one disable the mouse pointer in a windowed application? (ie. A first person shooter where the mouse movements rotates the players view rather than move a pointer) Is this possible?

Look at the MouseMotionListener. This will allow you to track mouse movements. It will give you the location of the mouse within the component that it is attached to. But if you look at SwingUtilities class there are some convenience methods to convert points to the screen or other components or the monitor.
For the cursor, you have the option to set your own bitmap for the cursor, you can look here. Or for a more recent SO answer, you can look here. So you could hide the cursor, or set it to an empty bitmap, and then intercept the mouse movements and events to control your view.

A tutorial on mouse events is at:
http://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html
If you look here:
http://java.sun.com/javase/7/docs/api/java/awt/event/MouseEvent.html
you can get the absolute location, on the screen, or coordinates relative to the component.
If there is a movement to change direction then you will need to remove the mouse listener, or you can just have some logic where the event handler will just exit, doing nothing. I think this would be better, otherwise you have to keep track of when you add and remove the listeners.

Related

JavaFX Side-Scrolling game: How to generate interactive objects in "extended" scene

So I am trying to create a 2D side-scrolling javafx game.
So far I used AnimationTimer to control movement of my character. But now I am kind of stuck trying to make the stage move.
I can move non-interactive elements using AnimationTimer again. But I am lacking an idea for how should I generate interactive elements in game.
For example, lets say player walks a lot of steps and reaches to take a pickup. Now how do I put this pickup in stage so it is somewhere later in game. To try explain my problem better, consider this pesky image I drew in paint:
Initially, only the screen between green bounds is visible to player. The player must walk forward (and hence the screen must walk forward too) and should find that pickup between two walls. How do I place pickup outside scene's visible view so it comes into view only when player reaches it?
The easy way: You add everything to the scene and give it absolute coordinates. You move the player by changing its coordinates in the scene. Depending on the position of the player you start scrolling. While you scroll the background you also move all other objects about the same x and y coordinates. The visible view has a given width and height. Depending on the player's position, view width/height and object range the objects become visible during scrolling.

Painting a custom Mouse over your preset mouse

I'm making a game and when I add my sprites to my screen, naturally, they are behind my mouse. But I want to add a custom mouse image to my screen, over the mouse.
I already have sprites for the mouse, and I know to to make the image appear wherever my mouse is. The only thing is that I already know its going to appear underneath my mouse. Any suggestions?
Set the image as a custom Cursor. See Toolkit.createCustomCursor(Image,Point,String) for turning the image into a cursor.

How to interact with Java3D elements using mouse?

I want the user to be able to change the length of a cylinder by clicking-and-dragging his mouse. How should I start on this ? Any form of help would be great.
Check out the example code here. The basic idea is:
Use mousePressed() listener to detect a mouse press.
Use Java3D picking API to detect if mouse press intersected shape.
Use mouseDragged() listener to detect amount of change and update your shape accordingly.

Lagging graphics in Java swing

I have a program that bounces an arbitrary number of balls around a predefined window. It relies on a Swing Timer to update the balls according to a delay set by the user. My problem is this: the balls lag much more than they should under modest circumstances. The weird thing is that the balls move smoothly if there is another action being performed (e.g. mouse click or mouse moving around the screen). Does anyone know what would cause this?
The weird thing is that the balls move smoothly if there is another action being performed (e.g. mouse click or mouse moving around the screen).
Based on that statement, I would guess that your problem is not properly calling repaint() on JPanel or other java.awt.Component subclass which is displaying the balls. You need to call Component.repaint() whenever your code changes the position of the balls.
Not sure if this might help: have you considered double buffering? (that is doing all expensive paint operations in an 'off-image' and copying that image into the visible area when done).

Java2D: Capturing an event on a Line object

I have a JPanel which has a line, circle, etc. Now when I click on the line, will the event get reported as a line event or a general JFrame event. I need to be able to move the line if the user clicks on the line and moves it. Is this possible in Java2D?
Yes, but you will need to do some work (see java.awt.Shape). Basically you need to track a list of Shapes. The JPanel will recieve a mouse event, which you can translate to (x,y) coordinates. You can then call Shape.contains(x,y) to see if your various shapes were clicked on.
This will work well for Circle, Polygon, Arc, etc; however in the case of Line2D, it won't work as easily, but you can use Line2D.intersects() with a small rectangle around the mouse click (this is also good UI since you don't want to force the user to click exactly on a pixel that is hard to see).
There is no such concept as a "line event" unless you decide to implement one.
I would suggest adding a MouseListener and a MouseMotionListener to the Canvas or JPanel onto which your geometric shapes are drawn. Use the MouseListener's mousePressed(MouseEvent) callback to determine whether a given shape has been clicked on. Once you've established this, use MouseMotionListener's mouseDragged(MouseEvent) method to move and redraw the shape as the mouse cursor is moved.
Here's a simple example that demonstrates some of the techniques adduced in other answers.
I created a canvas markup library in Java a few years back and if you don't need to worry about transforms on the canvas (scaling, rotation, etc.) it is very easy to do.
Basically you just need to maintain a collection of the canvas shapes in a List (not a Set because Z order is probably important). The mouse listener will be on your canvas, and not on individual shapes. Add new items to the beginning of your collection (or iterate the list backwards later).
When the canvas receives a mouse down event iterate through your collection of shapes until you find one that is underneath your mouse coordinates. The easiest way to do this is to have your shapes implement an interface that defines some sort of hitPoint(int x, int y) method. That way your rectangles can implement a contains(), lines can do intersects() or graphics paths, you can account for some hit padding, etc.
Taking it one step further, your shapes should define their own draw(Graphics2D g) method so that you can easily do things like selection boxes, or set the paint mode to XOR to make shape 'moving' easier. The paintComponent method of your canvas would just have to iterate through your collection of shapes, calling shape.draw(g) on each one, passing in the graphics instance provided to the paintComponent method.

Categories