Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Line to Cursor
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
secondgear




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
apython1992




PostPosted: 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?
Insectoid




PostPosted: 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.
apython1992




PostPosted: 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.
secondgear




PostPosted: 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.
Insectoid




PostPosted: 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.
secondgear




PostPosted: 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.
Insectoid




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
secondgear




PostPosted: 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?
apython1992




PostPosted: 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
secondgear




PostPosted: 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
DemonWasp




PostPosted: 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.
secondgear




PostPosted: 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.
DemonWasp




PostPosted: 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).
secondgear




PostPosted: Thu Mar 17, 2011 10:23 pm   Post subject: Re: Line to Cursor

Can you give me some code for that?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 24 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: