简体   繁体   中英

java game loop. a little help to beginner

@Override
    public void run() {
            while (running) {
                    currentState.update();
                    prepareGameImage();
                   // next line draws image(frame) to the screen
                    currentState.render(gameImage.getGraphics()); 
                    repaint();                      
            }

            // End game immediately when running becomes false.
            System.exit(0);
    }


    private void prepareGameImage() {
            if (gameImage == null) {
                    gameImage = createImage(gameWidth, gameHeight);
            }

            Graphics g = gameImage.getGraphics();
            g.clearRect(0, 0, gameWidth, gameHeight);
    }

this is a snippet of game loop. a little explanation from the book about how it works. In prepareGameImage() we prepare an off-screen image by creating and initializing the gameImage variable with a width of gameWidth and a height of gameHeight. (i dont get how this works --->) Next, on every frame, we clear this image using a rectangle of equal size to clear all images that have been drawn to the screen in the previous frame. This ensures that images from the previous frame do not bleed into the current frame. Every frame starts anew.
what i dont understand is last 2 lines of the snippet. value of gameImage.getGraphics(); gets stored inside of Graphics variable g . method g.clearRect(0, 0, gameWidth, gameHeight); should only affect variable g and should not affect value generated by gameImage.getGraphics();
could you explain how does the last 2 lines of code do the task - "images from the previous frame do not bleed into the current frame" :( :(
thanks

gameImage.getGraphics();

only passes the refference(does not make a copy) to the internal Graphics of the gameImage.

Lets say that gameImage is an instance of some class A that has a private variable of the type Graphics G. And has a method for accessing that variable:

public Graphics getGraphics(){
   return this.G;
}

as you can se... by calling the getGraphics, you only have a reference(pointer) to the graphics.

The "Graphics" element is something global to your program. It is managing all of your graphics , no matter where they are. But since it is this global thing, you can't just use a variable you define like, say, a String, but must get it from an existing object that has a reference to it. For example, every Image has a reference to the graphics object. The variable g you have is a reference to this element now and it can be used. This reference is then used in the next line to clear out the whole screen and previously created images so they do not bleed into the current frame .

(Note: This explaination may not be 100% accurate, but that's how it made me understand it in the first place.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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