Java 无法调用render方法

Java 无法调用render方法,java,render,Java,Render,我的主类(名为“Component”)具有公共静态void Main(字符串args[]){,我使用它打开主菜单 public static void main(String args[]) { Component component = new Component(); new Menu(component); } 这会使主菜单屏幕在您运行游戏时立即出现。 在主菜单中,一旦按下按钮,它将使用component.start(); 问题是我不能在菜单类中

我的主类(名为“Component”)具有
公共静态void Main(字符串args[]){
,我使用它打开主菜单

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

        new Menu(component);
    }
这会使主菜单屏幕在您运行游戏时立即出现。 在主菜单中,一旦按下按钮,它将使用
component.start();

问题是我不能在菜单类中调用render方法,因为它是运行程序时打开的第一个对象

抱歉,如果这让人困惑。如果您需要任何其他信息,请告诉我。谢谢

组件类:

    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) { }
        }
    }
}
公共类组件扩展小程序实现可运行{
私有静态最终长serialVersionUID=1L;
公共静态整数像素大小=2;
公共静态int-moveFromBorder=0;
public static double sX=moveFromBorder,sY=moveFromBorder;//侧面滚动线。
公共静态双目录=0;
公共静态整数宽度=700;
公共静态内部高度=560;
公共整数计时器=0;
公共字节movingTimer=0;
//几乎全屏显示的是1350732
//好的全屏,不包括浏览器栏是1350690
//普通屏幕700560
公共静态维度realSize;
公共静态维度大小=新维度(700560);
公共静态标注像素=新标注(size.width/pixelSize、size.height/pixelSize);
公共静态点mse=新点(0,0);//鼠标。
公共静态字符串name=“MegaTale Alpha 0.0.1”;
公共静态字符串version=“0.0.1 Alpha”;
公共静态字符串deathText=“你死了!”;
公共静态布尔值isRunning=false;
公共静态布尔值isMoving=false;
公共静态布尔值=false;
公共静态布尔值isMouseLeft=false;
公共静态布尔值isMouseRight=false;
公共静态布尔值=false;
公共静态布尔值isDebug=false;
公共静态布尔值isInMenu=false;
公共静态布尔soundOn=true;
公共静态布尔值musicOn=true;
公共静态布尔creatingWorld=false;
publicstaticbooleanloadingworld=false;
公共静态布尔savingWorld=false;
公共静态整数游戏模式=0;
公众形象屏;
公共静态整数fps;
公共时间;
公共静态层面;
公共静态二次BlockLayer二次BlockLayer;
公共静态特征;
公共静态库存;
公共静态工作台;
公共静止天空;
公共静态InGameMenu InGameMenu;
公共静态产卵器产卵器;
公共静态检查器;
public static ArrayList mob=new ArrayList();
公共部分(){
设置首选大小(大小);
//添加关键侦听器
addKeyListener(新建侦听());
addMouseListener(新监听());
addMouseMotionListener(新建侦听());
addMouseWheelListener(新建侦听());
addKeyListener(新的InGameMenu());
}
公开作废开始(){
frame=新的JFrame();
图像光标=空;
图像图标=空;
试一试{
cursor=ImageIO.read(getClass().getResourceAsStream(“/cursors/cursor.png”);
icon=ImageIO.read(getClass().getResourceAsStream(“/icon.png”);
}捕获(例外e){
e、 printStackTrace();
}
//制作光标。
Toolkit=Toolkit.getDefaultToolkit();
点光标热点=新点(0,0);
Cursor customCursor=toolkit.createCustomCursor(Cursor,cursorHotSpot,“默认”);
frame.setCursor(customCursor);
框架。添加(此);
frame.pack();
realSize=新维度(frame.getWidth(),frame.getWidth());
frame.setTitle(名称);
框架。设置尺寸(尺寸);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setresizeable(false);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
frame.setIconImage(图标);
//定义对象等。
新建平铺();//加载图像。
级别=新级别();
secondBlockLayer=新的secondBlockLayer();
字符=新字符(Tile.tileSize,Tile.tileSize*2);
存货=新存货();
workBench=newworkbench();
天空=新天空();
inGameMenu=新的inGameMenu();
产卵器=新产卵器();
checker=新的checker();
//开始游戏循环。
isRunning=true;
新线程(this.start();
}
公共停车场(){
isRunning=false;
}
公共静态JFrame;
公共静态void main(字符串参数[]){
组件=新组件();
新菜单(组件);
}
私有无效clearDeadMobs(){
对于(int i=0;ipublic 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");
    }
}