简体   繁体   English

LWUIT Label:创建自定义 Label

[英]LWUIT Label: Creating a Custom Label

I am trying to create a custom Label .我正在尝试创建自定义Label I want to do something like markups on an Image depending on what the user enters.我想根据用户输入的内容在Image上做一些标记之类的事情。 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?派生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,谢谢,

You shouldn't use UIManager.getInstance().getLookAndFeel().drawLabel(g, this);你不应该使用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.由于您希望绘图显示在顶部,因此您希望 label 先绘制然后再编写代码,因此paint方法中的第一行应该是super.paint(g) ,它会处理这个问题。

Your call to drawImage draws it at 0,0 which is always wrong.您对 drawImage 的调用将其绘制在 0,0 处,这总是错误的。 You should position drawing based on getX() and getY() .您应该 position 绘图基于getX()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.然后有条件地将正确的 UIID 分配给 label。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM