Java - 我想使用计时器绘制多个 2D 椭圆,但它不起作用

标签 java swing awt graphics2d ellipse

已尝试搜索,但找不到任何内容。

我正在尝试使用数组和 for 循环绘制多个 2D 椭圆,我每秒都会重新绘制框架。问题是,我每次重新绘制时只得到一个椭圆,有人可以告诉我我的代码有什么问题吗?

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

public class MovingDot extends JFrame{
    static int posX = (int)Math.round(Math.random()*780);
    static int posY = (int)Math.round(Math.random()*780);
    static int width = (int)Math.round(Math.random()*780);
    static int height = (int)Math.round(Math.random()*780);
    static int dots = 0;
    public static Timer timer;

    public MovingDot(){
        super("Moving Dot");

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800, 800);

        Dot2 dot = new Dot2();
        add(dot);

        setVisible(true);
        timer = new Timer((int)Math.round((1000)), timerAction);
        timer.start();
    }

    private ActionListener timerAction = new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent ae){
            posX = (int)Math.round(Math.random()*780);
            posY = (int)Math.round(Math.random()*780);
            width = (int)Math.round(Math.random()*780);
            height = (int)Math.round(Math.random()*780);

            float r = (float)Math.random();
            float g = (float)Math.random();
            float b = (float)Math.random();

            Color col = new Color(r,g,b);

            setBackground(col);

            dots++;

            repaint();
        }
    };

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run()
            {
                new MovingDot();
            }
        });
    }
}


class Dot2 extends JPanel{

    @Override
    public void paintComponent(Graphics c2){
        int x = MovingDot.posX;
        int y = MovingDot.posY;
        int w = MovingDot.width;
        int h = MovingDot.height;

        float r,g,b;

        Color col;

        Graphics2D c = (Graphics2D) c2;

        c.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        Ellipse2D.Float[] e = new Ellipse2D.Float[10];

        for (int i = 0; i < 10; i++) {
            if (i == 0)
                r = (float)Math.random();
            else
                r = 0.163F;
            g = (float)Math.random();
            b = (float)Math.random();

            col = new Color(r,g,b);

            c.setColor(col);
            e[i] = new Ellipse2D.Float(x, y, w, h);
            c.fill(e[i]);
        }
    }
}

我自己发现了问题所在,我必须在我的paintComponent中使x、y、w和h随机。不,这不是学校作业,我正在尝试用书自学 Java。

关于使我的方法静态化,我计划在 JPanel 中使用它们,但我意识到我不需要它们,所以我要删除它们。感谢您的建议!

最佳答案

  • 您不应该在 PaintComponent 内创建 Ellipse 数组,这毫无意义。
  • 而是在类中创建数组。
  • 您的 JPanel 的 PaintComponent 方法中不应包含任何程序逻辑。它应该只有绘制椭圆的代码。也就是说,它应该使用 for 循环遍历数组,如果数组中的项不为 null,则绘制它。
  • 您最好使用 ArrayList<Ellipse2D>而不是一个数组。这样您就不必检查空值。
  • 在计时器的 ActionListener 中,如果您的计数器 < 10,您将向数组添加一个 Ellipse2D 并调用重绘。
  • 如果计数器 >= 10,您将停止计时器

  • 此外,静态变量都不应该是静态的,将它们设置为静态表明程序设计已关闭。如果这是学校作业,可能会导致你的成绩被扣除。

关于Java - 我想使用计时器绘制多个 2D 椭圆,但它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23985079/

相关文章:

java - System.out.println - println 在 Eclipse IDE 中拼写不正确

java - Wro4j、webjars 和 font-awesome

java - JScrollPane 和 JList 自动滚动

Java:我的 JFormattedTextField 中不良行为的来源在哪里?

java - java "Clearthought"TableLayout 项目的最新版本

java - 如何显示用户在 JPanel 的 JTextField 中输入的内容?

java - 使用 GraphicsEnvironment 注销字体?

java - GWT Eclipse 插件安装

java - Java 中将基元或对象放入对象映射中的区别

java - KeyListener 中出现意外的 StackOverflowError