我一直试图在java中设置圆形的动画。但是,当我在类main中的动画循环中调用repaint()
时,我得到一个错误,我不能在静态方法中引用非静态方法,而main必须是静态的。这是一个简单的程序,它应该可以工作,但由于某些原因它不会。另外,根据this教程,我做的一切都很好。
我的代码:
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);
}
}
}