Drawing a custom component

﹥>﹥吖頭↗ 提交于 2020-01-15 05:33:08

问题


This is kind of 2 questions:
1) Is the overriding the paint member of a Component the best approach for a simple 2D graphics game using codename one? Or should I not even be attempting it??
2) Why does the code below draw my Component only to erase it instantly?

I initialise my component like this:

protected void StartGame()
{
    final Component newC = new PaintedComponent();

    Container mv = findContainerMainVisual();

    mv.addComponent(newC);
    mv.setShouldCalcPreferredSize(true);
    mv.animateLayout(200);
}

And then the component overrides the paint member function like this:

public class PaintedComponent extends Component {
    private int nextColour;

    public PaintedComponent() {
        super();
        setSize(new Dimension(200,200));
        nextColour = 0x8f8f8f;
    }


    @Override
    public void paint(Graphics g) {
        super.paint(g); // I've tried without this, but it's the same
        g.setColor(0xffffff);
        g.fillRadialGradient(0xffffff, nextColour, 0,0,this.getWidth(), this.getHeight());
    }
}

回答1:


Check out the Poker demo in Codename One which you can see in the latest batch of demos, it takes a very different approach although overriding paint should work just fine too.

The reason the component isn't showing is because its sized to 0 when the animateLayout() method validates the screen. You should either place it in the center of a border layout (to take up all available space and disable scrolling) or override calcPreferredSize() to return a sensible size.

I would suggest avoiding radial gradients since they are REALLY slow on some platforms. See this about performance.



来源:https://stackoverflow.com/questions/21239559/drawing-a-custom-component

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!