Linked List of Class Instances
Howdy
I am new to java and I think the project I chose to teach myself was maybe a little on the ambitious side.
In essence I have 2 classes, say, Class A and Class B that are defined at the same level (i hope that makes sense)
Class A contains a linked list of instances of Class B.
Some behaviours defined in Class B need to effect or be effected by the state of the other instances of Class B in the LinkedList
An instance of Class B may receive an event that means it needs to remove it self from the list, or change the position relative to the other Class B instances .
How do i reference the other members of the linked list, How do i reference anything in the instance of Class A which contains the instance of Class B recieving the mouse event?
Any ideas?
A brief explanation of what I am trying to achieve follows.
You can think of the main form as being, say, a drafting table.
Conceptually I want to be able to add, remove, move and resize 'sheets of paper' on the drafting table and on each sheet of paper I want to be able to add, remove, move and resize shapes. These shapes could be to represent different elements of a flow chart, or electrical components in an electronic circuit, etc. The shapes should not be allowed to overlap.
For the 'sheets of paper' I have a class based on a JPanel, that handles its own resizing etc. The 'shapes' are also a class based on JPanel that handle their own resizing.
The main form or (drafting table) has a linked list of instances of 'sheet' class and each instance of 'sheet' class has a linked list of instances of 'shape' class.
Now when I want to add a shape, the instance of the 'sheet' class handles the mouse click and runs through its linked list of 'shape' class instances to see if this is a valid location, ie is does not overlap another 'shape'. If it is a valid location it adds it to the linked list.
What I am having trouble with is when i resize or move a 'shape'. the mouse event is handled in the 'shapes' class but I don not know how to check the other 'shape' class instances to see if the new location or size is valid.
thanks in advance for any help,
Shade
Re: Linked List of Class Instances
This is just a suggestion to the development of the two classes. If the two classes are related, have you considered using Inheritance in your projects? Perhaps you should try that and make your work a little easier.
Re: Linked List of Class Instances
Quote:
Originally Posted by
elisha.java
This is just a suggestion to the development of the two classes. If the two classes are related, have you considered using Inheritance in your projects? Perhaps you should try that and make your work a little easier.
Inheritance is not always the solution if your classes are related.
Quote:
Class A contains a linked list of instances of Class B.
Clarify it? What do you mean by instances of Class B?
@Shade: In order to get help, you need to provide SSCCE
Re: Linked List of Class Instances
Thanks,
I didn't provide the source code as I'm was necessarily looking for someone to fix my problem for me, just wanted to know if what i am trying to do is possible.
Elisha, I'll see if this is possible and/or does what it needs.. not sure if the classes are similar enough at this point
So here is some skeleton code of what I am trying to achieve, I'm just interested in the concept of how this might be possible at the moment
'-----------------------------------------------------
DraftingTable Class Code
package DraftingTable;
public class DemoClassDraftingTable {
public LinkedList<DemoClassSheet> DraftingSheets = new LinkedList<DemoClassSheet>();
}
'------------------------------------------------------
Code for the Class for the 'Sheets' that can be on the drafting table
package DraftingTable ;
public class DemoClassSheet {
public LinkedList<DemoClassShape> PlacedShapes = new LinkedList<DemoClassShape>();
}
'------------------------------------------------------
Code for the Class for the 'Shapes' that can be on the drafting table
package DraftingTable ;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;
import java.awt.event.MouseEvent;
public class DemoClassShape {
public DemoClassShape() {
//stuff
}
MouseInputListener resizeListener = new MouseInputAdapter() {
public void mouseDragged(MouseEvent me) {
/*
Problem is here
The code here handles the resizing of the shape (by clicking and dragging the mouse)
When changing the size of a shape, i need to know where the other shapes on this sheet are, ie I need to run through the other shapes that are in the linkedlist of which this is part.
How do I do this?
*/
}
};
}
Re: Linked List of Class Instances
When you create the DemoClassShape object pass it a reference to a class with a method that can test if the current shape is overlapping any previous shapes.
The DemoClassShape class would call the tester method with a reference to itself. The tester method would have the list of current shapes it could look through and return true or false.
Re: Linked List of Class Instances
Thanks Norm,
your suggestion is along the lines of what i was hoping to be able to do. The DemoClassSheet has code in it that tests for overlap when you originally drop the 'shape' on the 'sheet'. It has a default size, if that doesn't fit it will reduce the size of the shape until it does fit or it is smaller than a minimum size. I was intending to use a version of that code in DemoClassShape and my stumbling block was how do I refer back to the instance of the DemoClassSheet that the 'shape' is in.
Your suggestion to have the overlap test as method of another class makes sense.
I've put that test in the DemoClassSheet and in essence that has got it working. Some odd behaviour but that is my problem to sort out.
Thank you very much!