• Post Reply Bookmark Topic Watch Topic
  • New Topic

connecting two objects in Java project  RSS feed

 
jaya mishra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone
I am posting here for the first time.
I need urgent help though if anybody would be so kind.
So I have to create a project in java and I need steps on how to consider thinking about the problem.

Its a GUI related project. What is required is that there are two objects that a user would place on the screen window(imagine them like rectangles on the screen).
And these rectangle objects have connector points. One point on the middle of each side of the rectangle(Hope that made sense).  And the user will choose one point in each rectangle and my program will have to find a path  between those two connector points. There could be obstacles like other rectangles between those two rectangles or walls.

So I am thinking I could use a path finding algorithm(like A*) to find a path between the two points but that's all I could figure out till now.
I need help on how to think about this problem especially with the GUI part.
Also the problem also requires that the number of turns be minimum. But I think I could work on that later. But if you've got an idea please share it here
Thank you so much in advance
(I am sorry if this looks like I am asking you to do my work. Its just that I don't even know how to start and I wouldn't mind a little hand holding. The project is due in two days and I am too stressed out for creative thinking. Sorry again)
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think you are right about using a path finding algorithm. You sh‍ould do that independently of the GUI. Create objects representing the sizes and locations of the rectangles and their control points. Also something to represent a path between the two. Maybe a List of points with (xy) locations. Start by writing that on paper, and you sho‍uld find simple paper instructions easy to turn into code.
 
jaya mishra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Campbell Ritchie
Thank you so much for replying.
Here's the main problem that I am having: How do I connect the gui part and the path finding algorithm?
Because I was thinking of the path finding algorithm in terms of a 2d array grid. So I can look at neighbour points and the end point and start point as elements of the grid.
But how do I do that with gui?
Thanks again
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As said, you do not need any gui for now. If you have your starting and ending point, and all possible rectangles in between, all specified in 2D-coordinates, then you can apply one of the path finding algorithms lying around the internet. Once you have your path, also specified in 2D coordinates, then it should be an easy matter to visualize it within some frame. I would advise to start with relatively small grids, so that you can test your code quickly.

However, the Shape classes may be helpfull, since all these have a handy method "intersects".

If you do want a gui to draw upfront the grid with all and everything on it, to measure the coordinates, you could use a spreadsheet for that, or a painting program.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create shapes corresponding to the objects in your model; there are shapes which look like rectangles and you can create others which look like paths through a maze.
 
jaya mishra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Piet Souris
"then it should be an easy matter to visualize it within some frame"

This is where I have the problem. Can I you please go into more detail on this
Thanks again for the reply.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allright. But have you solved a particular problem yet? If so, can you show us what data you have and in what form?
 
jaya mishra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Sorry for the late reply

So right now I am getting a path on a 2D array.

This could be one of the paths like  in the image.

The red boundary --> shows the rectangle boxes I have to create.
The blue boxes --> shows the connector points

1--> the starting connector point user clicked
2--> the ending connector point user clicked

There are blocks which could be other rectangles(I just created a small block but in reality they are other rectangles)

The line of  'o' represent the path


How do I convert all this to GUI is my main concern.

Thank you again for replying
Capture12.PNG
[Thumbnail for Capture12.PNG]
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do the arrays look like which represent the paths? Can you describe those rectangles in terms of location and size? Can you give locations for those control points?
Once you can do those things, you will find it surprisingly straightforward to convert those data to display objects.
 
jaya mishra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The path is stored in an arraylist. I am using the A star algorithm.
See but the problem is that first the user places those rectangles in the window. How do I convert the location of that rectangle to points on the 2d array. Also I would have to place such that the connector points also get a location in the 2D array.
Because the rectangle can be placed anywhere on the window(JFrame). And since my 2D array could only be so big(because then A* would take a lot of time to find the path) I cannot find an exact pixel location for the rectangle to 2D array.

Once I have those points I could then find a path. and then convert that it back to GUI.
But how do I do the initial step of converting GUI to 2D array points?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaya mishra wrote:. . . But how do I do the initial step of converting GUI to 2D array points?
Do you want to do that? I thought you had a rectangle whose coordinates you know and a path whose windings you know and you wanted to convert 2D array points to a GUI, which is not at all difficult. You usually want a representation of the structure of your display and the GUI is simply there to make it visible. If you have the coordinates and sizes in some object somewhere there are all sorts of classes in the 2D package which you can read about here which will render your object in a display.

If you have the coordinates of all the turnings in your path, there are Path classes in that package which can take those coordinates and turn them into a series of lines. The same applies to rectangles. Read the link I posted and look at the classes in this package.
 
jaya mishra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Campbell Ritchie
Thank you for all the help you are giving me. Really. 

Here is the problem I am still unable to get.
So initially the user would see a plain window. He would place a rectangle(1) at one location and another rectangle(2) at another location. These rectangles would have those connector points like I mentioned before.
Now the user would click any(of the four ) connector point on one rectangle and any one(of the four) connector point of the other rectangle.
Then I have to find a path between those connector points.

Now the problem I have is
1) How do I convert where the rectangle is placed by the user to  a 2D array point.
2)Also how do I make connector points such that  they also get a specific 2D array point.


My 2D array is :

public static int[][] Mapdata =
{
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1,-1 , 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1,-1 , 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1,-1 , 1, 1, 1, 1, 1, 1},
{ 1,1,-1,-1,-1, 1, 1, 1, 1, 1 ,-1,-1,-1, 1, 1, 1},
{ 1,1,-1,-1, 1, 1, 1, 1, 1, 1 ,-1,-1, 1, 1, 1, 1},
{ 1,1,-1,-1,-1, 1, 1, 1, 1, 1 ,-1,-1,-1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 1, 1, 1, 1, 1, 1}
};

Here -1 signifies the blocked points. And 1 signifies the path where I can travel.
I converted it into the image( -1 to '#', 1 to '.' , and the path to 'o' ) you saw because it because it was easier to look at.

Also I gave the start point and end point
Node node_goal = new Node(null,null,1,12,5);
Node node_start = new Node(null,node_goal,1,4,7);

Offcourse when the actual program is created I wouldn't be able to give all the(node_goal, node_start, -1s) points myself. It would depend on where the user clicks

So this is what I want to do. Get the points where the user clicks and convert it into 2D array points and then find a path. After I find the path in 2D array I would convert it back to gui.
Although now I feel this whole converting thing is a wrong approach.

Can you please help me more on that.

Thank you again so much.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaya mishra wrote:. . . the user . . . would place a rectangle(1) at one location and another rectangle(2) at another location. These rectangles would have those connector points . . .
That allows you to create two rectangle objects and two connector point objects. You can use a mouse listener to identify the points clicked for the corners of the rectangles and the locations of the connector points.
. . . (. . . four ) connector point on one rectangle . . .
I thought you only had one connector point per rectangle. That is only a minor complication. You can click 4× or something like that to create the four points. You can use methods in shapes to find whether the mouse click was inside that object or not and pick up the click to match it to a connector point.
. . . 1) How do I convert where the rectangle is placed by the user to  a 2D array point.
Why do you need arrays? You can get things like x and y (top left corner) from a shape and use that to identify points on it.
2)Also how do I make connector points such that they also get a specific 2D array point.
Not sure. I can think of a few options. Make the class a subclass of another shape? Simply use a point object?
My 2D array is : . . .
Apart from that fact that you haven't got a 2D array . . . what is wrong with that? That looks like a normal way to represent a map to chart a path through.
Your array isn't a 2D array because Java® doesn't support 2D arrays. That is an array of arrays.
Get the points where the user clicks and convert it into 2D array points and then find a path. After I find the path in 2D array I would convert it back to gui.
Although now I feel this whole converting thing is a wrong approach.
What is wrong with that? The normal concept with GUIs is that you create a model and alter the model as the user enters information. Click locations are of course a kind of information. Then the model's changes are rendered on the display. That appears to be what you are doing.
Can you please help me more on that.

Thank you again so much.
That's a pleasure . You have got a lot further on your own than you think.
 
jaya mishra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey again

I think I am not able to explain my problem very well. Let me put it this way. So lets say in the JFrame window(full screen size) the user places one rectangle at (250,47), ie lets say its top left coordinates. And the rectangle is of constant size.
And lets say the user placed another rectangle at (407,290)
These are random coordinates offcourse.
Then what do I do next?
All I can do right now is find a path in the 2D array(Mapdata[][]).
How do I take those coordinates and map them to the Mapdata. Then find a path in Mapdata and then again be able to show that path in JFrame.
The problem I think is that there are too many pixels to map to each point in the array.

Do you get what I think the problem is.
The only way I think I could approach this is creating a very large array. With as many pixels so each pixel would get a point in 2D array. Then everything would be easy.
But creating such a big array would make the program take a very long time to calculate the path. I am pretty sure the program would hang.

I am sorry if all this sounds must be very obvious. I am just not able to understand how to approach something like that.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaya mishra wrote:. . . lets say in the JFrame window(full screen size)
Don't put the squares directly onto the frame. Add a JPanel which occupies the entire frame and paint the shapes on that.
the user places one rectangle at (250,47), ie lets say its top left coordinates. And the rectangle is of constant size.
And lets say the user placed another rectangle at (407,290)
Now you have two arrays of coordinates {250, 47, 300, 97} and {407, 290, 457, 340} if the rectangles are actually 50×50 squares. Remember you can add a mouse listener to the panel and that will give you the locations. You can use a similar technique to add the connector points.
. . . creating a very large array. . . .
Make each number on the array represent a 20×20 square of the display, or something like that. You have now reduced the size of the array by 400×. If you have a maze with passages small enough to be represented as single pixel widths, you would never see it on screen, so I can't see why there is a need for every pixel to be represented individually.

I think you are going to want shape classes, and you probably want them in a List (or array) and see which intersects the current mouse location. If you look up some of the shape classes (e.g. this one), you will find they have methods for detecting that sort of thing already.
I know precious little about path finding algorithms, but I can see what you are doing and I can see similarities between your arrays and some shape classes in the 2D package, which I think I gave you a link to yesterday.

[edit]Presumably you have a mechanism for adding the location of the rectangles to your forbidden zones in the array.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively,

if you want to start with a simple GUI and with a limited gridsize, I would not use advanced things like the Shape classes. I've devised some code that mimicks your earlier text based screenshot, and it uses a 15 x 15 grid.
You can turn a gridpoint into a WALL element by clicking on it. If you click again, then you get a Rectangle element, clicking again gives you a starting point ("s"), then a finishing point ("f") and then back to an empty element again.

You start the whole thing by prssing the "reset" button. After you are done with the set up, press the "Find Path" button, and it will draw a path, currently from top left to bottom right. It "calculates" this path by using a SwingWorker, so that the GUI does not slow down.

Disclaimer: this program is only a toy, meant as a demo, and not meant for anything serious work. It is only tested very slightly. But I hope you get an impression of what it means to get a program running in a GUI. For serious work, like drawing real Shapes and finding paths in a much larger grid, that is more complex, but something for next week?

Here is a link to the code in DropBox:
https://www.dropbox.com/s/g06kyok9c6l2oxh/JayaMishra.java?dl=0
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What am I doing wrong? Piet's app always seems to go straight along a diagonal. I had some Rs some #s one s and one f and the oooo went straight from top left to bottom right.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Campbell,

you are not doing anything wrong. The code is only meant to give OP an idea what it takes to make a GUI version. And therefore, my "path finding algortithm" is a tad on the simplistic side: from top left to bottom right. Well, it served only to test the code. OP must of course fill in his (existing?) algorithm (the A star algo that he mentioned).

By the way: in the PathFinderWorker I have somewhere "Math.max" where it should have been "Math.min".

A full blown version, with dragging real 2D rectangles and walls, and determining how starting and finishing points should look like, that is much more involved, and takes quite some experience.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hahahahahahahahahahahahahahaha!
 
jaya mishra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys
Thank you both for all your help.
I am now trying to work on the GUI part. Its actually really tough to be honest.
Lets see how it goes.

@Piet -> Thanks for taking the time to write that code. Really helpful
@Campbell Ritchie -> Thank you seriously for answering all my queries. I would have been blocked forever in stackoverflow if I asked those questions there I suppose.
Although I can't guarantee this is the end of my questions. 
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaya mishra wrote:. . . Thank you . . .
That's a pleasure
I would have been blocked forever in stackoverflow if I asked those questions there I suppose. . . .
Hahahahahahahahahahahahahahahahahahaha!
I can't guarantee this is the end of my questions. 
Quite right. You need to keep asking until you get the app working.

Please show us what figures you have for your rectangles and for your path. Do you have lots of pairs of (xy) coordinates? I think you are closer to getting it working than you think.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!