im creating a program for drawing graphs but I have a problem, I need to know if the mouse is over a QuadCurve. How would create the function that function?
the program I'm doing is to draw graphs (draw nodes and edges). to draw the edges with the mouse at a point just press the JPanel and drag to the end point and is drawn with Graphics2D QuadCurve2D object and stored in an array of objects. each object in the object is saved QuadCurve2D and it contains 3 points (start, control, end), now I need a method to eliminate any edge that this drawn on the JPanel and for this each edge must have a method that takes as a parameter the mouse point and returns true if the point is on the curve
Hook up a mouse motion listener to your panel, then on mouse motion events use the intersects method on the QuadCurve2D object to detect whether it hits a rectangle centered on the mouse cursors new position. Experiment with the width and height parameters to find what feels best for you.
Related
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.
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.
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.
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.
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.