开发者社区> 问答> 正文

我应该在哪里放置公共静态void main(String [] args)?

我是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);
         }

    }

}

展开
收起
垚tutu 2019-12-12 09:18:52 814 0
1 条回答
写回答
取消 提交回答
  • #include

    您可以将其添加到中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);
            }
        });
    }
    
    2019-12-12 09:19:07
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多