Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Need help understanding doublebuffer

    Hi, I am new to java programming and am trying to make a very basic game. I would like to make a full screen gui with the code:
    JFrame frame = new JFrame();
    frame.setIgnoreRepaint(true);
    frame.setBounds(new Rectangle(new Point(0, 0), tk.getScreenSize()));
    frame.setUndecorated(true);
    frame.setVisible(true);
    and then make the gui print our a graphic.
    My problem is that the only way i know how to put stuff on the screen is with the paint(Graphics g) method and this forces me to put all my logic into the paint(Graphics g) method. I was wondering if there was a way to create a graphic outside of the paint method(with like drawImage(Image, int, int, ImageObserver)) and then feed it into the paint method. Thank you in advance for helping me.

  • #2
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    IN the end this is what i want:

    imports

    public class Core{
    public Core(){ //constructor
    //JFrame code
    update()
    }
    public static void main(String args[]){
    Core gui=new Core();
    }
    public void update(){
    Graphic g;
    //game logic decides what to draw on graphic
    paint(g); //Feed the created graphic into paint
    }
    public void paint(Graphic g){
    //Put graphic on screen?
    }
    }

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Graphics can be retrieved off of any Graphical component using .getGraphics(). This will return the Graphic in use.
    Best I can describe the idea behind double buffering for the graphics is you track two sets of graphics: your graphic with the intent of being what the graphic will be, and the internal graphic that is. You paint everything you need to your graphic and then swap the graphics during a repaint which minimizes the flicker over using individual component updates.
    Here's an example of double buffering. Its used in an applet, but its logic is identical to using it in a Frame: http://www.realapplets.com/tutorial/...Buffering.html. Its usage overrides that of the paint and update methods to handle the double buffering. It looks like the easiest tutorial I could find, but doesn't explain why it works (for which you can try to google a few more for the actual ideas behind double buffering, there are a lot of good examples with step by step as to what does what).

  • Users who have thanked Fou-Lu for this post:

    javarower (02-27-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •