Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Linked List of Class Instances

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default 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.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Linked List of Class Instances

    Quote Originally Posted by elisha.java View Post
    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.

    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

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

    */

    }
    };

    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,921
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default 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.

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

Similar Threads

  1. [SOLVED] Storing instances of a Sub Class in it's Super Class (Which is an array)
    By Hiroto in forum Object Oriented Programming
    Replies: 5
    Last Post: November 6th, 2011, 10:36 AM
  2. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 04:20 PM
  3. Multiple class instances ??? But how ???
    By dumb_terminal in forum Object Oriented Programming
    Replies: 6
    Last Post: December 2nd, 2010, 08:42 AM
  4. Replies: 0
    Last Post: December 1st, 2010, 06:10 AM
  5. Multiple instances of linked list
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 11th, 2010, 08:48 PM

Tags for this Thread