Computer Science Canada

Line to Cursor

Author:  secondgear [ Wed Mar 16, 2011 7:49 pm ]
Post subject:  Line to Cursor

Hi, I am making a program where there is a cursor on the screen.

How do I make it so the line trails the cursor around where ever it goes. Except the distance should only be 10 so the end of the line keeps dissapearing as the cursor keeps going and it should not be bigger then 10 the line.

Author:  apython1992 [ Wed Mar 16, 2011 9:53 pm ]
Post subject:  RE:Line to Cursor

Well, nobody will give you a fully coded solution, but we can help you break the problem down to smaller steps.

1. Do you know how to draw a line to the screen?
2. Do you know how to get the cursor's position on the screen?

Author:  Insectoid [ Wed Mar 16, 2011 9:54 pm ]
Post subject:  RE:Line to Cursor

You need to keep an array of where the cursor has been, and draw lines between those spots (if you draw dots, there will be spaces). You may also need to calculate distances, and do some estimation if you want it to be really smooth. Especially calculating where the cursor was between frames. That might prove tough.

Author:  apython1992 [ Wed Mar 16, 2011 10:03 pm ]
Post subject:  RE:Line to Cursor

We probably need more information about the problem. Is the line going to always have the same orientation, moving as a whole with the cursor? Or will one end of the line be fixed at a certain location while the cursor controls the location at the other end? Try to be a little more specific.

Author:  secondgear [ Wed Mar 16, 2011 11:26 pm ]
Post subject:  Re: Line to Cursor

I do know how to draw the line.

For the program I'm using it for it's

g.drawLine(int, int, int, int);

To get the current mouse it's

getCurrentMouseXY();

So basically this is what I thought of doing.

g.drawLine(getCurrentMouseXY() -10, getCurrentMouseXY() -10, getCurrentMouseXY(), getCurrentMouseXY());

I'm not sure if it'll work I can only test it on saturday.

It's basically as if the cursor moves there will be a line following the cursor in the exact same direction it moved. But the line distance would only be 10 max. Then it would dissapear.

The line nor the cursor will be at a fixed location.

The cursor will move and the line will follow it the same direction as the cursor. If the cursor stops. The line will dissapear.

Author:  Insectoid [ Wed Mar 16, 2011 11:59 pm ]
Post subject:  RE:Line to Cursor

It looks to me that your function will draw the line at a 45-degree angle only, with a length of sqrt (200). However I'm not totally sure how getCurrentMouseXY() works. Also, mouse functions are most likely part of a class you'll need to invoke.

Author:  secondgear [ Thu Mar 17, 2011 12:03 am ]
Post subject:  Re: Line to Cursor

I think what I would have to do is store the mouse points and draw a line for each mouse point. I don't seem to know how I can do that though.

Author:  Insectoid [ Thu Mar 17, 2011 12:12 am ]
Post subject:  RE:Line to Cursor

Keep an array of x-y coordinates. Every frame, get rid of the last point, and shift everything down. This could really benefit from a linked list. Lots of shitfin' happ'n'n',

Anyway, then you just use a for loop to draw all your lines.

Author:  secondgear [ Thu Mar 17, 2011 12:56 pm ]
Post subject:  Re: RE:Line to Cursor

Insectoid @ Thu Mar 17, 2011 12:12 am wrote:
Keep an array of x-y coordinates. Every frame, get rid of the last point, and shift everything down. This could really benefit from a linked list. Lots of shitfin' happ'n'n',

Anyway, then you just use a for loop to draw all your lines.


I'm pretty new to java. I have no idea how to do all that. I just tried to learn about arrays and it's confusing me. You think you can help me out a bit? By giving me like a place to start at?

Author:  apython1992 [ Thu Mar 17, 2011 1:36 pm ]
Post subject:  RE:Line to Cursor

This might be helpful for you, if you've never heard of arrays before:

http://www.tutorialhero.com/tutorial-76-java_arrays.php

Author:  secondgear [ Thu Mar 17, 2011 4:40 pm ]
Post subject:  Re: Line to Cursor

ahh I can't figure it out. I'll jsut forget it.. too hard

Author:  DemonWasp [ Thu Mar 17, 2011 5:38 pm ]
Post subject:  RE:Line to Cursor

Not to be rude, but if this is too hard you will find that any serious programming is completely impossible.


Stick with it! Don't give up just because you've hit a snag. Take a break, eat a sandwich / drink a coffee, and come back to it. Collect your thoughts: write them down on a piece of paper. Read the documentation. Read the documentation again, more carefully this time.


Try to solve the problem like a computer would, except do it by hand, on paper. Get yourself a sheet of graph paper and a pencil. Draw a rectangle (about 15x10 or so) to be your "screen". Draw an X in the square where the mouse cursor is right now. Next, draw two "tail" points that were at the last two positions. Connect them with lines.

Now, below the first screen, draw a second one. Draw the X in a new position. Figure out where the two tail points should be (hint: base this on where the mouse was in the last screen, plus where the tail points were in the last screen). It will probably be helpful to decide on a coordinate system, with the x-axis along the bottom and the y-axis along the side: then you can just figure out the (x,y) coordinate for each point! Finish by connecting the dots.

Repeat the last paragraph until you think you've got the general idea.


