How to interact with Java3D elements using mouse? - java

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.

Related

OpenCV-Java Click a shape with mouse and change its color

Assume that we have a shape on black screen such as:
Blue shape on black
I want to click to this shape with mouse and want that shape's color change. For example, after clicking:
Gray shape on black
I know I can do it with shape detection and with a mouse click event or just changing matrix's values. I tried them but these two solutions are a little bit long for my project. I am just looking for a simple OpenCV method that I can do this operation much more efficient.
Maybe some function which can do the same job with Photoshop's "Magic Wand Tool" can be useful. I searched on internet but I cannot find any solution so I want to ask here. Again, good to remember, I can implement the solution but I just need a method in some framework.

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 do I show a circle when touched on android?

I am trying to make an android app and I can't seem to find anywhere how to make a simple activity where a circle shows up when anywhere on the screen is touched. It doesn't have to link with the coordinates with the click, just a circle in the middle of the screen. I have the frameview and everything set up, I just need to figure out the circle class. Any help?
Use Touch events to get the touch points of x and y value.
refer this link
use canvas to draw circle.
refer this link
This tutorial is kinda old, but should get you most of the way there...
You should use a SurfaceView with a drawable at the place where screen is touched.
Refer to this sample which does something similar
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/animation/BouncingBalls.html

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.

Java mouse operations

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.

Categories