【问题标题】:Casting random color in a Color array在颜色数组中投射随机颜色
【发布时间】:2015-04-09 17:21:34
【问题描述】:

作为程序的一部分,我需要在颜色数组中存储随机颜色。基本上,我创建了一个应该返回一个新的随机颜色的方法,我已经测试过了,它可以工作。我遇到的问题是如何在另一个类和方法中调用它。我尝试了很多东西,但没有一个有效。我在下面粘贴相关代码,顶部是颜色数组的实例化。

static Color[] colors = new Color [10000];

public static Color randomColor() {
    return new Color(generator.nextInt(256), generator.nextInt(256), generator.nextInt(256));
}


private class DotsListener implements MouseListener, MouseMotionListener, ActionListener
{

    public void mousePressed(MouseEvent event)
        {
            if(SwingUtilities.isLeftMouseButton(event))
                {
                    pointList.add(event.getPoint());
                    sizes[pointList.size()] = SIZE;
                    colors[pointList.size()]= new Color(event.randomColor());

【问题讨论】:

  • 将其作为参数传递?不确定您的问题到底是什么。

标签: java arrays oop colors casting


【解决方案1】:

你可以直接调用方法;您不需要创建第二种新颜色。

colors[pointList.size()] = randomColor();

如果您的 randomColor() 方法位于不同的类中,请将类名放在方法调用之前。例如,如果你有一个 ColorUtil 类,你可以这样称呼它:

colors[pointList.size()] = ColorUtil.randomColor();

【讨论】:

    【解决方案2】:

    randomColor() 属于哪个类?因为它是一个静态方法,你只需要调用ClassName.randomColor();

    【讨论】:

      猜你喜欢
      • 2013-02-03
      • 2018-09-20
      • 2018-03-18
      • 2016-02-28
      • 1970-01-01
      • 2015-01-11
      • 2014-12-26
      • 2017-09-08
      相关资源
      最近更新 更多