2012-04-10 274 views
0

我正在尝试创建自定义Label。我想根据用户输入的内容,在Image 上做标记。我真的不知道该怎么做,但我希望你能知道我在这里想达到的目标。什么是派生类Label的正确方法?这是我的代码。LWUIT标签:创建自定义标签

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); 
    } 
} 

我希望你能帮助我。

感谢,

回答

4

因为你希望你的绘图出现在你想要的标签绘制第一,然后让你的代码顶部,则不应使用UIManager.getInstance().getLookAndFeel().drawLabel(g, this);

所以在paint方法中的第一行应是super.paint(g)这将照顾到这一点。

您对drawImage的调用将它绘制为0,0,这总是错误的。您应该根据getX()getY()来定位图纸。

话虽如此,我没有看到任何担保子类。如果你只是想画一个徽章,为什么不创建一个样式,在其背景中与给定的标志对齐的图像。然后只是有条件地将正确的UIID分配给标签。