简体   繁体   English

Java:将多个对象绘制到一帧

[英]Java: Painting multiple objects to one frame

I've been working on a java game recently, and I have a lot of it figured out. 我最近一直在从事Java游戏的工作,并且我已经弄清楚了很多。 One thing still plagues me, however. 然而,有一件事仍然困扰着我。 The way it's set up, a player moves across a background (the game board). 设置方式是,玩家在背景(游戏板)上移动。 Currently, every time the player moves, it repaints the whole frame, including the background. 当前,玩家每次移动时,都会重新绘制整个帧,包括背景。 This causes a brief, yet annoying screen flicker whenever the player moves. 每当玩家移动时,这都会导致短暂但令人讨厌的屏幕闪烁。

I've separated out my code to draw the background separately from the things that need to be repainted: 我已经分离出代码来分别绘制背景和需要重绘的内容:

public void drawMap(Graphics pane) {...} public void drawMap(图形窗格){...}

public void drawPlayer(Graphics pane) {...} public void drawPlayer(图形窗格){...}

The problem is that I can't find a way to make the board stay on the screen when I use repaint(); 问题是,当我使用repaint()时,我找不到使板子停留在屏幕上的方法。 , a necessity for the player to move. ,这是玩家移动的必要条件。 Any suggestions? 有什么建议么?

You should look into double buffering, basically you paint an image to the buffer, then paint the buffer. 您应该研究双重缓冲,基本上是先将图像绘制到缓冲区,然后再绘制缓冲区。 It should remove the flickering effect you are talking about. 它应该可以消除您正在谈论的闪烁效果。 Below are a few helpful links: 以下是一些有用的链接:

http://docs.oracle.com/javase/tutorial/extra/fullscreen/doublebuf.html http://docs.oracle.com/javase/tutorial/extra/fullscreen/doublebuf.html
http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering
http://www.ecst.csuchico.edu/~amk/classes/csciOOP/double-buffering.html http://www.ecst.csuchico.edu/~amk/classes/csciOOP/double-buffering.html

Just comment if your having trouble understanding it. 如果您在理解上有困难,请发表评论。

UPDATE: I would also suggest you look in 'Killer game programming in java' . 更新:我还建议您查看“用Java编写的Killer游戏编程” You can get a free ebook of one of the older versions. 您可以免费获得其中一个较旧版本的电子书。 Some of it is a bit out dated, but the first few chapters about setting up a game loop and drawing to the screen etc are still very much relevant. 其中有些过时了,但是关于设置游戏循环和绘制到屏幕等的前几章仍然非常相关。

UPDATE 2: From the second link, try something like this: 更新2:从第二个链接,尝试这样的事情:

private void drawStuff() { 
    BufferStrategy bf = this.getBufferStrategy();
    Graphics g = null;

  try {
    g = bf.getDrawGraphics();

       drawMap(g);
       drawPlayer(g);

  } finally {
    // It is best to dispose() a Graphics object when done with it.
    g.dispose();
  }

  // Shows the contents of the backbuffer on the screen.
  bf.show();

      //Tell the System to do the Drawing now, otherwise it can take a few extra ms until 
      //Drawing is done which looks very jerky
      Toolkit.getDefaultToolkit().sync();   
}

UPDATE 3: This post here gives a nice code sample that you can play with and adapt, that should give you the best idea on how to do double buffering 更新3:这个帖子在这里给出了一个很好的代码示例,你可以玩和适应,这应该给你如何做双缓冲最好的主意

I suggest to avoid redrawing everything with every change. 我建议避免每次更改都重画所有内容。 Instead draw the whole frame at a fixed interval, eg every 50ms. 而是以固定间隔(例如每50毫秒)绘制整个帧。 Just keep the status of every element in a class and if something changes just change the data value. 只需保留类中每个元素的状态即可,如果发生更改,只需更改数据值即可。 Due to the fixed redrawing interval the display will pick up any changes at the next redraw. 由于固定的重绘间隔,显示屏将在下次重绘时获取所有更改。

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

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