问题描述
这是2个问题:
1)是组件$的重载
paint
成员吗?使用代号 1的简单2D图形游戏的最佳方法?还是我什至不应该尝试呢?
2)为什么下面的代码绘制我的 Component
只是为了立即擦除它?
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?
我像这样初始化组件:
protected void StartGame()
{
final Component newC = new PaintedComponent();
Container mv = findContainerMainVisual();
mv.addComponent(newC);
mv.setShouldCalcPreferredSize(true);
mv.animateLayout(200);
}
然后该组件将覆盖 paint
成员函数如下:
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());
}
}
推荐答案
检查在中您可以在最新一批的演示中看到代号 Codename One,它采用了一种非常不同的方法,尽管覆盖油漆也可以正常工作。
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.
该组件不起作用的原因之所以显示,是因为当animateLayout()方法验证屏幕时,其大小为0。您应该将其放置在边框布局的中心(占用所有可用空间并禁用滚动),或者覆盖calcPreferredSize()以返回合理的大小。
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建议避免使用径向梯度,因为在某些平台上,它们的速度确实很慢。参见关于效果。
I would suggest avoiding radial gradients since they are REALLY slow on some platforms. See this about performance.
这篇关于绘制自定义组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!