Edit series by dragged mouse event Teechart in Java - java

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.

Related

Android Java: Removing boundaries on the mouse

I have a special use case that requires a connected mouse to either wrap around the screen or to remove the boundaries on the mouse entirely. Pretty much what I need is to be able to see the physical change in mouse movement.
The MotionEvent for the mouse pointer does not provide how the mouse actually moves, but only how it moves on the screen itself. I have been using the onHover(View, MotionEvent) to get mouse data, and I can single out the mouse event by constraining the events to InputDevice.SOURCE_MOUSE, but it does not give any InputDevice.SOURCE_MOUSE_RELATIVE events, and I think that's the source I need to find out these relative mouse movements.
I am a beginner in Android development, so I don't know if I'm missing the obvious, but I've tried looking through the Android documentation. Is something like this even possible?

How to get mouse position and buffer?

How do I get the mouse position. I have tried:
int mouseX = MouseInfo.getPointerInfo().getLocation().x;
int mouseY = MouseInfo.getPointerInfo().getLocation().y;
But that does it for the whole screen. Anyway to do it relative to the JPanel/JFrame
If I'm only using Graphics JFrame and JPanel that is being repainted every millisecond, should I have buffers? Or will it be fine?
How do I add a mouseAcionEvent only to the frame so it gets X() and Y() of mouse but only in frame?
Use a MouseListener instead of MouseInfo. MouseListener will trigger events which are contextual to the component which raised them, which means you won't need to translate the events into the component space as the event will already be converted to within the component context.
See How to write a mouse listener for more details
How should I update my game rePaint() every millisecond or another way?
Use a javax.swing.Timer...
See How to use Swing Timers for more details...
Should I use buffers?
That will depend. Swing components are already double buffered, but if you use a more complex timing mechanism (AKA game loop), you might find it useful, even to roll your own.
I, personally, would start simple
How can I improve the way I thought out my code in the first place? Is it right having 10 loops or only all in 1 to reduce lag ect.
There are probably lots of things, but start with broader idea...
Breakdown entities to their own responsibilities, for example, the player should know where it is and how it should be painted. It could even know how it's suppose to move based on the current state of the game. This way you could create any number of entities, all with there own set of rules which are isolated and easily updated.
Devise a controller mechanism which is responsible for taking in keyboard and mouse events and simply updating the current state of the game model. That is, rather than going "the user pressed the 'left' key, move player to the left", it would simply raise a flag in the game model that the "left" state has been triggered (or untriggered) and the engine would, on the next update loop, ensure that each entity knew about the change
Don't use magic or hard coded numbers, instead provide some kind of ability to scale the scene. For example, you could decide what is shown on the screen based on the size of the window...

Using JPanel to draw straight lines and get point coordinates

I am at a total loss right now. I haven't worked much with building GUIs in Java, I've been reading all about swing and JPanel, and I think what I am trying to do is possible, I just haven't figured out how.
I'm trying to build a GUI in which you can draw straight lines within a certain drawing area, I would like to be able to get the start/endpoint coordinates in order to perform some math with those points. Any help would be greatly appreciated!
I will leave the code to you so here is the algorithm:
1. Create a JFrame and add a JPanel to it.
2. Add a mouse listener for the JPanel
3. Every time the mouse is pressed, get the x and y of the click. (starting points)
4. When the mouse is dragged , record x and y continuously.
5. When mouse is released, record the x and y. (ending points)
6. You could either use the drawLine() method of Graphics class or use draw() of Graphics2D in this case you will need a Line2D.Double -- the arguments remain the same - start x, start y, end x and end y
here is an image to explain a lil bit better:
Start with Performing Custom Painting and 2D Graphics.
Basically, you going to need a mouse listener to monitor the user interaction with your panel, check out How to write mouse listeners for more infor still.
Depending on your needs, if you need to maintain all the click points of the user, you would need to store them in something like a List, or if you just need to know the start and end points, the you just need a couple of Point objects.
You would be able to use these to paint onto your surface and performing your required calculations.
Remember, in this context, the points are contextual to the container they were generated on. That is 0x0 will be the top left of the container
Updated
You could also take advantage of the Shape API, using a Line2D to represent the two points. This would make it easier to distinguish between distinct lines/points
This is harder than just "draw straight lines with (x1,y1) and (x2, y2)" approach.
You need a Line(your custom) object that is dynamically created and placed on the JPanel which is listening for MouseEvents The canvas area being the JPanel itself. You also need to separate the MODEL from the VIEW so that your custom canvas JPanel will draw itself properly with an override for paintComponent()
The question is slightly vague, so I can't provide any code.
you need to add the mouse listener on JPanel.
then:
public void mouseClicked(MouseEvent me){
if(click==1){
int x1=me.getX();
int y1=me.getY();
click=click+1;
}
else{
int x2=me.getX();
int y2=me.getY();
click=1;
}
}
drawLine(x1,y1,x2,y2)
To draw line with mouse move you can also add mouse motion listener.

Does SWT Canvas Provide Tools For Mouse Move/Click Active Areas?

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.

How to generate events from graphics generated by Java2D

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)

Categories

Resources