Solved

Line Thing

Posted on 2004-09-26
43
176 Views
Last Modified: 2010-03-31
How can I make sure if a user clicked point is on a line? I have two points and successfully drawn a line using drawLine(x1,y1,x2,y2) between p1 and p2. But I need to accept a point clicked by a user and it has to be on the line, thus any click out of the line will do nth. Any suggestion?


0
Comment
Question by:jtcy
  • 21
  • 19
43 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12154499
If the line perfectly horizontal?
0
 

Author Comment

by:jtcy
ID: 12154508
no, the line can be at any slope.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12154515
No matter. When you draw the line

Line2DDouble line = Line2DDouble(x1, y1, x2, y2);
g2d.draw(line);

....

Then in the event handler of your MouseListener

boolan onLine = line.contains(event.getX(), event.getY());
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12154519
Or simpler

boolan onLine = line.contains(event.getPoint());
0
 

Author Comment

by:jtcy
ID: 12154525
um...oh...what is g2d by the way?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12154538
java.awt.geom.Graphics2D. Cast a normal java.awt.Graphics to it
0
 

Author Comment

by:jtcy
ID: 12154548
I already done: import java.awt.geom.Line2D.*; but it keeps saying package Line2D doesnt exist.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12154573
It should be

Line2D.Double
0
 

Author Comment

by:jtcy
ID: 12154584
yeah, thats what i did and it complains that.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12154593
import java.awt.geom.Line2D.Double;
0
 

Author Comment

by:jtcy
ID: 12154604
Still....hm~~~ wats wrong?

----------------------------------------------------------------------
 Point A = new Point(A_x, A_y);
                              Point B = new Point(B_x, B_y);
                   
                              if (A != null && B != null)
                               {
                                      g.setColor(Color.black);
                                      Line2D.Double line = Line2D.Double(A.x, A.y, B.x, B.y);
                                g2d.draw(line);

                              }
-----------------------------------------------------------------------

and this is in a static class.

0
 

Author Comment

by:jtcy
ID: 12154638
okay, needs to import java.awt.geom.*;
0
 

Author Comment

by:jtcy
ID: 12154645
How do I covert an integer to a double? Becuz the four params that I used are int, I tried int.DoubleValue() but it says deference error.
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12155697
As in the other thread:
int someInt = 5;
double someDouble = someInt;

You only need to ask once, you know... ;)
0
 

Author Comment

by:jtcy
ID: 12157729
Why isnt the following constructor working>?? It keeps saying identifier expected.

Vector 2DLines = new Vector();
0
 

Author Comment

by:jtcy
ID: 12157743
oh....cant start with a number as an Identifier, forgot that.
0
 

Author Comment

by:jtcy
ID: 12158103
How can I make the border of the line thicker?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158157
g2d.setStroke(...) before you draw it
0
 

Author Comment

by:jtcy
ID: 12158164
um...which class is the method from?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158208
0
 

Author Comment

by:jtcy
ID: 12158223
Yup, got it. However, I still cant ge the contains thing to work.

---------------------------------------------------------------------------------------
public void mouseClicked(MouseEvent e)
        {
            System.out.println("Clicked on " + e.getX() + " and " + e.getY());
            for (int f=0; f<Lines2D.size(); f++)
              {
                   Line2D.Double tempLine = (Line2D.Double) Lines2D.get(f);
                   if (tempLine.contains(e.getX(), e.getY()))
                    {
                           System.out.println("CLICKED ON POINT!!!");
                           .............
                    }
              }
        }

-----------------------------------------------------------------------------

It never succeed printing the CLICKED ON POINT out when I click on the line. It;s weird. Is it too sensitive that I have not clicked that close on the line or what. hm...

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158248
>>Is it too sensitive that I have not clicked that close on the line or what

Could be. I should perhaps have mentioned, in case you're not aware, that increasing the thickness of the line stroke does not affect the geometry, so you'll need to click the exact middle ;-)
0
 

Author Comment

by:jtcy
ID: 12158285
hm,...........is there any other way?.....
0
 

Author Comment

by:jtcy
ID: 12158294
It should print that out, cuz I am sure I have clicked on the line, hm~~~......
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158317
Can you post full, runnable code? If it's long, please post a link instead
0
 

Author Comment

by:jtcy
ID: 12158346
Hi, I have uploaded the stuff:

www.geocities.com/jonathan_tay_2000
0
 

Author Comment

by:jtcy
ID: 12158361
Here's how you can trace:

Look at the load listener, then drawMap(), then CoordinatesArea.MouseClicked()

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158381
That's a bit too much work jtcy (especially for 30 points ;-)). Since you're so near, put it into a runnable (doesn't need to be executable - yet) jar file instead, so it can be run with

java -classpath project.jar SomeClass
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158389
Also, check that the code you last posted is getting called:

boolean foundLine = false;
// loop (set foundLine if found)

if (!foundLine) {
    // print no lines found
}
0
 

Author Comment

by:jtcy
ID: 12158409
um...you just run it with

javac *.java
java GUI

thats all.....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158421
No, since there are classes and source in different directories
0
 

Author Comment

by:jtcy
ID: 12158431
you just extract them to a directory and all files and folders will be put and arranged into sub folders automatically, and all you need to do is run GUI.java. Don't extract by dragging files out youself. Press "Extract" button on winzip and choose a folder and it will place relevant files into sub folders itself and left GUI.java at current directory.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158476
I think you can imagine i don't need to be told how to run a Java archive ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158489
>>
First node x: 35 :: 153
First node y: 10 :: 8

Clicked on 151 and 136
Clicked on 155 and 51
>>
0
 

Author Comment

by:jtcy
ID: 12158517
Wats wrong?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158567
Nothing apparently (wouldn't get that readout otherwise) - that's the point ;-)
0
 

Author Comment

by:jtcy
ID: 12158706
I just found this:

*****
Tests if a specified coordinate is inside the boundary of this Line2D. This method is required to implement the Shape interface, but in the case of Line2D objects it always returns false since a line contains no area.
****

- - ~
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158760
My apologies: in that case the class conforms to the Euclidean definition of a line, which is good in a way. And i thought that readout was showing your object registering line clicks, but i suppose it's registering just points, i.e. mouse clicks.

There's one way to do it that will definitely work: take the general equation of a line and see if the coordinates fall on it. I assumed that's exactly what the library class would be doing - one could argue that their Euclidean (non-)implementation is unhelpfully pedantic.

I can't help thinking that the package provides a better way though, so before you start wheel reinvention per my suggestion, make sure you investigate thoroughly
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158776
... such as


if (tempLine.ptLineDistSq(e.getX(), e.getY()) <= 0.0)
0
 

Author Comment

by:jtcy
ID: 12158785
I am still wondering if there is other classes that can accept mouse listener on a line.........
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 30 total points
ID: 12158807
AFAIK no, as they'd have to be some sort of component and hence rectangle-oriented in the normal plane. The way to go is per my last i think, although i haven't tested it. Gotta go ...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question