Java 类型超类的Arraylist,用于保存各种子类类型
我的类中的函数在经过测试后仍然有效。ColorDrop创建指定颜色的下落。指定速度的减速等等。 我想把我的点滴放在一个列表中,然后在GUI中批量生产。 Drop是超类,ColorDrop和SpeedDrop是扩展超类的子类。 代码已编译,但GUI为空。我把我的arraylist组装错了吗?或者我对列表中的对象调用的方法不正确Java 类型超类的Arraylist,用于保存各种子类类型,java,object,arraylist,Java,Object,Arraylist,我的类中的函数在经过测试后仍然有效。ColorDrop创建指定颜色的下落。指定速度的减速等等。 我想把我的点滴放在一个列表中,然后在GUI中批量生产。 Drop是超类,ColorDrop和SpeedDrop是扩展超类的子类。 代码已编译,但GUI为空。我把我的arraylist组装错了吗?或者我对列表中的对象调用的方法不正确 package advancedobject; import java.awt.Color; import java.awt.Graphics2D; import
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();
}
}
打包高级对象;
导入java.awt.Color;
导入java.awt.Graphics2D;
导入java.util.ArrayList;
公共类MyGooDrop扩展了Goo{
滴试验滴;
滴色滴;
快速下降;
落波;
int random=(int)Math.random()*宽度;
ArrayList滴剂;
公共MyGooDrop()
{
testDrop=新的跌落(宽度/2,-10,10);
colorDrop=新的colorDrop(宽度/3,-10,10,颜色为蓝色);
快速降落=新的快速降落((宽度*3/4),-10,10,5);
WavHydrop=新的WavHydrop(-10,高度/2,10);
drops=newarraylist();
fillDropList();
}
公共空白填充列表()
{
对于(int i=0;i这一行:
drops.add(new ColorDrop ((int) Math.random()*width, -10, 10, drops.get(i).randomPainter()));
尝试从与尝试将对象添加到(i)相同的位置获取对象,并对该对象(仍然为空对象)调用randompainer()将导致NPE
正如在评论中提到的,检查更新中的循环并为i绘制方法好的,让我们一点一点地看一些事情。看到您正在使用AWT和Graphics2D,我将假设您正在使用JPanel或类似的东西。如果是这种情况,那么我也将假设您的
public void draw(Graphics2D g) {
...
}
在某个时刻被一些人呼叫
@Override
public void paint(Graphics g) {
draw((Graphics2D)g);
}
在你的Goo类中的某个地方。另外,我假设你的更新方法在一个线程中被重复调用。它缺少的一件事可能会修复没有被更新的GUI,那就是像这样添加一个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();
}
public void update(){
testDrop.move(宽度、高度);
颜色下降。移动(宽度、高度);
快速下降。移动(宽度、高度);
波浪移动(宽度、高度);
对于(int i=0;i在添加到列表时,您不需要强制转换为Drop
,因为您添加的所有项都会继承它,没有强制转换也无法工作:\n强制转换为Drop
完全没有必要。是否会出现异常,如IndexOutboundsException?您正在为(int i=0;i<12;i++)添加12个元素()到您的ArrayList,但您正在尝试为(int i=0;iyou在这一点上是正确的,但现在我在索引5处得到了我的界限,它试图开始添加类型子类colordrop的Drop,我如何解决这个问题?我更正了for循环,目前正在修复我的。它现在绘制,但它们没有正确移动。我将在帖子中更新我的代码,以显示我的add现在包含索引to需要更多的信息。它们是如何不正确移动的?它们在做什么?发布你的移动方法也会有所帮助,尽管如果你的测试删除有效,那么除了使用不同的参数构造循环之外,似乎没有什么原因使你的循环不起作用。我一直在测试我的代码,看起来填充调用了DropList函数,但没有添加dorp?这很奇怪,因为我没有得到null指针,但是当我放置System.out.println(“我正在制作drops!”)时任何添加下的测试行都不会打印任何内容..没关系,这是因为在for循环运行之前没有设置arraylist大小。它现在可以工作了,只是不会产生应有的DORP,但我会继续!谢谢!
/**
* 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 (int i = 0; i < drops.size(); i++)