简体   繁体   English

如何从静态上下文初始化重绘计时器?

[英]How can I initialize a repaint timer from a static context?

After some reading I am back to trying to make these drawings move. 经过一番阅读后,我又回来试图让这些图纸移动。 Where I am having difficulty is that the timer is never started - when I try to run the initGame method it tells me I can't reference it from a static context. 我遇到困难的地方是计时器永远不会启动 - 当我尝试运行initGame方法时,它告诉我我无法从静态上下文中引用它。 If not the main loop, then where do I start this? 如果不是主循环,那么我从哪里开始呢? I have been able to make it start in drawComponent, but that is just creating a new timer every time it draws which is no good. 我已经能够在drawComponent中启动它,但这只是在每次绘制时创建一个新的计时器,这是不好的。

Main class: 主要课程:

public class Test2 extends JFrame {

    private JPanel paintPanel;
    public Test2() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(800, 600));
        paintPanel = new  PaintPanel();
        getContentPane().add(paintPanel, BorderLayout.CENTER);
        pack();
    }
    class PaintPanel extends JPanel implements ActionListener {
        private List<Shape> gladiators;
        private Shape mouseOverShape=null;
        private Timer timer;

        public void initGame() {

            timer = new Timer(50, this);
            timer.start();

        }       
        @Override
        public void actionPerformed(ActionEvent e) {
            repaint();
                        // change object location here
            System.out.println("Repainting");
        }

            public PaintPanel(){
            super();

            // Create my Gladiator objects and add them here?

        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            for (Shape s : gladiators){
                g2.draw(s);
            }
        }
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Test2 gamePanel = new Test2();
                gamePanel.setVisible(true);
                PaintPanel.initGame();
            }
        });
    }
}

Gladiator class: 角斗士课程:

public class Gladiator implements Drawable{


        int[] location = new int[] {25,25};

        public void Draw(Graphics g){
               // draw out the shapes which constitute each "gladiator"
    }

}

I have also included some quote lines which indicate where I assume other actions go. 我还包括一些引用行,表明我假设其他行动去了哪里。 I would appreciate any feedback on any major problems with those or other concepts. 对于那些或其他概念的任何重大问题,我将不胜感激。 Thanks all! 谢谢大家!

Edit: 编辑:

Following Hovercraft Full of Eels suggestion I moved the declaration of the method from inside the main method to inside of the Test2 method... I also changed a capitalization error. 继Hovercraft Full of Eels建议后,我将方法的声明从main方法内部移到了Test2方法的内部......我也改变了大小写错误。 It now looks like this: 它现在看起来像这样:

public Test2() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setMinimumSize(new Dimension(800, 600));
    paintPanel = new  PaintPanel();
    getContentPane().add(paintPanel, BorderLayout.CENTER);
    pack();

    paintPanel.initGame();  
}

Main method is the same, minus the "PaintPanel.initGame();". 主方法是相同的,减去“PaintPanel.initGame();”。

I get a cannot find symbol error referring to initGame. 我找到了一个无法找到引用initGame的符号错误。

2nd Edit: 第二编辑:

I think my question was answered. 我想我的问题得到了回答。 I will make another edit if I still have questions or mark this answered in a bit. 如果我还有问题或者稍微回答一下,我会再做一次编辑。 Thanks, Hovercraft! 谢谢,气垫船!

Where do you call initGame() ? 你在哪里调用initGame() Since the timer is started in this method, if you never call it, the Timer never starts. 由于计时器是以此方法启动的,如果您从未调用它,则Timer永远不会启动。

Regarding: 关于:

when I try to run the initGame method it tells me I can't reference it from a static context 当我尝试运行initGame方法时,它告诉我无法从静态上下文中引用它

You need to show us this attempt. 你需要告诉我们这个尝试。 Usually this means that you're trying to call the method on the class not on the instance. 通常这意味着您尝试在类上调用不在实例上的方法。

ie,

public Test2() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setMinimumSize(new Dimension(800, 600));
    paintPanel = new  PaintPanel();
    getContentPane().add(paintPanel, BorderLayout.CENTER);
    pack();

    paintPanel.initGame();  
}

Edit 编辑

You state: 你说:

PaintPanel.initGame();

You can't do this. 你不能这样做。 You can't call a non-static method on a class as if it's a static method. 您不能在类上调用非静态方法,就好像它是静态方法一样。 Instead it must be called on the instance as I show above. 相反,它必须在我上面显示的实例上调用。 So the answer to the question, 那么这个问题的答案,

How can I initialize a repaint timer from a static context? 如何从静态上下文初始化重绘计时器?

Is that you don't. 那是你没有。 You initialize it from a non -static context. 您从静态上下文初始化它。

Note that you shouldn't declare the paintPanel variable as a JPanel since you need to call the initGame() method on it which is not a method of JPanel. 请注意,您不应将paintPanel变量声明为JPanel,因为您需要在其上调用initGame()方法,而不是JPanel的方法。 Instead declare it as a PaintPanel variable. 而是将其声明为PaintPanel变量。


Edit 2 编辑2

regarding: 关于:

I get a cannot find symbol error referring to initGame. 我找到了一个无法找到引用initGame的符号错误。

Please re-read the last paragraph above this edit. 请重新阅读此编辑上方的最后一段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM