Moving objects to mouseclick position java - java

I'm fairly new to programming. I'm trying to figure out how to move an object to the position where my mouse clicks within a panel. The object can be represented as a shape like a circle and is initialized at the top left corner of the panel. When I click, I don't want it to teleport to the position of where my mouse clicks but rather move there smoothly. How do I do that?

Related

How can I position mouse cursor in the middle of the screen and move the object according to mouse movement?

I am working on a 3d game where the cursor(crosshair) is in the center of the screen and when you move your mouse the object will rotate according to the mouse(like in fps games).
I've already replaced the default crosshair with my own, but I am having trouble with centring it on the screen. I've already determined the middle position of cursor placement using:
cursorPosition=new Vector2((Gdx.graphics.getWidth()-cursorSize.x)/2,(Gdx.graphics.getHeight()-cursorSize.y)/2);
And then I apply that position each time the render() method is called using this:
Gdx.input.setCursorPosition((int)cursorPosition.x,(int)cursorPosition.y);
It does not work as I expected. If I move my mouse fast the position of the cursor still moves and then it resets to the middle of the screen.
I've also tried setting cursor catched to true, but that only makes cursor invissble.
Gdx.input.setCursorCatched(true);
I want the mouse cursor to be placed in the middle of the screen at all times and then 3d move the object according to mouse movment.
Use Gdx.input.setCursorCatched(true) but then draw your mouse cross-hair to the center of the screen while the mouse is catched and remove it when you uncatch it.
Depending on how you've set up your game, your crosshair should be one of the last things you render:
public void render() {
...
spriteBatch.render(cursor, Gdx.graphics.getWidth()-cursorSize.x)/2,(Gdx.graphics.getHeight()-cursorSize.y)/2);
spriteBatch.end();
}

How corner points of a shape displayed

I try to write a program to draw a custom shapes and then execute all mouse events like dragging,clicking,moving etc.I want to know that in other editor when any shape is select or mouse is near to there boundary then its boundary point start to display.I just want the logic not code how corner points displayed like in image? I've done checking that my mouse clicking is inside of shape or not.This is a rounded-rectangle.When I clicked on its rectangle boundary is start displaying and connection points are also start displaying.How do I do that?
Shape interface has getBounds() and contains() methods. Use contains() to determine whether point belongs to the Shape and then use getBounds() to get rectable and use the rectangle's corners to draw the dragging points.

Java - Resizing a Drawn Object

Let's say that I have drawn a square on the screen using the following code:
I want that if the user clicks on the shape, a border with four small boxes (north, south, east and west) is drawn on the shape. Hovering on any of the small boxes changes the cursor to the resize cursor and, if the user drags the small box to a new location, the shape is resized.
How can this be done please?
Instead of using drawRect() method create Shape object for the rectangle and draw the Shape. For Shape you can use getBounds() to get main rectangle. Use the bounds to find the small boxes rectangles (also Shapes).
When mouse is moved (or dragged) check whether one of the boxes shapes contains the mouse event coordinates.
On drag you can change original Shape using AffineTransfrorm.

Rotate a triangle polygon according to mouse position?

I'm coding my own multi-player game in Java; I'm in need of help with rotating a triangle polygon according to the mouse position. I have this triangle, which represents the character, the player can click the right mouse button, and a bullet will fire from the top of the ship (which ever point I assign to be the top), but I want the rotation of the player(triangle), for aiming and such, to be controlled by the mouse and where it lies on the screen. How can I go about doing this, and I want to show an animation also of it rotating in respect to where the mouse is, not just appearing there.
Extra:
One more thing, after that, I'm most likely going to want to have the mouse locked to the screen, so that it won't trail off. How can I lock the mouse to the center of the screen?
I'm using the drawPolygon method from java.awt.Graphics, maybe this is the best way?

Java swing editable line drawing

I am writing a zigzag drawer as my school assignment. Basically what is expected is to be able to draw zigzags on a canvas, to be able to move vertices of the zigzags and to be able to move the entire zigzag. Also we can change the color and thickness of the zigzags.
I could manage to draw zigzags, a left click starts and subsequent left clicks continues the zigzag, and a right click finishes it. In this way i can draw several zigzags.
What i can not do is how i can make the vertices of the zigzags movable? I am keeping the point coordinates in an arraylist of type mypoint which consists of x, y and depth values. I am drawing all the painting on a canvas which is an extended class of JPanel. in the paintcomponent method i call drawline methode for every vertex in the list. As these are just paintings i cannot figure out how i will detect that the user is clicking on the vertex. Can i have little button like controllers when clicked will do the job i want. i tried to use labels and standard buttons, but neither can i position them appropriately nor are they too large to be just handlers for vertices.
Do you have suggestions on these?
You could have two modes of operations, which must be chosen by the user by selecting a radio button, for example: one for drawing zigzags, and the other one for selecting vertices.
While the chosen mode is "zigzag", the mouse clicks allow drawing zigzags as you have already implemented. When the chosen mode is "vertex selection", then a mouse click could find a vertex whose distance from the clicked point is less than 3 pixels, and the dragging of the mouse could move the vertex from its original position, following the mouse pointer.
You could inform the user about the selected vertex by displaying a small squere around the vertex.

Categories

Resources