【问题标题】:how can i use an array to make random circles我如何使用数组制作随机圆圈
【发布时间】:2013-12-25 08:39:50
【问题描述】:

我一直在做一项学校作业,老师希望我们在 JPanel 上出现 7 个圆圈并向下移动。一旦一个圆圈到达底部,就应该制作一个新圆圈来替换到达 JPanel 底部的圆圈。我决定使用一个数组来继续制作随机圆圈,但我无法让它正常工作。我使用了一个 for 循环来填充具有随机半径和颜色的圆的数组。代码可以编译,但是当我运行它时出现异常。我很难让阵列正常工作。我不确定的另一件事是如何绘制圆圈,以便它们在 JPanel 上留出空间。

代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Random;

public class keyExample extends JPanel implements ActionListener, KeyListener {

    private Circle[] circles = new Circle[7];

    Timer t = new Timer(5, this);
//current x and y
    double x = 150, y = 200;
    double changeX = 0, changeY = 0;
    private Circle;
    private int circlex = 0, circley = 0; // makes initial starting point of circles 0
    private javax.swing.Timer timer2;

    public keyExample() {

        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);


        }

        timer2 = new javax.swing.Timer(33, new MoveListener());
        timer2.start();

    }

    public void NewCircle() {
        Random colors = new Random();
        Color color = new Color(colors.nextInt(256), colors.nextInt(256), colors.nextInt(256));

        Random num = new Random();
        int radius = num.nextInt(45);

        for (int i = 0; i < circles.length; i++) {
            circles[i] = new Circle(circlex, circley, radius, color);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLUE);
        g2.fill(new Rectangle2D.Double(x, y, 40, 40));
        NewCircle();
        for (int i = 0; i < circles.length; i++)
        circles[i].fill(g);

    }

    public void actionPerformed(ActionEvent e) {
        repaint();
        x += changeX;
        y += changeY;
        changeX = 0;
        changeY = 0;

    }

    public void up() {
        if (y != 0) {
            changeY = -3.5;
            changeX = 0;
        }
    }

    public void down() {
        if (y <= 350) {
            changeY = 3.5;
            changeX = 0;

        }
    }

    public void left() {
        if (x >= 0) {
            changeX = -3.5;
            changeY = 0;
        }
    }

    public void right() {
        if (x <= 550) {
            changeX = 3.5;
            changeY = 0;
        }
    }

    private class MoveListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            repaint();
            Random speed = new Random();
            int s = speed.nextInt(8);
            circle.move(0, s);
        }
    }

    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if (code == KeyEvent.VK_UP) {
            up();
        }
        if (code == KeyEvent.VK_DOWN) {
            down();
        }
        if (code == KeyEvent.VK_RIGHT) {
            right();
        }
        if (code == KeyEvent.VK_LEFT) {
            left();
        }
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
    }
}

【问题讨论】:

  • 有什么异常?
  • 我在代码上方添加了异常。它会继续打印出该异常,直到我关闭程序。
  • 圆圈(您尝试在paintComponent中访问)为空
  • 在你尝试用它作画之前让它非空。

标签: java swing jpanel keylistener paintcomponent


【解决方案1】:

您的问题是您尝试使用 circle 变量进行绘制,该变量从未为其分配有效引用。

一种解决方案是通过circle = new Circle(...) 给它一个有效的引用,但话虽如此,我会告诉你忽略它,因为你甚至不应该使用变量circle。摆脱它。您想要做的是使用您的 circles 数组——这就是您应该在您的 paintComponent 方法中绘制的内容。在paintComponent 内部使用for 循环并遍历数组,绘制数组包含的每个圆形项目。

【讨论】:

  • 我编辑了我的代码以显示我为解决问题所做的工作。我创建了一个名为 NewCircle() 的新方法,它使用 for 循环用随机圆圈填充数组。然后我在使用绘画组件时调用了该方法。现在我遇到的问题是将 JPanel 上的圆圈隔开
  • @user3076906:这是一个新问题,应该是一个新问题。
  • 好的,但仍有一些我不确定的事情。 1)在JPanel上隔开圆圈。 2)当其中一个圆圈到达屏幕底部时,如何继续制作新圆圈。 3)如何让所有的圆圈向下移动
  • @user3076906:你应该解决每个问题,一次一个,与其他问题隔离。
猜你喜欢
  • 1970-01-01
  • 2019-01-17
  • 2015-07-04
  • 2020-01-30
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
  • 2013-06-24
  • 2013-10-24
相关资源
最近更新 更多