Then, read about arrays, if you haven't already.


Then start thinking: If I had a couple of arrays, I could hold all the (x,y) coordinates for these points, right? I could even replace "old" points with newer ones, so that it looks like the tail is disappearing behind the cursor!

Once you've got an idea of how to apply arrays to your problem, try solving it on paper again. This time, however, you must erase the first screen before drawing the second, and erase the second before drawing the third. You are only allowed to keep information in arrays at the side: one array for the x-coordinate, one for the y-coordinate.

At this point you should be able to write your program with much less confusion.

Author:  secondgear [ Thu Mar 17, 2011 9:57 pm ]
Post subject:  Re: Line to Cursor

Ok thanks I think I got some of it.

code:

                int fromX[]= new int[1];
               
                fromX[0]=getCurrentMouseX();

                int fromY[]= new int[1];
               
                fromY[0]=getCurrentMouseY();
               
                int toX[]= new int[1];
               
                toX[0]=getCurrentMouseX();

                int toY[]= new int[1];
               
                toY[0]=getCurrentMouseY();
               
                g.drawLine(fromX[0], fromY[0], toX[0], toY[0]);


Did not test it you but I don't seem to understand how I will make it pause before getting the next X and Y coordinates. Right now I think they get all the coordinates at the same time. How do I make it get the from coordinates first then get the to coordinates after.

Author:  DemonWasp [ Thu Mar 17, 2011 10:10 pm ]
Post subject:  RE:Line to Cursor

You add a delay (Thread.sleep) of a few milliseconds, or you listen to the mouse-move and draw when that event fires. It depends whether you're working with active rendering (Thread.sleep) or event-based rendering (mouse-move).

Author:  secondgear [ Thu Mar 17, 2011 10:23 pm ]
Post subject:  Re: Line to Cursor

Can you give me some code for that?

Author:  DemonWasp [ Fri Mar 18, 2011 9:17 am ]
Post subject:  RE:Line to Cursor

The documentation for the Java API is here: http://download.oracle.com/javase/6/docs/api/

The function you want to use is Thread.sleep(milliseconds): http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#sleep(long)

You will have to put the call to Thread.sleep() in a try-catch block as it can throw an exception (you can probably ignore any exception it throws).

Author:  secondgear [ Fri Mar 18, 2011 9:33 pm ]
Post subject:  Re: Line to Cursor

so umm this should work?

code:

int fromX[]= new int[1];

                fromX[0]=getCurrentMouseX();

                int fromY[]= new int[1];

                fromY[0]=getCurrentMouseY();

                try {
                        Thread.sleep(5000);
                } catch(InterruptedException e) {

                }       

                int toX[]= new int[1];

                toX[0]=getCurrentMouseX();

                int toY[]= new int[1];

                toY[0]=getCurrentMouseY();

                g.drawLine(fromX[0], fromY[0], toX[0], toY[0]);

Author:  Insectoid [ Fri Mar 18, 2011 9:42 pm ]
Post subject:  RE:Line to Cursor

Nope. fromX[] will need to be as big as the number of points you want. At the moment, fromX[] is acting just like an integer.

Author:  secondgear [ Fri Mar 18, 2011 9:45 pm ]
Post subject:  Re: Line to Cursor

I don't understand that.

fromX is the mouseX to draw a line you need fromMouseX and fromMouseY to the other X and other Y, Can you explain what I am doing wrong pleas?E

Author:  DemonWasp [ Sat Mar 19, 2011 2:50 am ]
Post subject:  RE:Line to Cursor

You've got the basic idea correct -- you will need to draw a line from the last point stored in the fromX and fromY arrays to the new point. However, you're still missing two big pieces of this puzzle.

First, you need to store at least several more entries in these arrays -- probably at least a history of the last 10-20 positions, if not more. You will also need to connect them with lines.

Second, you need to implement adding the "new" coordinates to the array.

Author:  secondgear [ Sat Mar 19, 2011 10:41 am ]
Post subject:  Re: Line to Cursor

I understand I need more points otherwise it will just be a straight line. But I want it to be following the cursor movement. I also need it to remove the last points as the cursor moves further or after a few seconds. I have no idea how to do that. Can someone just do it for me -__-??

Author:  apython1992 [ Sat Mar 19, 2011 12:31 pm ]
Post subject:  RE:Line to Cursor

But if someone does it for you, you won't learn anything! Just keep trying to figure out what you have to do, and if you don't know how to do it, you can always look it up. For example, if you think you need to remove the last points as the cursor moves further, look up how to remove elements from an array in Java. To start, I suggest looking at Java's ArrayList, instead of just a regular array.

Author:  secondgear [ Sat Mar 19, 2011 12:39 pm ]
Post subject:  Re: Line to Cursor

Well if I look at code I'll know how to do it in the future...

Author:  apython1992 [ Sat Mar 19, 2011 12:54 pm ]
Post subject:  Re: Line to Cursor

secondgear @ Sat Mar 19, 2011 12:39 pm wrote:
Well if I look at code I'll know how to do it in the future...
I'm not so sure about that. There's something to be said about thinking clearly and logically about a solution to a problem, as opposed to just blindly staring at code and wondering how it works.

The skills you'll develop from learning how to actually think about solutions far outweigh anything you'll gain from memorizing someone else's solution.


: