last day (34 days later) » 

8:53 PM
1
A: How do I create an object of an ArrayList <Shape> in java

launepublic class NamedShape { private String name; private Shape shape; public NamedShape( String name, Shape shape ){ this.name = name; this.shape = shape; } public String getName(){ return name; } public Shape getShape(){ return shape; } } Now you can creat...

 
Thank you so much :)
can you please just tell me if the shapes that are inside this array "shapes" ArrayList<Shape> shapes = new ArrayList<Shape>(); will also be present inside the ArrayList<NamedShape> t = new ArrayList<NamedShape>(); if no then how can i use this new array "NamedSpace"? My Shapes are initially saved inside the array "shapes". How it will work with the array "NamedSpace" can you please explain to me?
Please just clarify this?
Based On what you posted, Do i have to remove my ArrayList: ArrayList<Shape> shapes = new ArrayList<Shape>(); ? I will have to edit my whole block of codes if i have to remove this arraylist shape. Is there a way that i can link the array "Shape" to "NamedShape"?
Can you please tell me how i do this for loop? ` for (NamedShape s : shapes) { graphSettings.setPaint(strokeCounter.next()); graphSettings.draw(s); }` Its giving me error on the last line for the "s"
 
Done - see extended answer. NamedShape is just a "parcel" for two objects. Where you had a Shape, you now have a NamedShape, and you have to call getShape() on it to extract the Shape. - You can always extend it, adding more objects.
 
Superbe!! You are just great!! now if i want to retrieve data of a specific index for the NamedShape() what do i do? I want to retrieve the "Shape" and the "Name" of a specific index inside the array "NamedShape"?
Can you please see my edited code above and tell me based on what you've advised me if my arraylist will look like result1 or result2? This will help me to proceed and remove my doubts.
 
It'll be more like result #2: one object per (Array)List entry, each NamedShape object contains a reference to a String and one to a Shape. Note that the reference you take from your original List<Shape> is the one my snippet inserts into the NamedShape. You can, provided i is less than the list's shapes.size() use shapes.get(i) to retrieve a NamedShape object and apply getShape() to that: shapes.get(0).getShape() would return the first Shape added to the NamedShape list.
 
You are magic :) Thank you so much. It's working fine :D
 
8:53 PM
BTW, did you consider using a Map<String,Shape>, which is another way of "associating a (string) name with a Shape"? You kept asking for an ArrayList, so I never mentioned that, and a Map doesn't let you access entries by an index value - you'd need to know the "name".
 
Infact, i'm working on a project where i have to convert my Shapes into tables. (ER Diagram to tables). So, i draw my shapes, it sends me a pop up message to name the shape. The shape and its name is saved into that ArrayList <NamedShape> it will then check for collision with other shapes, if there are any collisions it retrieves the name of the shape (based on their index). and it will say "Line 1 is linked to Student" "Line1 is linked to ID" . For now, I've been able to do everything that I described above!
I'm now at a stage where i have to make java understand that if a line of the same index has a collision with 2 shapes(if line(1) is linked to 2 shapes of different indexes) then eventually "Student" is linked to "ID". Only after that, i will be able to make the conversion.
 
Could you provide an example in simple terms I might understand? "line"? "Student"? "ID"? I do know what these words mean, but the scenario you describe isn't clear to me.
 
Please see the above last picture that I've posted. May be this will help you to better understand.
What i want is that i gives me "Student is linked to ID"
 
The concept of a "line" connecting a "rectangle" (associated with "Student") and an "ellipse" (associated with "ID") is something that isn't expressed with an ArrayList<NamedShape>, as this merely collects unconnected Shape objects with some String (the "name") inside. If you need to represent connected Shape objects (named or not), a different data structure may be required. An ArrayList merely collects NamedShapes, i.e., shapes with a String to be displayed inside.
 
Do you have any idea of how i can rectify this? I mean what data structure you think will be correct? I've already done more than 75% of the work using ArrayList. For now everything is working fine, i just need to know how i can get this "Student is linked to ID". If I decide to change the data structure i will have to start everything again :(
 
8:53 PM
This depends on the way the linkage is defined and entered into the system. It also depends on what can be connected: one shape to one other shape, one shape to several other shapes, some shapes to several other shapes. What is it? 1:1, 1:n, m:n?
 
Please see last picture above.
For now am concentrating on a 1:1 relationship like on the picture, one shape connected to one other shape.
 
You can define a new object "Connection" containing two NamedShape objects. It is also possible to add another field to NamedShape, indicating to which other NamedShape this is linked. It depends what is more convenient for storing and using this information. For instance: how do you draw that connection?
 
am here
but for now it can be that a shape is connected to only one shape but later on i will have m:m too where many shapes will be connected.
are you there?
 
9:10 PM
Then defining a Connection object is the way to go. Same pattern as NamedShape: a class with two fields, but now both are NamedShape.
 
you mean, ArrayList<Connection> con = new ArrayList<Connection>(); with two fileds Name, Shape?
fields**
 
No, two fields both being NamedShape - you are connecting two shapes with names in them.
 
Ok and how do i proceed to get for e.g "Student" is connected to "ID" using that new object "Connection"?
 
Of course, if all NamedShapes have a unique (!) name, the connection could be described by the two names. (This is not the right way to do it. If you need to design data structures, you must know all. As this is developing, it was just a name and a shape in a list; now it is connections between named shapes... All bets are off - you may have to redesign everything if some other property turns up.)
To create a Connection object, you might say new Connection( shapes.get(0), shapes.get(1) ) if these two are connected.
 
If you have to change the data structure, what will you choose?
what data structure you will choose to do this?
 
9:29 PM
I don't know enough to answer this. There is, most likely, some model behind these rectangles and ellipses. I guess it would be a graph with nodes (the NamedShape objects) and arcs (edges). Have a collection of nodes and arcs. Edges might be directed. Nodes would have properties describing them what they represent - a key, a record and how it is called: not by shape and string: this is representation.
It seems that you are planning to create a graphical representation of a data base data model.
This is not trivial.
It's bed time for me. I hope I haven't scared you too much ;-)
 
To create this database data model, i will write an sql script
based on the ER Diagram
 
Sounds reasonable - you can access the DB definition.
 
No not that much scared
 
But: it is not trivial.
 
;)
but before writing this sql script, i need to convert the entity relationship diagram first
at my place it's 1:36 a.m
am not feeling so sleepy.
Can you just tell me before you go if i can still proceed with the arraylist?
if you think there is hope?
 
9:37 PM
Time to go to bed for you, too. We can chat some more tomorrow, but I'll be AFK until about 19 hrs from now which would be 9pm your time.
 
Fine for me.
 
You can use that ArrayList as the list of nodes of the graph. You may have to add another field to the NamedShape.
A unique identification across the DB. Perhaps add a Map to locate the nodes by that unique id.
 
Thank you.
I'll think
 
So: good night, cu
 
about everything you told me.
We'll continue the chat tomorrow. Thank you for your concern. It really helps. Good night
Take care.
 

  last day (34 days later) »