Java 2D game graphics

前端 未结 5 760
一个人的身影
一个人的身影 2020-12-07 09:16

Next semester we have a module in making Java applications in a team. The requirement of the module is to make a game. Over the Christmas holidays I\'ve been doing a little

5条回答
  •  独厮守ぢ
    2020-12-07 09:55

    The flickering is due to you writing direct to the screen. Use a buffer to draw on and then write the entire screen in 1 go. This is Double Buffering which you may have heard of before. Here is the simplest form possible.

    public void paint(Graphics g)
    {
    
        Image image = createImage(size + 1, size + 1);
        Graphics offG = image.getGraphics();
        offG.setColor(Color.BLACK);
        offG.fillRect(0, 0, getWidth(), getHeight());
        // etc
    

    See the use of the off screen graphics offG. It is expensive to create the off screen image so I would suggest creating it only on first call.

    There's other areas you can improve this further eg creating a compatible image, using clipping etc. For more fine tuned control of animation you should look into active rendering.

    There's a decent page I have bookmarked discussing game tutorials here.

    Good luck!

提交回复
热议问题