Reputation: 95
I am trying to create a custom Label
. I want to do something like markups on an Image
depending on what the user enters. I really don't know how to do it but I hope that you will know what I am trying to achieve here. What is the proper way to derive the Label
class? This is my code.
class CustomLabel extends Label
{
List paths;
Image image;
public CustomLabel(Image img,List paths)
{
this.image = img;
this.paths = paths;
}
public void paint(Graphics g)
{
g.setColor(0x000000);
for (int i = 0; i < paths.size(); i++)
{
Path path = (Path)paths.getModel().getItemAt(i);
int firstLocX = path.discoveredNode.getX();
int firstLocY = path.discoveredNode.getY();
int secondLocX = path.nodeDiscovered.getX();
int secondLocY = path.nodeDiscovered.getY();
g.drawLine(firstLocX, firstLocY, secondLocX, secondLocY);
}
g.drawImage(image, 0, 0);
UIManager.getInstance().getLookAndFeel().drawLabel(g, this);
}
}
I hope you can help me with this.
Thanks,
Upvotes: 0
Views: 516
Reputation: 52770
You shouldn't use UIManager.getInstance().getLookAndFeel().drawLabel(g, this);
Since you want your drawing to appear on top you want the label to draw first and then have your code so the first line in the paint
method should be super.paint(g)
which will take care of that.
Your call to drawImage draws it at 0,0 which is always wrong. You should position drawing based on getX()
and getY()
.
Having said that I don't see anything that warrants subclassing. If you just want to draw an emblem why not create a style that has an aligned image in its background with the given emblem. Then just conditionally assign the right UIID to the label.
Upvotes: 4