简体   繁体   English

为什么我的渲染方法不会显示任何内容?

[英]Why won't my render method display anything?

I'm making a game for my APCS class in Java. 我正在用Java编写APCS类游戏。 It uses Algebra and Math, but that's not the problem: 它使用代数和数学,但这不是问题:

I have a State class instead of a State Enum because for some reason, Enum doesn't work. 我有一个State类而不是State Enum,因为由于某种原因,Enum不起作用。

public class State {
    private int option;
    public int getState(){
        return option;
    }
    public void setState(int option){
        this.option=option;
    }
}

Now, I'm 90% sure this isn't the problem because when I set the state to the actual game state number, It runs fine, except it doesn't display the background. 现在,我90%确定这不是问题,因为当我将状态设置为实际游戏状态号时,它运行正常,除了它不显示背景。

My Menu class looks like this: 我的Menu类看起来像这样:

    public Rectangle playButton= new Rectangle(GameMain.WIDTH/2 +120,200,117,49);
    public Rectangle helpButton= new Rectangle(GameMain.WIDTH/2 +120,300,117,49);
    public Rectangle quitButton= new Rectangle(GameMain.WIDTH/2 +120,400,117,49);

    ImageLoader loader = new ImageLoader();

    GameMain game;

    public BufferedImage bgrd, play,help, quit;

    public void init(){
        try {
            bgrd = loader.loadImage("res/imgs/bgrd.png");
            play = loader.loadImage("res/imgs/play.png");
            help = loader.loadImage("res/imgs/help.png");
            quit = loader.loadImage("res/imgs/exit.png");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    public void render(Graphics g){
        Graphics2D g2d = (Graphics2D) g;

        g.drawImage(bgrd,0,0, null);
        g.drawImage(play,game.WIDTH/2 +120,200,null);
        g.drawImage(help,game.WIDTH/2 +120,300,null);
        g.drawImage(quit,game.WIDTH/2 +120,400,null);

    }

You can test it out yourself, but all images are in the correct directory and the res folder has been added to the build path. 您可以自己测试它,但所有图像都在正确的目录中,并且res文件夹已添加到构建路径中。

The ImageLoader class just includes a method loadImage which returns a BufferedImage. ImageLoader类只包含一个返回BufferedImage的方法loadImage。

In my GameMain class, this is my render method which should display the images: 在我的GameMain类中,这是我的渲染方法,它应该显示图像:

private void render(){
        BufferStrategy bStrat = this.getBufferStrategy();
        if(bStrat==null){
            createBufferStrategy(3);
            return;
        }
        Graphics g = bStrat.getDrawGraphics();
        if(state.getState()==1){
            menu.render(g);
        }
        if(state.getState()==2){

        }
        if(state.getState()>2 && state.getState()<11){

            Font font=new Font("Verdana",Font.BOLD,20);

            g.setFont(font);

            g.drawImage(bgrd,0,0,this);

            g.drawString(gameName, 300, 100);
            g.drawImage(wizard,x,y-75,this);
            g.drawImage(hdkn,x,y,this);

            g.dispose();
            bStrat.show();
        }
        if(state.getState()==11){

        }

    }

And here is my init method which (when I'm done) will initialize everything: 这是我的init方法(当我完成时)将初始化所有内容:

public void init(){
        state=new State();
        state.setState(1);
        loader = new ImageLoader();
        menu=new Menu();
        menu.init();

        try{
            bgrd=loader.loadImage("res/imgs/bgrd.png");
            wizard=loader.loadImage("res/sprites/wiz.png");
            hdkn=loader.loadImage("res/sprites/hdkn.png");
        }catch(IOException e){
            e.printStackTrace();
        }

    }

This is the result when I set the state to the Menu state(1): Menu State Game 这是我将状态设置为菜单状态(1)时的结果: 菜单状态游戏

And this is the result when the State is set to the Game state (Numbers 3-10): Game State Game 这是状态设置为游戏状态(数字3-10): 游戏状态游戏时的结果

EDIT: Someone wanted to see my main method, I guess... 编辑:有人想看看我的主要方法,我猜......

game=new GameMain();

        game.setPreferredSize(new Dimension(WIDTH,HEIGHT));
        game.setMaximumSize(new Dimension(WIDTH,HEIGHT));
        game.setMinimumSize(new Dimension(WIDTH,HEIGHT));



        JFrame frame = new JFrame("Math Game");
        frame.add(game);

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        game.start();

EDIT: Found out why it didn't work. 编辑:找出它为什么不起作用。 I didn't include a bStrat.show() after my call to the render method in my GameMain render method. 在我的GameMain渲染方法中调用render方法后,我没有包含bStrat.show()。

EDIT 2: I looked back at my code and if anyone wants to use it, you should also put g.dispose() before it, so that it frees up resource allocation or something 编辑2:我回头看了我的代码,如果有人想要使用它,你也应该把g.dispose()放在它之前,这样它就可以释放资源分配或者某些东西

Maybe your Graphic object g cannot be painted immediately. 也许你的Graphic对象g不能立即绘制。 Try using 尝试使用

g.paintImmediately(0,0, /*your graphic width here*/, /*your graphic height here */)

after you asked for the picture to get drawn. 在你要求绘制图片之后。 This will force a UI refresh of your graphics, without letting your system choose when to refresh it, for this time only. 这将强制UI刷新您的图形,而不会让您的系统选择何时刷新它,仅限此时间。

Have a look here: https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#paintImmediately(int,%20int,%20int,%20int) 看看这里: https//docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#paintImmediately(int,%20int,%20int,%20int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM