Results 1 to 8 of 8
  1. #1
    trader5050 is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Red face Cast object to custom class problem

    Hello,

    I have a list of graphic objects (I call them "gobs" for short) in an ArrayList. The problem is that I'm trying to iterate through the list and call draw() on each one. Unfortunately, it will only iterate as an OBJECT and I cannot get them to cast appropriately so that I may call the unique draw() function. Note: the objects are all different types, otherwise I wouldn't be using an object in the first place!

    Java Code:
            // draw each item on screen
            for (Object gob : gobs) {
    
                gob = (gob.getClass()) gob;
                gob.draw();
                
                sketcher.drawImage(gob.display, gob.x, gob.y, null);
    
            }
    Any easy way to do this? Also tried using cast() to no avail.

    Any help is greatly appreciated!

    -Jason

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    trader5050 is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    Each "gob" object contains a whole lot of variables. Alpha, draggable or not (boolean), etc... Wouldn't I lose all of those? From what I gather (I'm very new still), interfaces are only for methods?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by trader5050 View Post
    Each "gob" object contains a whole lot of variables. Alpha, draggable or not (boolean), etc... Wouldn't I lose all of those? From what I gather (I'm very new still), interfaces are only for methods?
    If a class implements an interface it loses nothing. It gains the ability for other classes to call any of the interface's methods on this class without problem.

  5. #5
    trader5050 is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    I must be doing something wrong. I generate the following interface:

    Java Code:
    interface interface_gob {
    
        public int getX();
        public int getY();
        public BufferedImage getDisplay();
        public void draw();
    
    }
    ... and add "implements interface_gob" onto my classes.

    Still, getting a "symbol not found" on gob.getDisplay(), getX(), getY() when referenced here:

    Java Code:
            // draw each item on screen
            for (Object gob : gobs) {
                
                sketcher.drawImage(gob.getDisplay(), gob.getX(), gob.getY(), null);
    
            }
    I think it's still not seeing "object gob" as a "gob" class, just an "object." Until it recognizes that it's the correct class (and has those functions), it won't work... at least, that's my theory. That's why I was trying to typecast somehow... but the graphics objects will all be different, so it has to by dynamic based on what object it's actually looking at.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    1. Class and interface names should start with an uppercase letter.

    2. Have you been taught Generics yet?
    Lesson: Generics (The Java™ Tutorials > Learning the Java Language)

    3. If the answer to 2 is "No" then are you aware of casting? You used the word in your thread title, but I don't see that you ever did cast anything.
    Inheritance (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

    db

  7. #7
    trader5050 is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    Thanks for the replies.

    1. Yes. I have a bad habit when it comes to naming conventions. I refactored all of those appropriately.

    2. I haven't. They seem interesting... but to my untrained eye it appears it's a way to limit the information passed to an object, which is against what I'd like to do here. I want many different types of objects to be contained within the list and simply cast to whatever is appropriate for use. Avoiding the casting in the first place would be great, as they're all a subclass of "Gob" which will always contain the draw() function.

    3. I tried casting numerous ways and finally (sort of) gave up by just duplicating the item and then recasting that. See below:

    Java Code:
            // draw each item on screen
            for (Object gob : gobs) {
    
                String className = gob.getClass().getSimpleName();
                Gob newGob = new Gob(); // new doesn't matter because it'll just be reassigned
    
                if (className.equals("Gob")) { newGob = (Gob) gob; }
                if (className.equals("Gob_Network")) { newGob = (Gob_Network) gob; }
                
                sketcher.drawImage(newGob.display,newGob.x,newGob.y,null);
    
            }
    It's not very elegant, in my mind, but it works and it's an easy approach.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default

    Quote Originally Posted by trader5050 View Post
    Thanks for the replies.

    1. Yes. I have a bad habit when it comes to naming conventions. I refactored all of those appropriately.
    Good to hear that...:)

    Quote Originally Posted by trader5050 View Post
    2. I haven't. They seem interesting... but to my untrained eye it appears it's a way to limit the information passed to an object, which is against what I'd like to do here. I want many different types of objects to be contained within the list and simply cast to whatever is appropriate for use. Avoiding the casting in the first place would be great, as they're all a subclass of "Gob" which will always contain the draw() function.
    Except you do want to limit them. You want to limit them to things that are Gob(s). SO why not have a List<Gob>? That will allow you to have many different types of Gobs, but they would all be Gobs.

    Quote Originally Posted by trader5050 View Post
    3. I tried casting numerous ways and finally (sort of) gave up by just duplicating the item and then recasting that. See below:

    Java Code:
            // draw each item on screen
            for (Object gob : gobs) {
    
                String className = gob.getClass().getSimpleName();
                Gob newGob = new Gob(); // new doesn't matter because it'll just be reassigned
    
                if (className.equals("Gob")) { newGob = (Gob) gob; }
                if (className.equals("Gob_Network")) { newGob = (Gob_Network) gob; }
                
                sketcher.drawImage(newGob.display,newGob.x,newGob.y,null);
    
            }
    It's not very elegant, in my mind, but it works and it's an easy approach.
    Nope, go with the generics solution. That solution there is terrible and shows a problem with your design. Any time you find yourself having to do stuff like that the odds are that your model is wrong.

Similar Threads

  1. [Ljava.lang.Object; cannot be cast to domain.Customer
    By niteangell21 in forum New To Java
    Replies: 7
    Last Post: 09-26-2010, 08:38 PM
  2. How can I cast object to Vector?
    By batya in forum AWT / Swing
    Replies: 2
    Last Post: 11-19-2009, 05:02 PM
  3. class cast exception
    By venkatallu in forum New To Java
    Replies: 2
    Last Post: 09-02-2008, 09:50 PM
  4. How to cast an Object into a specific type (Integer/String) at runtime
    By mailtogagan@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2007, 01:04 PM
  5. How can I cast Object as an int
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 11:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •