Question

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. 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.

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. 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();".

I get a cannot find symbol error referring to 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!

Was it helpful?

Solution

Where do you call initGame()? Since the timer is started in this method, if you never call it, the Timer never starts.

Regarding:

when I try to run the initGame method it tells me I can't reference it from a static context

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.

i.e.,

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. Instead declare it as a PaintPanel variable.


Edit 2

regarding:

I get a cannot find symbol error referring to initGame.

Please re-read the last paragraph above this edit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top