改变在eclipse中运行的main()

时间:2014-10-30 19:27:35

标签: java game-engine

在Eclipse中,我应该如何更改程序的入口点。 我有两个独立的包,每个包都有静态主方法。现在,在运行标题包中的main方法后,将调用主包中的原始静态main方法。

我想要发生的是:

显示标题屏幕,如果用户选择玩游戏,则调用Game.main()

以下是两个类:

    package game.title;

import game.main.entities.movement.MenuKeyListener;
import game.main.graphics.ImageLoader;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.*;

import src.game.main.Game;

// Referenced classes of package game.main:
//            MenuKeyListener, Game

public class TitleScreen extends JFrame
{
    class MyCanvas extends Canvas
    {

        public void paint(Graphics g)
        {
            super.paint(g);
            g.drawImage(image, 0, 0, TitleScreen.SCREENHEIGHT, TitleScreen.SCREENWIDTH, this);
        }

        public void update(Graphics g)
        {
            if(TitleScreen.toggle == 0)
            {
                g.clearRect(0, 0, TitleScreen.SCREENWIDTH, TitleScreen.SCREENHEIGHT);
                g.drawImage(image, 0, 0, TitleScreen.SCREENHEIGHT, TitleScreen.SCREENWIDTH, this);
                int xCord[] = {
                    322, 350, 350
                };
                int yCord[] = {
                    533, 533, 553
                };
                g.setColor(Color.CYAN);
                g.fillPolygon(xCord, yCord, 3);
            } else
            if(TitleScreen.toggle == 1)
            {
                g.clearRect(0, 0, TitleScreen.SCREENWIDTH, TitleScreen.SCREENHEIGHT);
                g.drawImage(image, 0, 0, TitleScreen.SCREENHEIGHT, TitleScreen.SCREENWIDTH, this);
                int xCord[] = {
                    322, 350, 350
                };
                int yCord[] = {
                    600, 600, 620
                };
                g.setColor(Color.CYAN);
                g.fillPolygon(xCord, yCord, 3);
            }
        }

        final TitleScreen this$0;

        MyCanvas()
        {
            this$0 = TitleScreen.this;
            //super();
        }
    }


    public static void main(String args[])
        throws IOException
    {
        new TitleScreen();
    }

    public TitleScreen()
        throws FileNotFoundException, IOException
    {
        SCALE = 2;
        quit = false;
        runGame = false;
        setTitle("Mortem");
        setSize(new Dimension(SCREENWIDTH, SCREENHEIGHT));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(3);
        setResizable(false);
        MyCanvas canvas = new MyCanvas();
        add(canvas);
        ImageLoader loader = new ImageLoader();
        image = loader.load("/Title Screen.jpg");
        setVisible(true);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(20, 20));
        JLabel label = new JLabel();
        label.setLocation(10, 10);
        label.setSize(220, 100);
        panel.add(label);
        //Sound music = new Sound("/piano1.wav");
        //music.play();
        canvas.addKeyListener(new MenuKeyListener());
        while(!check) 
        {
            canvas.update(canvas.getGraphics());
            try
            {
                Thread.sleep(500L);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        if(toggle == 1){
            System.out.println("BYE!");
            System.exit(0);
        }
        else if(toggle == 0)
        {
            //music.stop();
            Game.main(null);
            System.out.println("Start Game!");

        }
    }

    private static int SCREENWIDTH = 700;
    private static int SCREENHEIGHT = 700;
    private int SCALE;
    private static Graphics g;
    private final BufferedImage image;
    private static BufferedImage titleImage;
    public static int toggle = 0;
    private boolean quit;
    private boolean runGame;
    public static boolean check = false;




}

哪个电话:

