I've a class called MapTile
that extends JLabel
. What I'm doing is loading a JPanel with a lot of MapTile's in a GridLayout. I notice when I load in 100x100 MapTile's the process takes longer than loading in 100x100 JLabel's. This is my MapTile
class below.
public class MapTile extends JLabel {
private static final long serialVersionUID = 1L;
private boolean selected;
private Point location;
public MapTile(){
super();
location = new Point();
}
public void setLocation(int x, int y){
setLocation(new Point(x,y));
}
public void setLocation(Point p){
location = p;
}
public Point getLocation(){
return location;
}
public void setSelected(Boolean b){
selected = b;
if (selected){
((GridPanel)this.getParent()).addToSelection(this);
this.setBorder(BorderFactory.createLineBorder(Color.RED));
} else {
this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
this.repaint();
}
public boolean isSelected(){
return selected;
}
}
There's nothing fancy, I just overwrote the setLocation method and added a few other methods. I also added just three instance variables.
Does extending a class cause a bunch of extra weight to deal with?
I can't help but think my MapTile
class is pretty simple, yet the time between loading 100x100 MapTile's vs loading 100x100 JLabel's is hugely different. It's taking about 4-5 times longer to load MapTile's in large quantities than JLabels.
You did not just overrode the setLocation
but you create a new instance of Point
each time the setLocation(int,int)
method is called which could have a performance impact
Besides that: - have you tested that when you remove the overridden methods you no longer experience a slow-down - have you profiled the application to see where the performance loss occurs
Oh yeah, I do not think your overridden version of setLocation
is correct. I fear you must call super.setLocation
as well if you want to JLabel
to behave correctly