2013-03-31 22 views
0

我將重寫下面的類來使用構造函數,並執行一些更智能的事情(而不是像方法那樣的四個生成器(),所以不用擔心這個問題替代將許多對象繪製到JPanel上

現在,我循環訪問這四個數組列表,然後將對象繪製到JPanel上。大約2-3000個對象之後,這可能會導致一些顯着的延遲。

我可以使用什麼作爲這些數組列表的替代?將某種類型的序列化轉換爲JSON/XML工作嗎?在我的專業編碼(而不是這個簡單的側面項目)中,我使用一個使用DataNucleus將模型類轉換爲SQL中的表結構的大型系統 - 我覺得這對於我的有趣的一面項目來說會是巨大的矯枉過正。思考?在JPanel類

public class Life { 
PlayPanel panel; 
public int prodSeed; 
public int herbSeed; 
public int predSeed; 
public int decoSeed; 
public ArrayList<Producer> prodStore = new ArrayList<Producer>(); 
public ArrayList<Herbivore> herbStore = new ArrayList<Herbivore>(); 
public ArrayList<Predator> predStore = new ArrayList<Predator>(); 
public ArrayList<Decomposer> decoStore = new ArrayList<Decomposer>(); 


public Life() { 
} 

public void setPanel(PlayPanel p) { 
    panel = p; 
} 
public void producers() { 
    for(int j = 0; j < prodSeed; j++) { 
     Producer p = new Producer(panel.life); 
     prodStore.add(p); 
    } 
    panel.producerPaint = true; 
} 
public void herbivores() { 
    for(int j = 0; j < herbSeed; j++) { 
     Herbivore h = new Herbivore(panel.life); 
     herbStore.add(h); 
    } 
    panel.herbivorePaint = true; 
} 
public void predators() { 
    for(int j = 0; j < predSeed; j++) { 
     Predator p = new Predator(panel.life); 
     predStore.add(p); 
    } 
    panel.predatorPaint = true; 
} 
public void decomposers() { 
    for(int j = 0; j < decoSeed; j++) { 
     Decomposer d = new Decomposer(panel.life); 
     decoStore.add(d); 
    } 
    panel.decomposerPaint = true; 
} 
public void beginAction() { 
    for (int j = 0; j < prodStore.size(); j++) { 
     prodStore.get(j).behavior();panel.repaint();   
    } 
    for (int j = 0; j < herbStore.size(); j++) { 
     herbStore.get(j).behavior();panel.repaint(); 
    } 
    for (int j = 0; j < predStore.size(); j++) { 
     predStore.get(j).behavior();panel.repaint(); 
    } 
    for (int j = 0; j < decoStore.size(); j++) { 
     decoStore.get(j).behavior();panel.repaint(); 
    } 
    //Use booleans between these for loops to not repaint an extra amount of action 

} 

可怕地痛苦的paintComponent方法:

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    if(producerPaint == true) { 
     //int j = 0; 

     while(j < life.prodStore.size()) { 
      g.setColor(new Color(19,145,14)); 
      g.fillOval(life.prodStore.get(j).getxAxis(), life.prodStore.get(j).getyAxis(), life.prodStore.get(j).getxSize(), life.prodStore.get(j).getySize()); 
      j++; 
     } 
     j=0; 
    } 
    if(herbivorePaint == true) { 
     //int j = 0; 

     while(j < life.herbStore.size()) { 
      g.setColor(new Color(228,226,158)); 
      g.fillOval(life.herbStore.get(j).getxAxis(), life.herbStore.get(j).getyAxis(), life.herbStore.get(j).getxSize(), life.herbStore.get(j).getySize()); 
      j++; 
     } 
     j=0; 
    } 
    if(predatorPaint == true) { 
     //int j = 0; 

     while(j < life.predStore.size()) { 
      g.setColor(new Color(207,87,87)); 
      g.fillOval(life.predStore.get(j).getxAxis(), life.predStore.get(j).getyAxis(), life.predStore.get(j).getxSize(), life.predStore.get(j).getySize()); 
      j++; 
     } 
     j=0; 
    } 
    if(decomposerPaint == true) { 
     //int j = 0; 

     while(j < life.decoStore.size()) { 
      g.setColor(Color.white); 
      g.fillOval(life.decoStore.get(j).getxAxis(), life.decoStore.get(j).getyAxis(), life.decoStore.get(j).getxSize(), life.decoStore.get(j).getySize()); 
      j++; 
     } 
     j=0; 
    } 
} 

作爲相關旁註:我會從使用布爾值來返工走,因爲我通過這個代碼的工作(項目一年或者兩歲,在我知道我在做什麼之前)!

回答

2

在組件上繪製很多很多項目不是問題,但它不應該發生在paintComponent的內部。你想要做的是有一個單獨的圖像(例如BufferedImage),繪製到該圖像上,然後你的paintComponent只負責將該圖像繪製到JPanel上。因此,你的用戶並沒有看到所有這些東西一一畫出來。他們只看到最終產品。

在這個想法中搜索「雙緩衝Java」以獲得很好的演示。

至於數據結構,保留4個列表是好的,但您可能希望有更多的抽象繪圖工具在所有對象之間共享。例如,除了顏色之外,您繪製它們的方式都大致相同,因此請創建一個繪圖方法,以接受作爲參數的任何子類(例如,Producer,Decomposer等)的超類(例如BiologyObject) 。讓我知道如果這沒有意義。

UPDATE:有道上繪製的JPanel

private BufferedImage image; 

public MyClass() { 
    image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_ARGB); 
} 

public void paintOntoImage() { 
    //here is where you would draw all the things that you currently have in your paintComponent method 
    Graphics2D g = (Graphics2D) image.getGraphics(); 
    g.setColor(Color.white); 
    g.fillRect(0, 0, WIDTH, HEIGHT); 

    //now you can call "g.fillOval" or any other methods on "g" 
} 

public void paintComponent(Graphics g) { 
    g.drawImage(image, 0, 0, null); 
} 
+0

好了,要回雙緩衝似乎導致我的代碼,這個優秀位:動力學模型(https://sites.google.com/site/drjohnbmatthews/kineticmodel)。我在執行它到我自己的代碼時遇到了麻煩,我想我會回頭看看它,看看我能做什麼! – Davek804

+0

這似乎是矯枉過正給我。讓我給答案添加一些代碼。請稍後再回來 – CodeGuy

+0

哦,我明白你已經做了什麼。讓我試試看! – Davek804