    package game.main;

import game.main.entities.Enemy;
import game.main.entities.Entity;
import game.main.entities.Player;
import game.main.entities.Title;
import game.main.entities.movement.EnemyMovementThread;
import game.main.entities.movement.KeyManager;
import game.main.graphics.CharacterSpriteSheet;
import game.main.graphics.ImageLoader;
import game.main.graphics.ImageManager;
import game.main.graphics.SpriteSheet;
import game.main.levels.Level;
import game.main.sounds.Sound;
import game.main.tiles.Heart0Tile;
import game.main.tiles.Tile;
import game.title.TitleScreen;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable{

    public static final int HEIGHT = 240, WIDTH = 360,  SCALE = 3, TILESIZE = 16;
    public static boolean running = false;
    public Thread gameThread;
    public Thread enemyMovement;

    private BufferedImage spriteSheet;
    private BufferedImage characterAnimation;
    private static ImageManager im;
    private static Level level1;

    static BufferedImage level;


    private static Player player;
    private static Enemy enemy;
    private Title title;
    private Heart0Tile life;

    private Sound titleTheme;

    public void init() throws IOException{
        ImageLoader loader = new ImageLoader();
        spriteSheet = loader.load("/SS.png");
        characterAnimation = loader.load("/characterAnimation.png");

        SpriteSheet ss = new SpriteSheet(spriteSheet);
        CharacterSpriteSheet ca = new CharacterSpriteSheet(characterAnimation);
        im = new ImageManager(ss, ca);

        level = loader.load("/level1.png");

        level1 = new Level(level);



        player = new Player(WIDTH * SCALE / 2, HEIGHT * SCALE /2, im);
        enemy = new Enemy(WIDTH * SCALE /2, HEIGHT * SCALE /2 , im);

        this.addKeyListener(new KeyManager());

        title = new Title(50, 70, ss);

        titleTheme = new Sound("/Boss theme riff (1).wav");

        titleTheme.play();



    }

    public synchronized void start(){
        if(running)return;
            running = true;
            gameThread = new Thread(this);
            gameThread.start();
            enemyMovement = new Thread(new EnemyMovementThread("enemyMovement", 2000));
            enemyMovement.start();
        }

    public synchronized void stop(){
        if(running == false) return;
        running = false;
        try{
            gameThread.join();
            enemyMovement.join();
        }catch(InterruptedException e){
            e.printStackTrace();
        }

    }

    public void run(){
        try {
            init();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long lastTime = System.nanoTime();
        final double AMOUNTOFTICKS = 60D;
        double ns = 1000000000/AMOUNTOFTICKS;
        double delta = 0;


        while(running){
            long now = System.nanoTime();
            delta += (now - lastTime)/ns;
            lastTime = now;
            if(delta >= 1){
                tick();
                delta--;
            }


            render();
        }
        stop();
    }

    public void tick(){
        player.tick();

        //enemy.tick();
        //title.tick();

    }

    public void render(){
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null){
            createBufferStrategy(3);
            return;
        }


        Graphics g = bs.getDrawGraphics();

        //RENDER HERE
        g.fillRect(0, 0, WIDTH*SCALE, HEIGHT*SCALE);
        level1.render(g);
        //title.render(g);
        player.render(g);
        //enemy.render(g);

        //END RENDER
        g.dispose();
        bs.show();


    }


    public static void main(String[] args){
    Game game = new Game();
    game.setPreferredSize( new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
    game.setMaximumSize( new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
    game.setMinimumSize( new Dimension(WIDTH*SCALE, HEIGHT*SCALE));

    JFrame frame = new JFrame("Game");
    frame.setSize(WIDTH*SCALE, HEIGHT*SCALE);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setResizable(false);//change later
    frame.add(game);

    frame.setVisible(true);
    game.start();
    }

    public static Player getPlayer(){
        return player;
    }
    public static Enemy getEnemy(){
        return enemy;
    }
    public static Level getLevel(){
        return level1;
    }
    public static ImageManager getImageManager(){
        return im;
    }

}

尝试运行TitleScreen类后,我在控制台中得到了这个:

错误:无法找到或加载主类game.title.TitleScreen

2 个答案:

答案 0 :(得分:0)

在"运行配置"菜单,您可以设置要运行的主类。

答案 1 :(得分:0)

要更改主要方法,您需要在启动配置中修改它。

1.Right click your project
2. Select Run as-> Run Configurations
3. Set your main method for your application by searching the main class.
4. Save configuration and run your project.