gpt4 book ai didi

java - 我的 boolean 方法java

转载 作者:行者123 更新时间:2023-12-04 05:06:38 24 4
gpt4 key购买 nike

我已经做了很多寻找这个问题的答案,并进行了一些编辑,但我无法让它工作。我认为我的 boolean 方法 getColor() 有问题。我试图在来自不同类的循环中使用它。

这是我的 getColor 方法,来自 AvatarPanel 类

    public boolean setColor(boolean good)
{

if (good == true)
what--;
avatars.get(0).setBackground(colors.get(what));
repaint();
if (good == false)
what++;
avatars.get(0).setBackground(colors.get(what));
repaint();

return good;
}

这是我尝试在其中使用 getColor 的 Background 类中的 attackPerformed 方法。我不确定我是否写错了其中一种方法,或者attackPerformed 是否需要监听器。游戏仍然正常运行并且运行良好。我只是想得到一些帮助,以确保这些至少写得正确。

我决定显示后台构造函数可能是一个好主意,因为我认为它是我需要调用attackPerformed 的地方。我不知道在哪里需要调用attackPerformed,尽管我很确定它会在后台构造函数中。 attackPerformed 方法如下所示,构造函数在顶部。
public class Background extends JPanel implements Constants, ActionListener
{
private static final long serialVersionUID = 1L;

private final Color BACK_COLOR = Color.GRAY;

private ArrayList<Sprite> sprites;

private Avatar avatar;

/** Constructor */
public Background()
{

sprites = new ArrayList<Sprite>();
reset(0);
setBackground(BACK_COLOR);


KeyboardFocusManager manager =KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new KeyEventDispatcher()
{
public boolean dispatchKeyEvent(KeyEvent event)
{
int x = 0, y = 0;
double angle = 0;
Point p = avatar.getPosition();

switch (event.getKeyCode())
{
case KeyEvent.VK_DOWN:
y=6;
angle=270;
break;

case KeyEvent.VK_UP:
y = -6;
angle = 90;
break;

case KeyEvent.VK_LEFT:
x = -6;
break;

case KeyEvent.VK_RIGHT:
x = 6;
angle = 180;
break;
}

avatar.setParameters();
p.x += x;
p.y += y;
avatar.setPosition(p);
avatar.setAngle(angle);

if (!getBounds().contains(avatar.getBounds()))
{
avatar.restore();
Toolkit.getDefaultToolkit().beep();
return true;
}
repaint();
return true;
}



});
Timer timer = new Timer(15, this);
timer.start();



} //end of constructor



public void attackPerformed()
{
for(Sprite s : sprites)
{
AvatarPanel panel = new AvatarPanel();
Rectangle r = new Rectangle(sprites.get(0).getBounds());
Rectangle p = new Rectangle(sprites.get(1).getBounds());

if(r.intersects(p))
{
panel.setColor(false);
}
}
}


public void actionPerformed(ActionEvent e)
{
for (Sprite s : sprites)
{
if (s instanceof Move)
{
s.setParameters();
for (int i=0; i<3; i++)
{
((Move)s).nextPosition();
if (!getBounds().contains(s.getBounds()))
{
((Move) s).moveFailed();
}
else break;
}
}
}
repaint();
}




public @Override void paintComponent(Graphics g)
{
super.paintComponent(g); /* Let the parent class do its painting */

for (Sprite sprite: sprites)
{
sprite.draw(g);
}
}

public void reset(int level)
{
Sprite sprite = null;

int x, y, angle;
double scale, min, max;

sprites.clear();
int[] figures = FIGURES[level];
for (int f=0; f<figures.length; f++)
{
for (int i=0; i<figures[f]; i++)
{
x = (int)(Math.random()*600 + 100);
y = (int)(Math.random()*400 + 100);

switch (f)
{
case 0:
sprite = avatar = new Avatar(x,y);
break;
case 1:

sprite = new Predator(x,y);
break;
case 2:
sprite = new Mine(x,y);
break;
case 3:
sprite = new Shield(x,y);
break;
}

angle = (int)(Math.random()*360);
sprite.setAngle(angle);

min = sprite.getMinScaleFactor();
max = sprite.getMaxScaleFactor();
scale = Math.random() * (max - min) + min;
sprite.setScale(scale);

sprites.add(sprite);
repaint();
}
}
}

}

最佳答案

看起来您的 if 和 else 情况非常相似。
您可以将其简化为这个。

public boolean setColor(boolean good)
{
what+=good?-1:+1;
avatars.get(0).setBackground(colors.get(what));
repaint();
    return good;
}

关于java - 我的 boolean 方法java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15453476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com