Extending JLabel?

I'm working on a game (as previously said in older topics) and I would like to take advantage of objects.

Currently, I use a JLabel and set its icon image to the graphic I want to display. What I would to do is create a class called 'player' or something along those lines and have it extend JLabel (or an equivalent swing object) to have the graphics displayed by default as well as modify the other methods to customize further.

The only problem is that I can't override a method unless I have the sources so I can't say for certain that I can do this.

I guess I'm asking if anyone knows how to do this or knows where to find the JLabel code?

[677 byte] By [timothyb89_a] at [2007-11-27 0:23:48]
# 1

Not really sure what you are asking. You don't need the source to extend a class. You just override a method you want to change. You give know indication of how you want to "customize" the component so we can't give you any suggestions.

If you really want to look at the JLabel source then its in a file called src.zip that comes with your jdk.

camickra at 2007-7-11 22:19:52 > top of Java-index,Desktop,Core GUI APIs...
# 2

Yeah, you can definitely extend JLabel. Chances are, your main code already extends JDialog or JFrame! :)

Here's a quick example! (*Note, if you make this an inner class, you can take off "public")

public class MovablePlayer extends JLabel {

/* Overide the default constructors */

static MovablePlayer() {

super();

}

static MovablePlayer(Object o) {

if (o instanceof Icon) super((Icon)o);

else if (o instanceof String) super(null); // Removes any text

else printConstructorError(o);

}

static MovablePlayer(Object o, int i) {

if (o instanceof Icon) super((Icon)o, i);

else if (o instanceof String) super(null, i); // Removes any text

else printConstructorError(o);

}

static MovablePlayer(String s, Icon i, int h) {

super(null, i, h);

}

static private printConstructorError(Object o) {

System.out.println("Error: Cannot construct MovablePlayer with " + o.getClass().getConicalName() + " type";

}

/* Overide the default methods */

public void setIcon(Icon i) {

super.setIcon(i);

super.setText(null); // Removes any text

}

}

-FBL

FBLa at 2007-7-11 22:19:52 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks for the code!

But there seems to be a bit of a problem with the return type for the printConstructorError method.

It seems to be saying that he method declaration is invalid as there is no return type and everything I try invalidates everything else. It seems that whatever the reason is, I can't manage to find it :P

timothyb89a at 2007-7-11 22:19:52 > top of Java-index,Desktop,Core GUI APIs...
# 4
just add void...static private void printConstructorError(...
TimRyanNZa at 2007-7-11 22:19:52 > top of Java-index,Desktop,Core GUI APIs...