Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Problems with Double Buffering -- BufferStrategy

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with Double Buffering -- BufferStrategy

    Alright I have programmed in Java many times and made games before but I always made applets so this is my first time using JPanel/JFrame. I have gotten everything underway and have been stuck on getting this different style of drawing working for me. Usually I just create an image and use paint() drawImage to display the the buffered image. I decided to try using the BufferStrategy method but I am having the hardest time getting it to work. I know my code is gonna be pretty botched up now since I went through all kinds of different tutorials and what not trying to figure it out. Anyway I'm sure someone can help and trust me I have been working on this all day (roughly 6-8hrs). Posting on the forums is always the last thing I do but sometimes I just need someones help to make it click.

    package bttg;
     
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.image.BufferStrategy;
     
    public class BTTG extends JPanel
    {
        BufferStrategy bufferStrat;
        Boolean running = true;
     
        private short screenSize = 800;
     
     
        public void render()
        {
     
            Graphics2D buffer = (Graphics2D) bufferStrat.getDrawGraphics();
     
    	buffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
     
            buffer.setColor(Color.darkGray);
            buffer.fillRect(0,0,screenSize,screenSize);
     
            Font monospaced = new Font("MONOSPACED", Font.BOLD, 15);
            buffer.setFont(monospaced);
    	buffer.setColor(Color.BLACK);
            buffer.fillRect(0,0,screenSize,19);
            buffer.setColor(Color.WHITE);
            buffer.drawString("BTTG",5,13);
     
            buffer.dispose();
            bufferStrat.show();
        }
     
     
        public BTTG()
        {
     
            JFrame container = new JFrame("BTTG");
            JPanel panel = (JPanel) container.getContentPane();
            panel.setPreferredSize(new Dimension(screenSize,screenSize));
            panel.setLayout(null);
     
            setBounds(0,0,screenSize,screenSize);
            panel.add(this);
     
            container.pack();
            container.setResizable(false);
            container.setVisible(true);
     
            setIgnoreRepaint(true);
     
           	container.createBufferStrategy(2);
    	bufferStrat = container.getBufferStrategy();
     
             while(running)
            {  
            render();
            try { Thread.sleep(10); } catch (Exception e) {}
            }
     
     
        }
     
     
     
     
        public static void main(String[] args) {
        new BTTG();
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems with Double Buffering -- BufferStrategy

    Where is the render() method called?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with Double Buffering -- BufferStrategy

    Woah woah woah I moved my drawString() to 50 , 50 and its there everything isn't being placed properly. Is there a reason for this? I thought the white on the edges were kinda weird.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Problems with Double Buffering -- BufferStrategy

    I moved my drawString() to 50 , 50 and its there everything isn't being placed properly.
    I ran the code (using both sets of coordinates) and the string was placed properly both times.

    ---

    Is it normal to construct the gui in the initial thread rather than the EDT (and not give main() a chance to finish)? That's a genuine question: I've never used anything other than the default strategy where using the EDT is nonnegotiable.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with Double Buffering -- BufferStrategy

    Everything is still shifted up and to the left leaving a white reversed L on the right side. Anyone got any idea of whats going on I put the project on my bros computer and it does the same thing. There should be a black bar that runs along the top and if the string should be inside it. I'm using netbeans for the first time as well if that has got anything to do with it, but I doubt it should.

    Quote Originally Posted by pbrockway2 View Post
    I ran the code (using both sets of coordinates) and the string was placed properly both times.
    You didn't change anything cause, like I said, I tried it on my brothers computer and its doing the same thing to me.

    Attachment 2033
    Just shows the displacement highlighted green in paint.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problems with Double Buffering -- BufferStrategy

    In your constructor you are adding "this" before "this" has been fully constructed.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with Double Buffering -- BufferStrategy

    Sorry I'm gonna need you to elaborate a little more.

    I changed this since I don't think I actually need JPanel. It fit the fillRect to the screen right for some reason but the cords for the string and the black rectangle are still off.
     
        public BTTG()
        {
            JFrame container = new JFrame("BTTG");
            container.setPreferredSize(new Dimension(screenSize,screenSize));
            container.setLayout(null);
            container.setBackground(Color.yellow);
            container.setBounds(0,0,screenSize,screenSize);
            container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            container.add(this);
            container.pack();
            container.setResizable(false);
            container.setVisible(true);
            setIgnoreRepaint(true);
     
           	container.createBufferStrategy(2);
    	bufferStrat = container.getBufferStrategy();
     
     
            while(running)
            {  
            render();
            try { Thread.sleep(10); } catch (Exception e) {}
            }
     
     
        }

  8. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Problems with Double Buffering -- BufferStrategy

    Sorry I'm gonna need you to elaborate a little more.
    public BTTG()
    {
        JFrame container = new JFrame("BTTG");
        container.setPreferredSize(new Dimension(screenSize,screenSize));
        container.setLayout(null);
        container.setBackground(Color.yellow);
        container.setBounds(0,0,screenSize,screenSize);
        container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        container.add(this); // <--- ?
        // etc

    I think jps is referring to the fact that you use the panel (specifically, you add it to the frame) before the BTTG() constructor has finished. That is, before the panel has been constructed.

    You didn't change anything cause, like I said, I tried it on my brothers computer and its doing the same thing to me.
    the cords for the string and the black rectangle are still off.
    As I said, I ran your original code both "buffer.drawString("BTTG",5,13);" and with "buffer.drawString("BTTG",50,50);". And in both cases it appeared (or failed to appear) exactly where it ought to appear (or fail to appear) according to the various API docs. I can't access the attachment, but in any case it would help if you elaborated on "the same thing", "off", "placed properly" etc: where do you intend the text to be painted? and where have you assumed the point x=5,y=13 is on the screen?

    ---

    No-one has addressed the question I posed in #4. I would suggest (a) you (fully) construct the gui on the event dispatch thread and not the initial thread and (b) use Swing's default double buffering rather than rolling your own. If there are performance problems then it might be time to consider a different buffering strategy, but even then my every instinct (uninformed by any actual experience with using buffering strategies...) would be to have it render in its own thread which would sleep when it's not doing anything to let the rest of the runtime do its thing.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with Double Buffering -- BufferStrategy

    Can you show what a bare application setup would look like for a 800,800. Maybe if I can see how some one has made a window with no errors I can see what I'm doing wrong. If there is a example in some tutorial that I could look at instead that would be cool to. Although I just got done trying to get all my code in with another tutorial and had problems the whole way just to find out that when I tried just using the code shown in the tut it didn't work and was the problem to begin with. If I can just get past getting the main window up where it can draw correctly the rest would be not a problem.

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problems with Double Buffering -- BufferStrategy

    I suggest you check out any of the wealth of samples available through a search engine and the tutorials. Here is a link to the Oracle custom painting tutorial. Then start your project code over once you have a better understanding.
    You will want to make sure each component's constructor has returned before using that component. You can search using keywords including "displayable" for more information on JComponents, but I basically stated what you need to know on that. Also, in the posted code the BTTG constructor only returns when the infinite loop ends, that loop will need to be moved out of the constructor so the constructor can return.

Similar Threads

  1. [SOLVED] Double Buffering Problem
    By beansnbacon in forum What's Wrong With My Code?
    Replies: 16
    Last Post: March 30th, 2013, 09:24 PM
  2. Double Buffering in game programming
    By WaffleZombie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 18th, 2012, 03:26 PM
  3. Android - SurfaceView flickers - Double Buffering
    By Nesh108 in forum Android Development
    Replies: 2
    Last Post: April 22nd, 2012, 06:09 AM
  4. Help With Double Buffering/Animations
    By bgroenks96 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 18th, 2011, 06:58 PM
  5. Double Buffering
    By Ganezan in forum Java Theory & Questions
    Replies: 2
    Last Post: November 20th, 2009, 03:51 AM