Issue
I have a player class which extends a JLabel
public class Player extends JLabel
{
public Player(int x,int y,int width,int height) throws IOException
{
this.setBounds(x, y, width, height);
ImageIcon icon = new ImageIcon(ImageIO.read(new File("Resources/Player.png")));
this.setIcon(icon);
}
}
and I wanna override an Update method from a parent class to implement physics for the player but I have to extend the parent class which I cant do cause I already extended the JLabel, any help will be appreciated.
Solution
The short answer is probably no. You can/have to override methods that are declared in an implemented Interface or super class. So if the method you want to override is not declared in the JLabel class or a super class of JLabel, you will not be able to override (because you can't override what is not even written).
If you just want to use the icon functionality of JLabel, this would be a better approach than directly extending from it:
public class Player {
private ImageIcon playerIcon = new ImageIcon("PATH TO ICON");
private JLabel icon = new JLabel(playerIcon);
public Player() {
// DO YOUR STUFF HERE
}
public void setPlayerPosition(int x, int y, int width, int height) {
icon.setBounds(x, y, width, height);
}
public Point getPlayerPosition() {
return icon.getLocation();
}
}
I don't recommend using a NULL Layout and placing the components directly on the container, but this is a different topic
Because of the lack of information about what you exactly want to achieve, it is hard to give you advice how to achieve it. Specify your question, provide more code and information about the structure.
Answered By - John
Answer Checked By - David Marino (JavaFixing Volunteer)