无法调用render方法

时间:2017-06-27 03:02:16

标签: java render

我的主要课程(命名为"组件)有public static void main(String args[]) {,我用它来打开主菜单。

public static void main(String args[]) {
        Component component = new Component();

        new Menu(component);
    }

这会导致您在运行游戏后立即显示主菜单屏幕。 在主菜单中按一个按钮后,它将使用component.start();

启动游戏

问题是我无法在Menu类中调用render方法,因为它是运行程序时首先打开的方法

抱歉,如果它令人困惑。如果您需要任何其他信息,请与我们联系。感谢

Component类:

    public class Component extends Applet implements Runnable{
    private static final long serialVersionUID = 1L;

    public static int pixelSize = 2;

    public static int moveFromBorder = 0;
    public static double sX = moveFromBorder, sY = moveFromBorder; //Side scrolling cords.
    public static double dir = 0;

    public static int width = 700;
    public static int height = 560;

    public int timer = 0;
    public byte movingTimer = 0;

//   Almost full screen is 1350, 732
//       goodish full screen, doesnt encompass the explorer bar is 1350, 690
//       Regular screen 700, 560
    public static Dimension realSize;
    public static Dimension size = new Dimension(700, 560);
    public static Dimension pixel = new Dimension(size.width / pixelSize, size.height / pixelSize);

    public static Point mse = new Point(0, 0); //Mouse.

    public static String name = "MegaTale Alpha 0.0.1";
    public static String version = "0.0.1 Alpha";
    public static String deathText = "You died!";

    public static boolean isRunning = false;
    public static boolean isMoving = false;
    public static boolean isJumping = false;    
    public static boolean isMouseLeft = false;
    public static boolean isMouseRight = false;
    public static boolean hasDragged = false;
    public static boolean isDebug = false;
    public static boolean isInMenu = false;
    public static boolean soundOn = true;
    public static boolean musicOn = true;
    public static boolean creatingWorld = false;
    public static boolean loadingWorld = false; 
    public static boolean savingWorld = false;

    public static int gamemode = 0;
    public Image screen;

    public static int fps;
    public int totalTime;

    public static Level level;
    public static SecondBlockLayer secondBlockLayer;
    public static Character character;
    public static Inventory inventory;
    public static WorkBench workBench;
    public static Sky sky;
    public static InGameMenu inGameMenu;
    public static Spawner spawner;
    public static Checker checker;
    public static ArrayList<Mob> mob = new ArrayList<Mob>();

    public Component() {
        setPreferredSize(size);
        //Adds key listeners
        addKeyListener(new Listening());
        addMouseListener(new Listening());
        addMouseMotionListener(new Listening());
        addMouseWheelListener(new Listening());
        addKeyListener(new InGameMenu());
    }

    public void start() {
        frame = new JFrame();

        Image cursor = null;
        Image icon = null;

        try {
            cursor = ImageIO.read(getClass().getResourceAsStream("/cursors/cursor.png"));
            icon = ImageIO.read(getClass().getResourceAsStream("/icon.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        //Making a cursor.
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Point cursorHotSpot = new Point(0, 0);
        Cursor customCursor = toolkit.createCustomCursor(cursor, cursorHotSpot, "default");
        frame.setCursor(customCursor);

        frame.add(this);
        frame.pack();
        realSize = new Dimension(frame.getWidth(), frame.getWidth());
        frame.setTitle(name);
        frame.setSize(size);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setIconImage(icon);

        //Defining objects, ect.
        new Tile(); //Loading images.
        level = new Level();
        secondBlockLayer = new SecondBlockLayer();
        character = new Character(Tile.tileSize, Tile.tileSize * 2);
        inventory = new Inventory();
        workBench = new WorkBench();
        sky = new Sky();
        inGameMenu = new InGameMenu();
        spawner = new Spawner();
        checker = new Checker();

        //Starting game loop.
        isRunning = true;
        new Thread(this).start();

    }

    public void stop() {
        isRunning = false;
    }

    public static JFrame frame;
    public static void main(String args[]) {
        Component component = new Component();

        new Menu(component);
    }

    private void clearDeadMobs() {
        for(int i = 0; i < mob.size(); i++) {
            if(mob.get(i).mobHealth <= 0) {
                mob.remove(i);
            }
        }
    }

    public void tick() {

//      if(frame.getWidth() != realSize.width || frame.getHeight() != realSize.height) {
//          frame.pack();
//      }

        if(!isInMenu) {
        character.tick();
        level.tick((int) sX, (int) sY, (pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2); 
        sky.tick();
        inventory.tick();
        checker.tick((int) sX, (int) sY, (pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2);
        workBench.tick();

        for(int i = 0; i < mob.toArray().length; i++) {
            mob.get(i).tick();
            }   

        clearDeadMobs();

        }else { 

        inGameMenu.tick();  
        }   

    }

    public void render() {
        Graphics g = screen.getGraphics();

        //Drawing things
        sky.render(g);

        secondBlockLayer.render(g, (int) sX, (int) sY, (pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2);
        level.render(g, (int) sX, (int) sY, (pixel.width / Tile.tileSize) + 2, (pixel.height / Tile.tileSize) + 2);
        if(!Character.isDead) {
        character.render(g);
        }
        for(int i = 0; i < mob.toArray().length; i++) {
            mob.get(i).render(g);
        }
        if(!Character.isDead) {
        inventory.render(g);
        workBench.render(g);
        }

        g.setColor(Color.WHITE);

        if(isDebug) {
        g.drawString(fps+" FPS", 5, 12);
        g.drawString("X: " + Math.round(sX) / 18, 6, 24);
        g.drawString("Y: " + Math.round(sY) / 18, 6, 35);
        g.drawString("Version: " + version, 6, 46);
        g.drawString("Width: " + getWidth(), 6, 57);
        g.drawString("Height: " + getHeight(), 6, 68);
        g.drawString("IsRaining: " + Sky.isRaining, 6, 79);
        g.drawString("Time: " + Sky.dayFrame, 6, 90);
        }

        if(isInMenu) {
            inGameMenu.render(g);
        }

        if(Character.isDead) {
            g.setColor(new Color(255, 100, 100, 150));
            g.fillRect(0, 0, size.width, size.height);
            g.setColor(Color.WHITE);
            g.setFont(new Font("", Font.PLAIN, 24));
            g.drawString(deathText, 117, 135);
        }

        if(timer < 250) {
            timer++;
            movingTimer += 0.1;

            if(movingTimer > 20) {
                movingTimer = 0;
            }
            //Creating a new world
            if(creatingWorld) {
            g.setColor(Color.WHITE);
            g.drawImage(Tile.menu, WIDTH - 10, HEIGHT - 10, null);
            g.drawString("Generating terrain", 125, 80);
            g.setColor(Color.GRAY);
            g.fillRect(50, 100, timer, 10);
            g.setColor(Color.BLACK);
            g.drawRect(50, 100, 250, 10);
            }
            if(loadingWorld) {
                g.setColor(Color.WHITE);
                g.drawImage(Tile.menu, WIDTH - 10, HEIGHT - 10, null);
                g.drawString("Loading Terrain", 125, 80);
                g.setColor(Color.GRAY);
                g.fillRect(50, 100, timer, 10);
                g.setColor(Color.BLACK);
                g.drawRect(50, 100, 250, 10);
            }
            if(savingWorld) {
                g.setColor(Color.WHITE);
                g.drawImage(Tile.menu, WIDTH - 10, HEIGHT - 10, null);
                g.drawString("Saving", 125, 80);
                g.setColor(Color.GRAY);
                g.fillRect(50, 100, timer, 10);
                g.setColor(Color.BLACK);
                g.drawRect(50, 100, 250, 10);
            }
        }
        g = getGraphics();

        g.drawImage(screen, 0, 0, size.width, size.height, 0, 0, pixel.width, pixel.height, null);
        g.dispose();
    }

    @Override
    public void run() {
        screen = createVolatileImage(pixel.width, pixel.height);

        int frames = 0;
        double nonProcessedSeconds = 0;
        long prevousTime = System.nanoTime();
        double secondsPerTick = 1 / 60.0;
        int tickCount = 0;
        boolean hasTicked = false;
        int totalTime = 0;

        this.totalTime = totalTime;

        while(isRunning) {
            long currentTime = System.nanoTime();
            long passedTime = currentTime - prevousTime;
            prevousTime = currentTime;
            nonProcessedSeconds += passedTime  / 1000000000.0;

            while(nonProcessedSeconds > secondsPerTick) {
                tick();
                nonProcessedSeconds -= secondsPerTick;
                hasTicked = true;
                tickCount++;
                    if(tickCount % 60 == 0) {
                        prevousTime += 1000;
                        fps = frames;
                        frames = 0;
                    }
            }
            if(hasTicked) {
                frames++;
            }
            frames++;
            requestFocus();
            tick();
            render();

            try{
                Thread.sleep(5);
            }catch(Exception e) { }
        }
    }
}

菜单类:

public class Menu extends JFrame {
    private static final long serialVersionUID = 1L;

    public int width = 800;
    public int height = 600;

    private JPanel window = new JPanel();
    private JFrame frame = new JFrame();

    private Rectangle rPlay, rOptions, rHelp, rExit, rSound, rMusic, rBack, rControls, rChangeLogs, rCreateNewWorld, rLoadWorld;
    ImageIcon icon1 = null;
    ImageIcon icon2 = null;
    ImageIcon icon3 = null;
    ImageIcon icon4 = null;
    ImageIcon background = null;

    private int buttonWidth = 200;
    private int buttonHeight = 50;

    private String title = "MegaTale Alpha";

    public Menu(Component component) {

        frame.setTitle(title);
        frame.setSize(new Dimension(width, height));
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.add(component);
        frame.getContentPane().add(window);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);


        Image icon = null;
        try {
            icon = ImageIO.read(getClass().getResourceAsStream("/icon.png"));
             icon1 = new ImageIcon(ImageIcon.class.getResource("/button.png"));
             icon2 = new ImageIcon(ImageIcon.class.getResource("/button2.png"));
             icon3 = new ImageIcon(ImageIcon.class.getResource("/button3.png"));
             icon4 = new ImageIcon(ImageIcon.class.getResource("/button4.png"));

        }catch(Exception e) {
            e.printStackTrace();
        }

        frame.setIconImage(icon);
        window.setLayout(null);
        window.setBackground(Color.GRAY);

        drawButtons();
        //Sound.playMusic("/sounds/music1.wav");    

        frame.repaint();
    }

    private void drawButtons() {

        // MAIN MENU
        JButton play = new JButton("Play");
        rPlay = new Rectangle((width / 2) - (buttonWidth / 2), 120, buttonWidth, buttonHeight);
        play.setBounds(rPlay);
        play.setHorizontalTextPosition(JButton.CENTER);
        play.setVerticalTextPosition(JButton.CENTER);
        play.setForeground(Color.WHITE);
        play.setIcon(icon1);
        play.setVisible(true);
        window.add(play);


        // Main Menu
        play.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Sound.playSoundEffect("/sounds/short_click.wav");
                play.setVisible(false);
                options.setVisible(false);
                help.setVisible(false);
                exit.setVisible(false);

                createNewWorld.setVisible(true);
                loadWorld.setVisible(true);
                back.setVisible(true);

///////////////////////////////

    // Play.
    createNewWorld.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Sound.playSoundEffect("/sounds/short_click.wav");
            Component component = new Component();
            Component.creatingWorld = true;
            Component.loadingWorld = false;
            frame.dispose();
            // STARTS THE GAME
            component.start();
            if(new Random().nextInt(100) < 50) {
                //Music.music2();
            } else if(new Random().nextInt(100) < 50) {
                //Music.music3();
            }

        }
    });
}
    //TODO: Render
    public void render(Graphics g) {
        System.out.println("Render");
    }
}

0 个答案:

没有答案