Java2D: Capturing an event on a Line object - java

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.

Related

Swing paint program - How to handle selection?

I'm trying to make a paint program using swing. I have several shapes that the user can draw, including ellipses, lines, and rectangles. The shapes can drawn (using graphics2d) with various strokes (line thicknesses) and can be filled or unfilled.
I'm trying to implement a feature such that the user can click a shape with the right mouse button and drag it to move it.
My current strategy is to poll the array of shapes is reverse order (meaning in case of multiple shapes the most recently added one is selected).
Each shape implementation has a method called isSelected(int x, int y). I need to figure out how to determine whether a point falls on the shape. One challenge is that an unfilled shape such as an oval should not be selected when clicked inside, but should be selected if it is filled. Also, selection should respond to the thickness of a stroke. That is, a line should be selectable by clicking out the outer area if it uses a thicker stroke, not just the exact center of the line.
How on earth can I go about implementing this?
I am storing an array of a custom type which implements a 2d shape from this API
Then you could use:
Shape#contains(double, double)
Shape#contains(double, double, double, double)
Shape#contains(Point2D)
Shape#contains(Rectangle2D)
Depending on the information you have and what you want to check

How do I change the color of a drawn item after pressing a JButton? [duplicate]

This question already has an answer here:
How to define multiple JButton actions from a different class
(1 answer)
Closed 6 years ago.
So the program draws a car and I'm going to make three buttons, one for flash to initialize the flashing ability, one left, to move the car one space to the left and one right, to move the car one space to the right. My question is how do I make it change color each time I press the button? So to sum it up, I don't know how to have an object change it's color each time the button is pressed while flashing is on. I assume I use paint()
Any help would be appreciated.
If you are drawing an image, you will need to load a different image file which is a different color, and then draw that image instead. You will do this inside whatever method you are currently using to draw the car.
If you are drawing a shape or text of some kind, you can call g.setColor() on the graphics object passed to the paint() or paintComponent() method before drawing the shape.
There are any number of ways to do it. The basic requirement though, is to have a Object of some kind which you can tell when it should change color, it would then undertake the appropriate action and update the UI accordingly.
You could...
Use a simple JPanel as the representation of the object and simply call it's setBackground color method when you want it to change color.
This assumes you want to make use of a LayoutManager to position the panel. While it is certainly doable, it will require some thought into the overall design
You could...
Use a JPanel and override it's paintComponent method and paint the color change there. At the simplest level, this is overkill, but if you also want to draw a "car" or an image of "car", then this becomes a little more meaningful, especially if you want to rotate the car based on the direction it's moving
See Painting in AWT and Swing, Performing Custom Painting and 2D Graphics for more details
Flashing
I assume you mean you want the object to be animated in some way and "blink" on and off.
This can easily be accomplished through the use of a Swing Timer which can be used to change the state of a simple flag which changes the way in which the component is rendered.
See How to use Swing Timers for more details
The important concept to take away from this is to create a object which encapsulates the requirements in away which is ease for you to use, so when you enable flashing for example, all you do is call the setFlashing(boolean) method and the object takes care of the rest

Java Paint Program

I have started a Java Paint program that seems to be working fine... There is just one problem. In my program I have it set up so that it repaint()'s ovals using MouseListener methods and overrides paintComponent(Graphics g). The problem is when I move my mouse to fast it begins to separate my ovals instead of making one smooth line when the mouse is dragged. Is there a way to fix this.
P.S. Keep in mind that I much rather use the fillOval method not the drawLine, because I still would like to set the stroke.
Thanks in advance
See Custom Painting Approaches for the two common ways to do painting. The example draws a Rectangle without problems as the mouse moves.
You can still set a stroke to use to draw a line between two points. You should store the previous mouse position and interpolate between the last position and the current position to create a Line2D shape. Then create a stroke that has the desired width of your oval, and apply that stroke to Graphics context, then draw the line. This link has more information about strokes and shapes.
If you really want to continue drawing ovals, you could interpolate along the line between the start/end points and draw multiple ovals in a loop.

Java Swing/AWT - painting objects with context menus

I want to make something akin to a diagram editor - an application that allows the user to create, view and edit a bunch of shapes on a canvas. My GUI has essentially three parts - a standard JMenuBar, one JPanel on the right side for showing info about the shapes and a JPanel next to it that should be used for visualizing the shapes.
Aside from that, I have a package that defines the shapes. For simplicity's sake say there is just a Square, containing the following information: coordinates on the canvas, size, user-defined name and description, color.
The main class of the project (an extension of JFrame) contains lists of Squares.
Now, I could visualize the shapes by simply drawing them in the JPanel using drawrect and whatnot, but I want to create an interactive editor - if the user right-clicks on a visualized shape, a context menu would pop-up allowing him to move it, change its properties or remove the shape altogether. Clicking an empty spot in the JPanel would allow the user (again, via a menu) to create a new shape.
Is there an automated way to do this as opposed to manually keeping a matrix mapping each pixel of the canvas to a certain shape and checking it upon right click? One where I could say, for example, draw this here within the JPanel and do something onclick...
A related question, when I edit the shape in the JPanel, how do I access the Square/Circle list in the main application class, so that I can really change it? I suppose this is simple, but right now I don't know how I would do it.
Please point me in the right direction, anything will be appreciated, tips, examples, links to relevant tutorials...
As suggested by #eugener, classes that implement the Shape interface have a contains() method that is useful for this. GraphPanel is an example that illustrates several of the features you mention.
The most common way to accomplish this is to allow shapes to determine if point is inside the shape. Hopefully your code is object oriented and each type of your shape is a class.
All you have to do is to define a method such as boolean isInside( point: Point) for each shape. Once you have those all you have to do is walk the shapes in the reverse z-order (from top to bottom) and and see where the mouse click point lends. If it does not lend on any shape - you clicked the canvas. Once you have this info you can show an appropriate menu.
Hope this helps

how to extract information from jpanel

I am trying a project called white board sharing in which I need to get the information from a panel and that information is like some drawings so how can I get it from a panel. In that panel I am drawing some rectangles circles.
In case you want to react on mouse events you might want to investigate the following sections of the Java API.
MouseListener
MouseMotionListener
Also if you want to react on mouse events that are triggered when the users moves his mouse over a drawn element you might be interested in Graphics2D and the classes that implement Shape.
You can pass a Graphics object to the panel for it to paint the items that were drawn.
It may help to think of the board as a view of some model that records an abstract representation of the board's content. As a concrete example, this GraphPanel has a very simple model containing nothing more than a List<Node> and a List<Edge>. These two members could be wrapped and serialized as discussed in this tutorial.

Categories