Java 2D. Hovering over Circle - java

If I draw some circles using Java2D. Is there a way display some text when I hover over any of the circles? i.e. I want to display the ID of that circle and some other stuff.

There are a number of ways to accomplish what you want. This is one solution. I assume that you are using Ellipse2D to create the circle. And I assume that you are drawing the circle on a JComponent like a JPanel.
So you declare the Ellipse.
Shape circle = new Ellispe2D.Double(x, y, width, height);
Then you implement MouseMotionListener to detect when the user moves the Mouse over the JPanel.
public void mouseMoved(MouseEvent e){
if(circle.contains(e.getPoint())){
//the mouse pointer is over the circle. So set a Message or whatever you want to do
msg = "You are over circle 1";
}else{
msg = "You are not over the circle";
}
}
Then in the paint() or paintComponent method (whichever one you are overriding to do the painting):
g2.fill(circle);
g2.drawString(msg, 10, 10); //write out the message

I don't know if You can do this directly. But You can use simple math to check cursors position: (x-a)^2+(y-b)^2=r^2 where x,y is cursors position a,b is circles center and r is radius.

You'll have to save all the centers and radius and test it against the current mouse position.
it's pretty simple operation. If the distance of the mouse position and the center of one of the circle is smaller then the radius, the mouse is inside it and you can draw the hover message you want.
there is a question here that shows the math: Equation for testing if a point is inside a circle
Hope that helps...
There is a Polygon class that might do it for you (the contains method), but none of the implementing classes is a circle :S

Related

Draw shapes in Java based off if/else statements and action listener

I'm trying to figure out if there's any way to draw images (drawRect(), drawOval(), etc.) based on if/else statements or by using an ActionListener.
I don't want to post the complete problem because this is for an assignment, but for my own scenario:
If I have a button on a JPanel named "draw rectangle" and I have the x, y, width, and height from user input, is there any way I can attach an actionListener to "draw rectangle" that could somehow draw the rectangle using those values (passed by reference?).
I know I can use paintComponent, but I can't put that into the ActionListener and it seems to do things of its own accord and not based on a specific user's actions.
I don't really have any code for this because I can't figure out how to do it at all.
If I have a button on a
JPanel named "draw rectangle" and I have the
x, y, width, and height from user input, is there any way I can attach
an actionListener to "draw rectangle" that could somehow draw the
rectangle using those values (passed by reference?).
The short answer is yes.
Generally, you'll need to save the rectangle instructions in a model, and have the JPanel redraw the model when any of the shape buttons are pressed.
Let's take your rectangle example. What do you need to know to draw a rectangle on a JPanel? You need a starting point (upper left) and an ending point (lower right). You can use a java.awt.Point to hold the starting and ending point. You can set the thickness of the line, in pixels. You can set the color of the rectangle, using a JColorChooser. You can also set the rectangle to be an outline or filled with the chosen color.
That description will be similar for a line and a triangle. A circle is a little different, with a center point and a radius. As you can see, we already have a lot of information to keep track of just with these simple geometric shapes.
Then there's the issue of the drawing surface itself. It's possible to have a drawing surface larger than your computer screen can show. You can put the drawing surface inside of a JScrollPane.
All of these things must be determined so you can build the model of your application. You do this before you build the view using Swing components and the controller using action listeners.

Moving and rotating a Collision rectangle

I am writing a simple game, or so it seemed, that has a functionality of rotating a Rectangle as the mouse moves. This did not seem like a problem at first but it just became a problem. What the Rectangle should do is rotate around a point as the mouse moves around on the Panel.
If you look at the picture, when the mouse moves the Rectangle will rotate. I know you can use the rotate functionality with Graphics2D.
g2d.rotate(angle, centerx, centery);
This is not very help full because I can not get the coordinates of the moving rectangle. This rotates the full graphics! How will I be able to draw this Rectangle so that it does this. I don't have an idea as on how to start. Please help.
Some more code and context would be nice, but based on the current question: You could create a transformed shape. Particularly, Rectangle and Rectangle2D implement the Shape interface. And you can create an AffineTransform that represents the rotation that you are currently doing to your Graphics. So the relevant part of the code should roughly look like
Rectangle2D rectangle = ...
AffineTransform at = AffineTransform.getRotateInstance(
angle, centerx, centery);
Shape rotatedRectangle = at.createTransformedShape(rectangle);
g2d.draw(rotatedRectangle);
You mentioned "collision" in the title. If you intend to use this rectangle in some sort of collision detection, you should note that it is not directly possible to intersect two arbitrary Shape objects. Particulary, you can not intersect the Shape rotatedRectangle with another Shape otherRotatedRectangle, but only with a Rectangle otherRectangle. If this is an issue, you have several options, but this would rather fit into a dedicated question.

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 - Draw Square Around Circle

I have a program containing five buttons:
i) Square ii) Rectangle iii) Circle iv) Triangle v) Clear
Clicking on any of the first four buttons will draw the respective shape on the drawing canvas using the following code:
The shapes can be dragged. They also should be resized. I was just trying to change the cursor of the mouse around the four edges of the shape (NW, NE, SW, SE). This works fine for squares and rectangles since they have four sides. However, I have some problem implementing the same for the circle and triangle.
This is what I did in my mouseClicked event:
What I want to do is that if the string shape_type contains circle, for example, it draws a border around it so that the user can see the boundaries of the shape.
However, I can't just use the following code inside the if statement:
How can I draw the border please? Thanks :)
You are correct that you can't do painting in a MouseListener method. What you can do is set shapeUnderMouse in your mouseClicked method, then call repaint. You'll get better performance if you only repaint the areas that you know are changing visually, but it's not strictly necessary.
The if (shapeUnderMouse != null) block should be placed in your paint method.

How to move Area class object in Java Graphics2D

I'm working with Graphics2D(java)
I'm trying move a random shape with mouse drag.
This random shape is stored in Area object of java.awt.geom.Area class.
I know how to select that area object, I just need to figure out how to actually move it to the new coordinates.
Shapes like ellipse and rectangle can be easily moved but how to move shapes like polygon or shapes that contain curve and no specific predefined structure.
In order to draw a rectangle using the Graphics class, you need the origin point and the width and height.
In order to draw an ellipse using the Graphics class, you need the origin point and the width and height.
Each of these simple figures is made up of an origin point and the dimensions of the figure.
Similarly, for any complex shape, you need an origin point. It's the origin point that "moves" when you move the shape with a mouse drag. In other words, the origin of the mouse move corresponds with the origin of the figure. As the mouse moves to new X and y coordinates, your origin moves to new x and y coordinates.
Let's say a rectangle has an origin of 10, 10. Let's say the origin of the mouse drag is 30, 30. As the mouse moves to new x and y coordinates, you change the origin of the rectangle. In this example, when the mouse has moved to 40, 40, the origin of the rectangle becomes 20, 20.
The rectangle and ellipse already have a draw method in the Graphics class. You'll probably have to write your own draw method for the complex shape.

Categories

Resources