• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Line Thing

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
jtcy
Asked:
jtcy
  • 21
  • 19
1 Solution
 
CEHJCommented:
If the line perfectly horizontal?
0
 
jtcyAuthor Commented:
no, the line can be at any slope.
0
 
CEHJCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CEHJCommented:
Or simpler

boolan onLine = line.contains(event.getPoint());
0
 
jtcyAuthor Commented:
um...oh...what is g2d by the way?
0
 
CEHJCommented:
java.awt.geom.Graphics2D. Cast a normal java.awt.Graphics to it
0
 
jtcyAuthor Commented:
I already done: import java.awt.geom.Line2D.*; but it keeps saying package Line2D doesnt exist.
0
 
CEHJCommented:
It should be

Line2D.Double
0
 
jtcyAuthor Commented:
yeah, thats what i did and it complains that.
0
 
CEHJCommented:
import java.awt.geom.Line2D.Double;
0
 
jtcyAuthor Commented:
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
 
jtcyAuthor Commented:
okay, needs to import java.awt.geom.*;
0
 
jtcyAuthor Commented:
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
 
CI-Ia0sCommented:
As in the other thread:
int someInt = 5;
double someDouble = someInt;

You only need to ask once, you know... ;)
0
 
jtcyAuthor Commented:
Why isnt the following constructor working>?? It keeps saying identifier expected.

Vector 2DLines = new Vector();
0
 
jtcyAuthor Commented:
oh....cant start with a number as an Identifier, forgot that.
0
 
jtcyAuthor Commented:
How can I make the border of the line thicker?
0
 
CEHJCommented:
g2d.setStroke(...) before you draw it
0
 
jtcyAuthor Commented:
um...which class is the method from?
0
 
jtcyAuthor Commented:
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
 
CEHJCommented:
>>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
 
jtcyAuthor Commented:
hm,...........is there any other way?.....
0
 
jtcyAuthor Commented:
It should print that out, cuz I am sure I have clicked on the line, hm~~~......
0
 
CEHJCommented:
Can you post full, runnable code? If it's long, please post a link instead
0
 
jtcyAuthor Commented:
Hi, I have uploaded the stuff:

www.geocities.com/jonathan_tay_2000
0
 
jtcyAuthor Commented:
Here's how you can trace:

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

0
 
CEHJCommented:
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
 
CEHJCommented:
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
 
jtcyAuthor Commented:
um...you just run it with

javac *.java
java GUI

thats all.....
0
 
CEHJCommented:
No, since there are classes and source in different directories
0
 
jtcyAuthor Commented:
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
 
CEHJCommented:
I think you can imagine i don't need to be told how to run a Java archive ;-)
0
 
CEHJCommented:
>>
First node x: 35 :: 153
First node y: 10 :: 8

Clicked on 151 and 136
Clicked on 155 and 51
>>
0
 
jtcyAuthor Commented:
Wats wrong?
0
 
CEHJCommented:
Nothing apparently (wouldn't get that readout otherwise) - that's the point ;-)
0
 
jtcyAuthor Commented:
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
 
CEHJCommented:
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
 
CEHJCommented:
... such as


if (tempLine.ptLineDistSq(e.getX(), e.getY()) <= 0.0)
0
 
jtcyAuthor Commented:
I am still wondering if there is other classes that can accept mouse listener on a line.........
0
 
CEHJCommented:
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

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 21
  • 19
Tackle projects and never again get stuck behind a technical roadblock.
Join Now