2014-03-29 33 views
-2

我試圖創建一個既有文本又有圖形版本的小圖形遊戲。對於這兩個版本,我使用了一個render()方法,將所有東西都繪製到板子上(在這種情況下,會將其繪製到JFrame中)。但是,我在嘗試將方法添加到JFrame時遇到問題。我想知道如何做到這一點,如果我不能做我還能做什麼。我如何實現render()方法到我的JFrame中

// Rows 
    for (int x = 0; x < this.cells.length; x++) { 

     // Columns 
     for (int y = 0; y < this.cells[x].length; y++) { 
      if (this.cells[x][y].type == Cell.EMPTY) { 
       g.setColor(Color.black); 
       g.drawOval(x, y, 10, 10); 
      } else if (this.cells[x][y].type == Cell.MONSTER) { 
       g.setColor(Color.red); 
       Polygon poly = new Polygon(); 
       poly.addPoint(x, y); 
       poly.addPoint(x - 10, y - 10); 
       poly.addPoint(x + 10, y + 10); 
       g.drawPolygon(poly); 

      } 

這是渲染方法,其通過搜索單元陣列和基於對象的在其x和y的類型,將以此爲根據的形狀。雖然,我對編程世界比較陌生,但我會很感激所有我能得到的幫助!

+2

http://stackoverflow.com/questions/2134840/drawing-on-jframe – BitNinja

+0

爲了更好地幫助越早,張貼[MCVE(http://stackoverflow.com/help/mcve)(最小完整和可驗證示例)。 –

回答

2
  1. 不要畫到框架。相反,請畫一個JPanel
  2. 要做定製繪畫,@OverridepaintComponent(Graphics)方法。
    • 請務必在渲染前先撥打super.paintComponent(g);
  3. @Override面板的首選尺寸返回一個尺寸,將顯示整個圖形。
  4. 實現用於重複任務的搖擺Timer(例如動畫)
    • 在聽衆,調整與再現位置或其它因素,則調用repaint();
相關問題