2014-01-20 159 views
1

這是2個問題的種類:
1)的paint成員是否使用代號1代替最簡單的2D圖形遊戲?或者我應該甚至不嘗試呢?
2)爲什麼下面的代碼只能將我的Component僅僅立即擦除?繪製自定義組件

我初始化我的組件是這樣的:

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

    Container mv = findContainerMainVisual(); 

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

,然後針對組件覆蓋這樣的paint成員函數:

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

查覈在代號一位Poker demo,你可以在看最新一批的演示,它採取了一種非常不同的方法,雖然壓倒一切的油漆應該工作得很好。

組件未顯示的原因是因爲當animateLayout()方法驗證屏幕時,其大小爲0。您應該將其放置在邊界佈局的中心(以佔用所有可用空間並禁用滾動)或覆蓋calcPreferredSize()以返回合理的大小。

我建議避免徑向漸變,因爲它們在某些平臺上真的很慢。有關性能,請參閱this

+0

謝謝。是的,我已經看過撲克演示了,但我不認爲我可以通過移動組件來實現我想要的工作 - 雖然很酷的想法:) 如果我想顯示一個圍繞另一個圍繞另一個環繞的物體,你認爲我可以我使用「撲克技術」來做到這一點? – noelicus

+0

此外,覆蓋'calcPreferredSize()'似乎沒有做任何事情,但'邊界佈局的中心'工程治療:) – noelicus

+0

我很難從描述中知道,但你可以創建自己的佈局管理器,並使用以任何你喜歡的方式定位組件。佈局管理器實際上很容易創建,在開發人員指南中有這樣一個例子。 –