Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  BufferStrategy - it doesn't update graphics  (Read 378 times)
0 Members and 1 Guest are viewing this topic.
Offline TheDirectShift

Senior Newbie





« Posted 2014-02-09 23:47:53 »

Hi, I'm having problems with BufferStrategy... for some reason I can't manage to update the graphics in it:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public void Draw()
    {
        backbuffer = this.getBufferStrategy();
       
        do
        {
            try{
                    graphics = backbuffer.getDrawGraphics();
                    DrawAll(); //this draws thing on "graphics"
           }
            finally
            {
                graphics.dispose();
            }

            backbuffer.show();
            Toolkit.getDefaultToolkit().sync();
        } while (backbuffer.contentsLost());
    }


It is necessary to say that I always call this method? but for some reason I can't see my ... new graphics?
If you need more details, just tell me.

Thanks.
Online BurntPizza
« Reply #1 - Posted 2014-02-09 23:55:02 »

We're probably gonna need to see more code to provide much useful help, for instance the DrawAll() method, and how/when things are initiated would be useful.

First, although I don't think it should make a difference (could be wrong, it's been awhile), I would dispose the graphics after backbuffer.show(), as per http://docs.oracle.com/javase/tutorial/extra/fullscreen/bufferstrategy.html
Offline TheDirectShift

Senior Newbie





« Reply #2 - Posted 2014-02-09 23:56:50 »

the only way I actually managed to "correctly" update the drawing, is by drawing something over it, using:

1  
2  
graphics.setColor (getBackground ());
graphics.fillRect (0, 0, this.getSize().width, this.getSize().height);


but, isn't
1  
graphics.dispose();
supposed to clear the graphics and let space for the incoming?

thanks

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online BurntPizza
« Reply #3 - Posted 2014-02-10 00:00:50 »

the only way I actually managed to "correctly" update the drawing, is by drawing something over it, using:

1  
2  
graphics.setColor (getBackground ());
graphics.fillRect (0, 0, this.getSize().width, this.getSize().height);


but, isn't
1  
graphics.dispose();
supposed to clear the graphics and let space for the incoming?

thanks



No. Graphics.dispose() basically destroys that particular graphics context, and any calls to it after are ignored.

Also, instead of setting the current color to the background color and fillRect, you can simply: clearRect()
That uses the background color (hence 'clear' as in wipe off) automatically.
Offline TheDirectShift

Senior Newbie





« Reply #4 - Posted 2014-02-10 00:02:11 »

Quote
First, although I don't think it should make a difference (could be wrong, it's been awhile), I would dispose the graphics after backbuffer.show(), as per http://docs.oracle.com/javase/tutorial/extra/fullscreen/bufferstrategy.html

this link dispose the graphics before showing them from backbuffer

Quote
Also, instead of setting the current color to the background color and fillRect, you can simply: clearRect()

good tip
Online BurntPizza
« Reply #5 - Posted 2014-02-10 00:16:39 »

Quick working example usage of BufferStrategy:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class BSTest {
   
   public static void main(String[] a) throws InterruptedException {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(400, 400);
      frame.setVisible(true);
      frame.setLocationRelativeTo(null);
     
      frame.createBufferStrategy(2);
     
      while (true) {
         Graphics g = null;
         t: try {
            g = frame.getBufferStrategy().getDrawGraphics();
            if (g == null) break t;
            render(g);
         } finally {
            g.dispose();
         }
         frame.getBufferStrategy().show();
         Thread.sleep(20);
      }
   }
   
   private static void render(Graphics g) {
      g.clearRect(0, 0, 400, 400);
      g.setColor(Color.blue);
      g.fillOval((int) System.currentTimeMillis() % 400, (int) System.currentTimeMillis() % 400, 10, 10);
   }
}


This works fine for me, take from it what you will.
Offline TheDirectShift

Senior Newbie





« Reply #6 - Posted 2014-02-10 00:55:59 »

Thanks, looks pretty similar to my approach, but I think that what does the trick is
 
1  
2  
    g.clearRect(0, 0, 400, 400);
      g.setColor(Color.blue);


so, Solved!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (48 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!