【问题标题】:Trying to Paint Several Objects in Java尝试在 Java 中绘制多个对象
【发布时间】:2018-03-31 00:00:10
【问题描述】:

所以我有一个名为 Game 的类,其中包含我要绘制的对象的 ArrayList 和一个计时器。它们都实现了 ActionListener。 我通过 ArrayList 在 Game 循环中执行 actionPerformed 并为每个项目调用 actionPerformed。然后,每个对象的 actionPerformed 方法调用 repaint。但是,这似乎只绘制了 ArrayList 中的最后一个对象。

我在每个对象的 actionPerformed 方法中放了一些测试打印,程序确实到达了所有对象的重绘线。

它看起来像:

public class Game extends JFrame implements ActionListener
{
    public ArrayList<GameObject> things = new ArrayList<GameObject>();
    public Timer t = new Timer(5, this);
    public Game ()
    {
        super();
        this.setSize(620, 440);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);

        this.setTitle("Moving Ball");
        GameObject b = new Ball(this);
        GameObject p = new Paddle(this);
        things.add(b);
        things.add(p);


        for (int i = 0; i < things.size(); i++)
        {
            this.add(things.get(i));
        }

        t.start();
        this.setVisible(true);
    }

    public void actionPerformed (ActionEvent e)
    {
        for (int i = 0; i < things.size(); i++)
        {
            things.get(i).actionPerformed(e);
        }
    }
}

并且游戏对象已经覆盖了paintComponent并包含:

public void actionPerformed (ActionEvent e)
{
    //...
    repaint();
    //...
}

【问题讨论】:

    标签: java timer actionlistener paintcomponent repaint


    【解决方案1】:

    您没有在任何地方触发动作侦听器。你应该添加一行

    things.get(i).addActionListener(this);
    

    在适当的地方。看起来像这样:

    public class Game extends JFrame implements ActionListener
    {
        public ArrayList<GameObject> things = new ArrayList<GameObject>();
        public Timer t = new Timer(5, this);
        public Game ()
        {
            super();
            this.setSize(620, 440);
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setResizable(false);
    
            this.setTitle("Moving Ball");
            GameObject b = new Ball(this);
            GameObject p = new Paddle(this);
            things.add(b);
            things.add(p);
    
    
            for (int i = 0; i < things.size(); i++)
            {
                this.add(things.get(i));
            }
            for (int i = 0; i < things.size(); i++)
            {
                things.get(i).addActionListener(this);
            }
    
            t.start();
            this.setVisible(true);
        }
    
        public void actionPerformed (ActionEvent e)
        {
            //repaint your gameobject
        }
    }
    

    【讨论】:

    • 所以由于我的 GameObject 扩展了 JPanel,我不能在它们上使用 addActionObject,但我可以在 Timer 上使用它。所以我用 t.addActionObject(things.get(i)); 尝试了两个循环。和 t.addActionObject(this);。当然,这些都行不通。
    • 当我用 this 初始化我的 Timer 时,它实际上是在执行 t.addActionObject(this),并且由于每次 Timer 事件发生时,Game 都会执行 actionPerformed,它会执行每个 GameObject 的 actionPerformed,这实际上是和 t.addActionObject(things.get(i)) 一样。
    • 在我看来,我的实际问题是我的 JFrame 中添加了多个 JPanel。我尝试将一个主面板添加到我的 JFrame 并将我的 GameObject 面板添加到主面板,但这不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多