0

I have been trying to animate a circle shape in java. However, when I call repaint() in my animation loop in the class main, I get an error that I cannot reference a non-static method inside a static one, and main has to be static. This is a simple program and it should work, but for some reason it doesn't. Also, according to this tutorial, I did everything all right.

My code:

    package com.ultraluminous.pong;

    import java.awt.Color;    
    import java.awt.Graphics;   
    import java.awt.Graphics2D;   
    import java.awt.RenderingHints;   

    import javax.swing.JFrame;  
    import javax.swing.JPanel;   

    public class Game extends JPanel{   
        static int x = 0;  
        static int y = 0;

        private static void shift(){
            x+=1;
            y+=1;
            }

            @Override
            public void paint(Graphics g){
                super.paint(g);
                Graphics2D Graph = (Graphics2D) g;
                Graph.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Graph.setColor(Color.CYAN);
                Graph.drawOval(x, y, 50, 50);
                Graph.fillOval(x, y, 50, 50);
            }

            public static void main (String[] args){
                JFrame Win = new JFrame("Pong");
                    Win.add(new Game());
                    Win.setResizable(false);
                    Win.setSize(900, 600);
                    Win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    Win.setVisible(true);
                    while (true) {
                        shift();
                        repaint();
                        Thread.sleep(10);
                    }   
            }
      }
Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47

0 Answers0