9 Replies - 4679 Views - Last Post: 28 May 2010 - 07:40 AM Rate Topic: -----

#1 snowboarder72992   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 09

Casting "Class" (Gridworld Modifications)

Posted 07 March 2010 - 07:42 AM

ok, so long story short, i'm trying to make a version of pokémon with gridworld. i would like to be able to change an object's displayed image on the fly, but the way it is currently implemented is to grab the gif with the same filename as the class. since the function that processes images deals with multiple class types, it uses the generic "class" as a parameter.

i have one question, but worded two different ways so people who know a lot about gridworld might understand and or someone who is knowledgeable in Java could help also

is there a way to cast "Class" to the original class which was sent to the function?

or

is there a way to be able to programatically change an object's (for example Actor) image?


***From info.gridworld.gui
 /**
     * Constructs an object that knows how to display an image. Looks for the
     * named file first in the jar file, then in the current directory.
     * @param imageFilename name of file containing image
     */
    public ImageDisplay(Class cl) throws IOException
    {
        this.cl = cl;
        imageFilename = cl.getName().replace('.', '/');
        URL url = cl.getClassLoader().getResource(imageFilename + imageExtension);
        if (url == null)
            throw new FileNotFoundException(imageFilename + imageExtension + " not found.");
        tintedVersions.put("", ImageIO.read(url));
    }




ps: i did try to cast it like normal and it didn't work.

Is This A Good Question/Topic? 0
  • +

Replies To: Casting "Class" (Gridworld Modifications)

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Casting "Class" (Gridworld Modifications)

Posted 07 March 2010 - 09:14 AM

Actually, I guess you could subclass Actor, and override the ImageDisplay(Class c) method. However, much of GridWorld is unfortunately completely undocumented and is difficult to exactly establish how to do such things with it. This is one of those difficult things...
Was This Post Helpful? 0
  • +
  • -

#3 snowboarder72992   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 09

Re: Casting "Class" (Gridworld Modifications)

Posted 07 March 2010 - 09:27 AM

View PostDogstopper, on 07 March 2010 - 08:14 AM, said:

Actually, I guess you could subclass Actor, and override the ImageDisplay(Class c) method. However, much of GridWorld is unfortunately completely undocumented and is difficult to exactly establish how to do such things with it. This is one of those difficult things...

Thanks for the help. One question though, what do you mean by subclassing Actor? I assume that u mean the "asSubClass" function in Class, but it's all a little bit shady to me. could you explain what you meant some more?
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Casting "Class" (Gridworld Modifications)

Posted 07 March 2010 - 09:33 AM

Yes, subclassing is a fundamental part of Object Oriented Programming. It allows you to make a class with all the methods and attributes of the "parent" class, but also allows you to override or make new methods separately of it.

So this is how you subclass Actor:
class MyActor extends Actor {
    
    public ImageDisplay(Class c) {
        // Write whatever you want to ovverride
    }
}



Now MyActor is an Actor, only with a changed constructor...Also, I don't think that you should override constructors or helper methods, especially if it is undocumented.


For more information on OOP subclassing and inheritance, look at these links.
Overview: http://java.sun.com/...nheritance.html

Trail: http://java.sun.com/...subclasses.html

This post has been edited by Dogstopper: 07 March 2010 - 11:44 AM
Reason for edit:: Stupidity...please ignore that part.

Was This Post Helpful? 0
  • +
  • -

#5 snowboarder72992   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 09

Re: Casting "Class" (Gridworld Modifications)

Posted 07 March 2010 - 09:54 AM

I understand what you mean, but Actor does not inherit from ImageDisplay so it isn't possible for it to be overridden. that would be nice though.

This post has been edited by snowboarder72992: 07 March 2010 - 11:10 AM

Was This Post Helpful? 0
  • +
  • -

#6 snowboarder72992   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 09

Re: Casting "Class" (Gridworld Modifications)

Posted 07 March 2010 - 11:34 AM

Nevermind, I figured out how to do it. Thanks for all of your help though.
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Casting "Class" (Gridworld Modifications)

Posted 07 March 2010 - 11:43 AM

View Postsnowboarder72992, on 07 March 2010 - 11:54 AM, said:

I understand what you mean, but Actor does not inherit from ImageDisplay so it isn't possible for it to be overridden. that would be nice though.


Absolutely right...my bad...
Was This Post Helpful? 0
  • +
  • -

#8 snowboarder72992   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 09

Re: Casting "Class" (Gridworld Modifications)

Posted 08 March 2010 - 01:16 PM

View PostDogstopper, on 07 March 2010 - 10:43 AM, said:

Absolutely right...my bad...

It's alright, it happens to the best of us lolz.
Was This Post Helpful? 0
  • +
  • -

#9 Guest_shaffer*


Reputation:

Re: Casting "Class" (Gridworld Modifications)

Posted 28 May 2010 - 07:00 AM

How did you figure out how to change the image? Does it have anything to do with overriding the ImageDisplay class?
Was This Post Helpful? 0

#10 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Casting "Class" (Gridworld Modifications)

Posted 28 May 2010 - 07:40 AM

If you have a 48x48 GIF file named the same thing as the class in the same directory as the class, it should be changed automatically.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1