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 17 of 17

Thread: Double Buffering Problem

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Double Buffering Problem

    I am a beginner java programmer and I recently starting learning how to program applets. When I realized my first game started flickering like mad, I looked up ways to solve it and found double buffering. Before applying double buffering, my program ran perfectly except for the flickering, but afterwards the program stopped running certain parts of my paint method. For example when you press "H" to access the rules to play the game, the variable is changed but doesn't show the menu at all. I used the tutorial at Java Applet Tutorial - Double Buffering Example for double buffering. Here is my source code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.Timer;
     
    public class character extends JApplet implements ActionListener, KeyListener{
     
    	//variables
    	Random r = new Random();
    	Scanner s;
    	private Timer t = new Timer(5, this);
    	Graphics g2;
    	Image offscreen;
    	boolean gameOver = false, wait = false, help = false, h = false, c = false;
    	int cx = 40, cy = 40, upspeed, downspeed, leftspeed, rightspeed, score, hscore,
    			ex = 350, ey = 350, w = 16, bx = getRandomX(30, 30), by = getRandomY(30, 30);
     
    	public void init(){
    		setSize(400, 400);
    		t.start();
    		addKeyListener(this);
    		setFocusable(true);
    		setFocusTraversalKeysEnabled(false);
    		offscreen = createImage(400, 400);
    		g2 = offscreen.getGraphics();
    	}
    	public void update(Graphics g){
    		paint(g);
    	}
     
    	public void paint(Graphics g){
    		super.paint(g2);
    		g2.clearRect(0, 0, 400, 400);
    		g2.setColor(Color.BLACK);
    		g2.fillRect(0, 0, 400, 400);
    		if(help){
    			g2.setColor(Color.WHITE);
    			g2.drawString("You are the red box. Use the arrow keys to move it.", 0, 10);
    			g2.drawString("The green box chases you. Avoid it with the arrow keys.", 0, 22);
    			g2.drawString("Every time the green and blue boxes touch, you get a point.", 0, 34);
    			g2.drawString("You also get bigger the higher your score is.", 0, 46);
    			g2.drawString("If you touch the green or blue box, it's game over.", 0, 58);
    			g2.drawString("Once you reach 30 points you won't get any bigger.", 0, 70);
    			g2.drawString("PRESS [H] TO RETURN", 0, 82);
    		}
    		if(gameOver && help == false){
    			g2.setColor(Color.WHITE);
    			g2.drawString("GAME OVER", 170, 363);
    			g2.drawString("SCORE: " + score, 0, 10);
    			g2.drawString("HIGH SCORE: " + hscore, 0, 22);
    			g2.drawString("PRESS [SPACE] TO RESTART", 120, 375);
    			g2.drawString("PRESS H FOR HOW TO PLAY", 122, 387);
    			g2.setColor(Color.RED);
    			g2.fillRect(30, 30, 16, 16);
    			g2.setColor(Color.GREEN);
    			g2.fillRect(350, 350, 16, 16);
    			g2.setColor(Color.BLUE);
    			g2.fillRect(bx, by, 16, 16);
    		}
    		if(gameOver == false && help == false){
    			g2.setColor(Color.WHITE);
    			g2.drawString("SCORE: " + score, 0, 10);
    			g2.drawString("HIGH SCORE: " + hscore, 0, 22);
     
    			g2.setColor(Color.RED);
    			g2.fillRect(cx, cy, w, w);
    			g2.setColor(Color.BLUE);
    			g2.fillRect(bx, by, 16, 16);
    			g2.setColor(Color.GREEN);
    			g2.fillRect(ex, ey, 16, 16);
    			if(wait == false){
    				g2.setColor(Color.WHITE);
    				g2.drawString("PRESS [SPACE] TO PLAY", 130, 375);
    				g2.drawString("PRESS H FOR HOW TO PLAY", 120, 387);
    			}
    			g.drawImage(offscreen, 0, 0, this);
    		}
    	}
     
    	//action listener
    	public void actionPerformed(ActionEvent e){
    		repaint();
    		if(cx > 0){
    			cx -= leftspeed;
    		}
    		if(cx < 400-w){
    			cx += rightspeed;
    		}
    		if(cy > 0){
    			cy -= upspeed;
    		}
    		if(cy < 400-w){
    			cy += downspeed;
    		}
    		if(wait && help == false){
    			if(cy+w/2 <= ey+8 && ey > 0){
    				ey -= 1;
    			}
    			if(cy+w/2 >= ey+8 && ey < 384){
    				ey++;
    			}
    			if(cx+w/2 >= ex+8 && ex < 384){
    				ex++;
    			}
    			if(cx+w/2 <= ex+8 && ex > 0){
    				ex -= 1;
    			}
    		}
    		if(cx >= ex-w && cx <= ex+16 && cy >= ey-w && cy <= ey+16 && wait){
    			gameOver = true;
    		}
    		if(cx >= bx-w && cx <= bx+16 && cy >= by-w && cy <= by+16 && wait){
    			gameOver = true;
    		}
    		if(ex < bx+18 && ex > bx-18 && ey < by+18 && ey > by-18){
    			if(gameOver == false){
    				score++;
    				setRandom(cx, cy);
    			}
    		}
    		if(score > hscore){
    			hscore = score;
    		}
    		if(gameOver && wait){
    			setRandom(30, 30);
    			wait = false;
    		}
    		if(score < 30){
    			w = 16+score;
    		}
    		else{
    			w = 46;
    		}
    	}
     
    	//key listener
    	public void keyPressed(KeyEvent e){
    		int code = e.getKeyCode();
    		if(wait && help == false){
    			if(code == KeyEvent.VK_LEFT){
    				leftspeed = 2;
    			}
    			if(code == KeyEvent.VK_RIGHT){
    				rightspeed = 2;
    			}
    			if(code == KeyEvent.VK_UP){
    				upspeed = 2;
    			}
    			if(code == KeyEvent.VK_DOWN){
    				downspeed = 2;
    			}
    		}
    		if((wait == false || gameOver) && code == KeyEvent.VK_H){
    			h = true;
    		}
    		if(code == KeyEvent.VK_SPACE && help == false){
    			if(gameOver && help == false){
    				cx = 30;
    				cy = 30;
    				ex = 350;
    				ey = 350;
    				score = 0;
    				gameOver = false;
    			}
    			if(wait == false){
    				wait = true;
    			}
    		}
    	}
    	public void keyTyped(KeyEvent e){}
    	public void keyReleased(KeyEvent e){
    		int code = e.getKeyCode();
    		if(code == KeyEvent.VK_LEFT){
    			leftspeed = 0;
    		}
    		if(code == KeyEvent.VK_RIGHT){
    			rightspeed = 0;
    		}
    		if(code == KeyEvent.VK_UP){
    			upspeed = 0;
    		}
    		if(code == KeyEvent.VK_DOWN){
    			downspeed = 0;
    		}
    		if(code == KeyEvent.VK_H){
    			if(h && help == false){
    				help = true;
    			}
    			else if(h && help){
    				help = false;
    			}
    			h = false;
    		}
    	}
     
    	//random methods
    	public void setRandom(int px, int py){
    		int x = r.nextInt(2);
    		if(x == 0){
    			if(px > 180){
    				bx = r.nextInt(176-(3*w))+w+2;
    			}
    			else{
    				bx = r.nextInt(200-(4*w))+(2*w)+182;
    			}
    			by = r.nextInt(380-(3*w))+w+2;
    		}
    		else{
    			if(py > 180){
    				by = r.nextInt(176-(3*w))+w+2;
    			}
    			else{
    				by = r.nextInt(200-(4*w))+(2*w)+182;
    			}
    			bx = r.nextInt(380-(3*w))+w+2;
    		}
    	}
    	public int getRandomX(int a, int b){
    		setRandom(a, b);
    		return bx;
    	}
    	public int getRandomY(int a, int b){
    		setRandom(a, b);
    		return by;
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Double Buffering Problem

    The call to super should pass g, not g2.
    That problem demonstrates how important good variable names are. The Graphics object for your image should have a unique name that wouldn't be confused with g.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Double Buffering Problem

    I tried that just now, but instead it flickers even more than before, and still doesn't paint the game over menu or the how to play menu. But it is different. When I passed g2 it froze instead of painting and when I passed g it painted a blank screen instead. Passing that variable instead just made the game worse, but maybe you are right and there is just another crucial part of the code missing. How come in the tutorial I used it didn't use the super line at all and still managed to change the image of where the red square was? Maybe that has to do with what I'm missing.

    --- Update ---

    I also noticed that the tutorial extended Applet instead of JApplet, which I used. I don't know how big of a difference this makes. If I need to make some major changes in the code, please let me know.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Double Buffering Problem

    Strange. I made that one change: super.paint(g); //2);
    and the code seems to execute ok.

    EDIT: The code worked several times. Now the screen is flashing continually???

    Try changing/removing different called methods that have to do with clearing the screen.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Double Buffering Problem

    When you ran it yourself, are you sure the entire program functioned perfectly? When I run it, besides flickering, the screen goes blank when it is supposed to display the game-over menu and it does the same for the how-to-play menu. Are you sure when you run it these features work perfectly? And I hardly know anything about double buffering. When you say to try changing/removing different methods for clearing the screen, which ones should I screw around with? Also, what are you running this in? I'm running it in Eclipse. I don't understand why it doesn't flicker when I pass g2 but it does with g. I also don't understand why neither paints the game-over or help menus. I am focused mainly on accessing these menus rather than the flickering because it is fixed when I pass g2.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Double Buffering Problem

    are you sure the entire program functioned perfectly?
    Sorry, I don't know how the program is supposed to function. There were 3 colored squares on start. Pressing the space bar caused one square to chase another. Pressing the arrows moved the chased square.

    which ones should I screw around with?
    Try them all one at a time.

    why neither paints the game-over or help menus.
    That's probably a logic problem with combinations of boolean variables. Try debugging the code by adding some println statements to print out the values of the variables as they are used and changed.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Double Buffering Problem

    I just now figured out that when I type g.clearRect(0, 0, 400, 400); it has the same effect of super.paint(g); Same goes for g2: passing g2 in super is the same as g2.clearRect(0, 0, 400, 400); clearRect() is what the tutorial used instead of super, but it didn't change anything. I know that the booleans work for accessing menus because it worked perfectly before I used double buffering. I'm trying to figure out why once I used double buffering I couldn't use the menus. As for how the program is supposed to function, it sounds like it ran correctly with you, but after the red square touches the green square there is supposed to be a game-over menu and all the squares return to their starting positions. Tell me what happened right after the green square caught the red one. Also in the beginning or in the game-over menu, you are supposed to be able to press "H" and the rules of the game show up. Please tell me if these features work because for me, they don't.

    I tried doing some println statements for the booleans and the gameOver and help variables did not change when I got caught by the green square or when I pressed h. The wait boolean worked perfectly, though.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Double Buffering Problem

    When space bar is pressed: The green square starts moving and the two rows of text at the bottom are cleared.
    The green square stops moving when the green square touches the red square.
    Pressing H does nothing.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Double Buffering Problem

    What's supposed to happen is when the green and red squares touch, it displays the same screen that appears in the beginning except that another line appears that says "GAME OVER" and instead of saying "PRESS [SPACE] TO PLAY" it is supposed to say "PRESS [SPACE] TO RESTART". The problem is that the booleans that activate these features for some reason don't change when they're supposed to. Do you know why double buffering could have changed this?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Double Buffering Problem

    I doubt that double buffering could change the values of boolean variables.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Double Buffering Problem

    Well, knowing that the variables are supposed to change when you press keys, maybe something is wrong in the portion of the code with action listeners and key listeners. I have no clue why the booleans aren't changing.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Double Buffering Problem

    Try debugging the code by adding printlns to print the values of the variables as they are used and their values changed.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Double Buffering Problem

    I already did that and I realized that the booleans that activate the menus that don'e appear are not being activated when they're supposed to. In the code, do you see anything wrong with the variables gameOver and help being activated. (Help should be activated when you type "h" and gameOver is true or wait is false. gameOver should be activated in actionPerformed() when you touch either the green or blue squares.)

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Double Buffering Problem

    If there is a logic problem with the setting of variables, then you need to try debugging the code to sort out the logic.
    The way I debug is by adding lots of println statements to show what is happening.

    The code is hard to understand because there are no comments describing its logic. For example: what is the h variable used for? How does its value relate to the value of the help variable?
    Why set some values on key press and others on key release?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Double Buffering Problem

    I am debugging the code now. I'm adding comments, but I'm not updating the code here. The "h" variable is true when the h key is down and false when it is released. This variable is working successfully. When you press and release it, it's supposed to toggle the variable "help" which activates the help menu. The problem for the help menu is that "help" is not changing but "h" is. I can't figure out what's wrong with it.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Double Buffering Problem

    "help" is not changing
    Add printlns to show all the values that control the setting of the help variable so you can see the values that the computer sees.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    beansnbacon (March 30th, 2013)

  18. #17
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Double Buffering Problem

    I fixed it! The one mistake I made indeed involved the double buffering. It was not a logic problem in the code. The problem was that the line g.drawImage(offscreen, 0, 0, this); was included in an if statement. So it only painted the image when there was not help or game over menu. I am so relieved I finally figured the problem out. And I actually was supposed to pass g2 in the super.paint();

Similar Threads

  1. 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
  2. Jar Embedded in Website: Double Buffering Issue?
    By Staticity in forum AWT / Java Swing
    Replies: 6
    Last Post: July 17th, 2012, 02:43 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