2013-01-11 85 views
-1

我的类中的函数在测试后工作。 ColorDrop会创建指定颜色的下降滴。指定速度的SpeedDrop等等。 我想把我的滴放在一个列表中,在GUI中批量生成它们。 Drop是超类,ColorDrop和SpeedDrop是扩展超类的子类。 代码编译,但GUI是空白的。我是不是把我的数组列表错了?还是我不正确地调用该列表中的对象的方法?类型超类的Arraylist可容纳不同的子类型

package advancedobject; 

import java.awt.Color; 
import java.awt.Graphics2D; 
import java.util.ArrayList; 

public class MyGooDrop extends Goo { 

    Drop testDrop; 
    Drop colorDrop; 
    Drop fastDrop; 
    Drop wavyDrop; 
    int random = (int) Math.random()*width; 
    ArrayList<Drop> drops; 
    public MyGooDrop() 
    { 

     testDrop = new Drop(width/2, -10, 10); 
     colorDrop = new ColorDrop(width/3, -10, 10, Color.BLUE); 
     fastDrop = new SpeedDrop ((width * 3/4), -10, 10, 5); 
     wavyDrop = new WavyDrop (-10, height/2, 10); 
     drops = new ArrayList<Drop>(); 
     fillDropList(); 
    } 

     public void fillDropList() 
    { 
     for(int i = 0; i<= 12; i++) 
     { 
      if (i <= 4) 
      drops.add(i, new Drop ((int) Math.random()*width, -10, 10)); 
      else if (i>4 && i<=8) 
      drops.add(i, new ColorDrop ((int) Math.random()*width, -10, 10, Color.BLUE)); //drops.get(i).randomPainter() 
      else 
      drops.add(i, new SpeedDrop ((int) Math.random()*width, -10, 10, (int) Math.random()*10)); 
     } 
    } 

    public void draw(Graphics2D g) { 

     // Fill background 
     g.setColor(Color.GRAY); 
     g.fillRect(0, 0, width, height); 

     testDrop.draw(g); 
     colorDrop.draw(g); 
     fastDrop.draw(g); 
     wavyDrop.draw(g); 
     for(int i = 0; i<=12; i++) 
     drops.get(i).draw(g); 
    } 

    public void update(){ 

     testDrop.move(width, height); 
     colorDrop.move(width, height); 
     fastDrop.move(width, height); 
     wavyDrop.move(width, height); 
     for(int i = 0; i<=12; i++) 
     drops.get(i).move(width, height); 
    } 

    public static void main(String[] args) { 

     MyGooDrop tester = new MyGooDrop(); 
     tester.go(); 

    } 
} 
+0

添加到列表时,您不需要强制转换为“拖放”,因为您添加的所有项都继承它 – fge

+0

如果没有强制转换,它不起作用:\ –

+0

转换为“拖放”是完全不必要的。 – Yuushi

回答

0

这条线:

drops.add(new ColorDrop ((int) Math.random()*width, -10, 10, drops.get(i).randomPainter())); 

你的努力,你正试图将其添加到(我)从相同位置获得一个对象,并在此呼吁randomPainter()(还是空的对象)将导致NPE。

正如评论中提到的那样,检查您的for循环更新并绘制i <= 12的方法,这应该是i < 12或更好,i < drops.size()。这些将会导致ArrayIndexOutOfBounds错误。

+0

我纠正了for循环,目前正在修复我的。它现在绘画,但他们没有正确移动我会更新我的代码在帖子中显示我的添加现在包括索引 –

+0

需要更多的信息。他们如何不正确地移动?他们在做什么?发布你的移动方法也会有所帮助,但是如果你的测试失败了,除了你用不同的参数构建它们之外,似乎没有什么原因会导致你的循环无法工作。 – AerusDar

+0

我一直在测试我的代码,它似乎fillDropList函数被调用,但没有添加宿主?这是奇怪的,因为我没有得到一个空指针,但是当我把一个System.out.println(“我做滴!”)测试线下的任何添加没有什么是打印.. –

0

好的,让我们一点一点地看一些东西。看到你正在使用AWT与Graphics2D,我会假设你正在使用JPanel或类似的东西。如果是这样的话,那么我也要去承担你的

public void draw(Graphics2D g) { 
    ... 
} 

是在某些时候某些

@Override 
public void paint(Graphics g) { 
    draw((Graphics2D)g); 
} 

在你的咕类的地方被调用。另外,我假设你的更新方法在一个线程中被重复调用。有一两件事,它的缺少可能会解决未更新将添加repaint()呼叫,像这样

public void update(){ 

    testDrop.move(width, height); 
    colorDrop.move(width, height); 
    fastDrop.move(width, height); 
    wavyDrop.move(width, height); 
    for(int i = 0; i<=12; i++) 
     drops.get(i).move(width, height); 
    //updates the GUI 
    repaint(); 
} 

至于你的for循环的GUI,因为你使用一个ArrayList,而不是一个数组,不在整个代码中使用硬编码的值,因为这会破坏使用List的目的。

/** 
* Adds a bunch of new drops to the drop list, a third of each type 
* @param numOfDrops - the amount of drops to add to the list 
*/ 
public void fillDropList (int numOfDrops) 
{ 
    int oneThird = numOfDrops/3; 
    int twoThirds = 2*numOfDrops; 
    for(int i = 0; i<= numOfDrops; i++) 
    { 
     if (i <= oneThird) 
     { 
      drops.add(new Drop ((int)(Math.random()*width), -10, 10)); 
     } 
     else if (i > oneThird && i <= twoThirds) 
     { 
      drops.add(new ColorDrop ((int)(Math.random()*width), -10, 10, Color.BLUE)); 
     } 
     else 
     { 
      drops.add(new SpeedDrop ((int)(Math.random()*width), -10, 10, (int)(Math.random()*10))); 
     } 
    } 
} 

到处你有一个for循环通过滴迭代,使用

for (int i = 0; i < drops.size(); i++) 

唯一的其他东西我可以建议是考虑为不同类型的跌落的战略格局,并观看你的括号。