How to make vector line selectable?

I'm making a project for college, where I have to make my own primitive vector editor, and I have two questions:

  1. Is it right to make vector line object by saving it's start point coordinates, end point coordinates, color and width of points, which it will consist of? Point is also my class, which is drawn on JPanel.
  2. If it is right, how can I make this line selectable? The only thing I can think of is to check mouse coordinates to be inside of line width.

I also have a restriction not to use any standard functions for drawing lines, splines, etc.

Answers


  1. Yes that's right. Or you could represent it in polar coordinates
  2. the best thing to do is actually turn your line into an Area, which implements Shape and thus contains, which is the method you want. Area is a great abstraction because it can represent any shape but everything gets manipulated in the same way.

You can use Line2D Shape. To check selection you can gt stroked Shape from BasicStroke and check whether the stroked Shape contains clicked point.


To Point 1: You also need to save the direction of the vector.

To Point 2: There are some frameworks like GEF which do the job for you. Here you have to change your model. You need two classes: Point and Connection


  1. You can use a class Vector if you have one with start and end Point for do it if you want.
  2. You can have a method like that:

    private static Shape generateVector(Point start, Point end)
    

    In this method you can use one object of ┬┤Area┬┤ for build the vector, with Line2D. Tree lines if you want an arrow.

    Or you can use a GeneralPath for build it.

For select a particular vector with the mouse, you can get the coordinates, with getX() and getY() on the MouseEvent and ask in the shape object with method contains and repaint with other color.

Good luck!


Need Your Help

Objective-C - pass reference of self to child object

ios objective-c osx reference

I'm working on an Objective-C where I need to pass a reference of self to a child object. For instance in Java if I was to do this within a method I could say room.place(this);

Call a webservice without proxy

c# wcf soap web-services

I have to call a webservice from withing a C# programm. The webservice has most probably not a standard format. The interface description (wsdl and xsd) are very complicated, and using a proxy gene...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.