我是Java的新手,在尝试创建小型蛇形游戏后,发生了错误
错误:在Game.GamePanel类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args)“
有人可以帮我解决错误吗?作为Java的初学者,这将是一个很大的帮助。我正在使用Eclipse最新版本
package Game;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements Runnable, KeyListener {
public static final int WIDTH = 1000;
public static final int HEIGHT = 1000;
//Render
private Graphics2D g2d;
private BufferedImage image;
//GameLoop
private Thread thread;
private boolean running;
private long targetTime;
//Game Stuff
private final int SIZE = 10;
Entity head;
ArrayList<Entity> snake;
//movement
private int dx, dy;
//key input
private boolean up,down,right,left,start;
public GamePanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
requestFocus();
addKeyListener(this);
}
public void addNotify() {
super.addNotify();
thread = new Thread(this);
thread.start();
}
private void setFPS(int fps) {
targetTime = 1000 / fps;
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
if(k == KeyEvent.VK_UP) up = true;
if(k == KeyEvent.VK_DOWN) down = true;
if(k == KeyEvent.VK_LEFT) left = true;
if(k == KeyEvent.VK_RIGHT) right = true;
if(k == KeyEvent.VK_ENTER) start = true;
}
@Override
public void keyReleased(KeyEvent e) {
int k = e.getKeyCode();
if(k == KeyEvent.VK_UP) up = false;
if(k == KeyEvent.VK_DOWN) down = false;
if(k == KeyEvent.VK_LEFT) left = false;
if(k == KeyEvent.VK_RIGHT) right = false;
if(k == KeyEvent.VK_ENTER) start = false;
}
@Override
public void run() {
if(running) return;
init();
long startTime;
long elapsed;
long wait;
while(running){
startTime = System.nanoTime();
update();
requestRender();
elapsed = System.nanoTime() - startTime;
wait = targetTime - elapsed / 1000000;
if(wait > 0) {
try {
Thread.sleep(wait);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
private void init() {
image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ARGB);
g2d = image.createGraphics();
running = true;
setUplevel();
setFPS (10);
}
private void setUplevel() {
snake = new ArrayList<Entity>();
head = new Entity(SIZE);
head.setPosition(WIDTH / 2, HEIGHT / 2);
snake.add(head);
for(int i = 1;i < 10;i++) {
Entity e = new Entity(SIZE);
e.setPosition(head.getX() + (i * SIZE), head.getY());
snake.add(e);
}
}
private void requestRender() {
render(g2d);
Graphics g = getGraphics();
g.drawImage(image, 0,0,null);
g.dispose();
}
private void update() {
if(up && dy == 0) {
dy = -SIZE;
dx = 0;
}
if(down && dy == 0) {
dy = SIZE;
dx = 0;
}
if(left && dx == 0) {
dy = 0;
dx = -SIZE;
}
if(right && dx == 0) {
dy = 0;
dx = SIZE;
}
if(dx != 0 || dy != 0) {
for(int i = snake.size() - 1;i > 0;i--) {
snake.get(i).setPosition(
snake.get(i - 1).getX(),
snake.get(i - 1).getY()
);
}
head.move(dx, dy);
}
if(head.getX() < 0 ) head.getX(WIDTH);
if(head.getY() < 0 ) head.getY(HEIGHT);
if(head.getX() > WIDTH ) head.getX(0);
if(head.getY() > HEIGHT ) head.getY (0);
}
public void render(Graphics2D g2d) {
g2d.clearRect(0, 0, WIDTH, HEIGHT);
g2d.setColor(Color.GREEN);
for(Entity e : snake) {
e.render(g2d);
}
}
}
您可以将其添加到中GamePanel,但是您需要创建一个JFrame并且您想要向其中添加一个实例GamePanel。就像是,
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new GamePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。