I have made an Ellipse with the help of java.awt.geom.Ellipse2D
Now, I want that whenever user clicks on that ellipse, an event is generated so that I can listen to that event and peform subsequent tasks based on the ellipse which generated that event.
Here is a simple example of an object drawing program that demonstrates click, drag and multiple selection. Also consider JGraph, which is a much more advanced library for graph visualization.
I'm going to assume this is a question asking a way to listen to mouse clicks which are made on an ellipse drawn on some Swing component using Graphics2D.draw.
The simple answer is, there is no way to generate mouse events from graphics drawn on a surface.
However, here's an alternative approach:
Store the Ellipse2D objects from which the ellipses were drawn from in a List.
Register a MouseListener on the Swing component where the user is to click on.
From the MouseEvents generated from the mouse clicks, determine the location at which the mouse was clicked (using MouseEvent.getPoint), and check if the mouse click occurred in any of the Ellipse2Ds contained in the aforementioned List, using the Ellipse2D.contains method.
I don't think this is possible without lots of handcoded stuff (letting the canvas or whatever, listen to mouse events, and calculating yourself if the ellipse was clicked on).
If you want to do more like that consider scenegraph. With that the ellipse would be an object in its own right and you can register event listeners.
Edit as response to comment:
Scenegraph: https://scenegraph.dev.java.net/
google for more resources: scenegraph java
And yes. Scenegraph ist part of the JavaFX stuff, but works nicely with pure Java (no FX)
Related
I'm trying to move the curve of the graph by dragging the mouse,
is this possible?
Here's a visual example of what I'm attempting to do:
I'm using the Teechart library in java, to draw the series I use a Jpanel (Swing).
Is there a function that allows me to do this?
I'm not familiar with Teechart. Maybe it will handle mouse input. If not, add a MouseMotionListener to the component that's displaying the graph, and implement the mouseDragged method. The MouseEvent parameter has methods to tell you where the event occurred. Use that to decide whether to update the graph, and by how much, and call the appropriate Teechart methods. You might also want to add a MouseListener and implement mousePressed and mouseReleased. The sequence of events should be Pressed, Dragged, possibly more drags, Released.
I need to create a line graph to allow me to drag it in different coordinate axes, when dragged backward the graph should show data that have already been painted.
"Currently I have some graphs showing real-time data, I receive this data via sockets, then I want to be able to pause the graph and dragging back to see the data that have already been shown."
I'm not experienced with Javafx, so I apologise if this misses the mark.
I have made something similar in the past using my own component and a JScrollPane. You can make a "scrolling savvy" client, allowing you to render the history as the user drags the graph backwards. If you make your client Scrollable, you have full control on how it responds. While this isn't javafx specific, there is a javafx panel, so I can't see a reason this wouldn't work, assuming comparability with light weight swing components.
Oracle introduce it here: http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html#scrollable
I want to make a rectangle to move object insade rectangle to other position on canvas, if anyone has any suggestion I will be very happy.
Here is an example:
This must be a paint application, so you should already have some code and you'd better post it, so we could use as the starting point.
You need to write a custom widget and intercept touch events to build the selection. Whenever the selection changes, you have to repaint the UI to draw the selection rectangle. When you detect the end of the gesture, the widget listens to touch events to decide if the user wants to move the selected area (the touch gestures started inside the selection) or deselect it and maybe start a new selection. Again, if the user is dragging the selection, every new touch event causes an invalidate() on the component.
What exactly is it that you are displaying?
If it is a image you can dimply duplicate the pixels in a selected rectangle and temporary save it in a new image until the drag motion stop and then paint it back on the original image.
I am trying to build a Media Player. I have all the code for music playback. And the GUI is made up of 3 main parts:-
A blue background.
A JList with JScrollPane
A JLayer Shell (with a polygon cut out to reveal the list and blue background beneath it.)
So my question is how can I create a custom list or JTable that is slightly rotated with the base of the component stretched out on both corners (using a perspective filter). Also the JScrollbar attached must be customized also. I guess at least it needs to be rotated. But would like to know how to make one by supplying my own graphics. Is this possible?
Here is a MOCKUP of the design:
http://www.splashportal.net/MOCKUP/iJuk-MOCKUP.swf
Here is a Screenshot of it:
You can transform a view component, as shown here, but mouse interaction is effectively impossible without re-writing the UI delegate. Alternatively, you may be able to adapt the approach shown here that uses an inverse transform to effect mouse control.
I'm creating an application with Java and SWT, and have a workspace generated on a Canvas. I need to make certain areas (controls) on the canvas trigger an event when mouseover-ed or clicked. Of course this could be done by listening to the MouseMoveEvent and checking the location of the mouse manually, but I would like to know if SWT provides an easier way to do this.
Thanks
As far as I know, there is no facility to automate this. You will need to register mouse listeners and investigate the x & y coordinates manually.
A different approach might be adding individual Canvas objects onto an enclosing Canvas. This way, you could add listeners to the individual controls. This will, however, use more memory (as the underlying object has a buffer, as well as the control on top) and redrawing will be more CPU intensive.
My personal preference goes to the first technique.
Thanks for the answer, Paul. I finally created an ArrayList of Rectangle objects for each control. Whenever the mousemove event triggers, I loop through each Rectangle and call Rectangle.contains() with the mouse coordinates. This solution turned out to be very organized.
In my case, the control events are homogenous (they all do the same thing), but if anyone has multiple control actions, Rectangle.data can be used to contain a Command object.