By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,246 Members | 2,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,246 IT Pros & Developers. It's quick & easy.

problem with flickering graphics

100+
P: 112
Hi, i have made several different things using a double buffer and a jframe before but never had this problem:

if you look thrugh the code, you can see that i draw a whole bunch of stuff to the screen and sometimes, i get a frame or two of just blank jframe color
Expand|Select|Wrap|Line Numbers
  1. public void draw()
  2.     {
  3.         BufferStrategy bf = this.getBufferStrategy();
  4.         Graphics2D g = null;
  5.         if(scrollPos>lastCoveredPos) lastCoveredPos = initializeRandomObjects();           
  6.         try {
  7.             g = (Graphics2D)bf.getDrawGraphics();
  8.             g.clearRect(0, 0, getWidth(), getHeight());
  9.             //draw with g here            
  10.             g.drawImage(background,(0-scrollPos)%(getWidth()*2),0, this);
  11.             if(scrollPos%(getWidth()*2)>=getWidth())
  12.             {                
  13.                 g.drawImage(background,getWidth()-(scrollPos%(getWidth())),0,this);
  14.             }
  15.             //draw the ACTOR                        
  16.             AffineTransform at = new AffineTransform();
  17.             at.translate(actor.getX()-scrollPos, actor.getY()-actorSize);            
  18.             at.rotate(Math.toRadians(r),actorSize/2.0, actorSize/2.0);
  19.             r%=360;
  20.             if(actor.getSpeedX()!=0)
  21.                 r+=actor.getSpeedX();
  22.             if(fired)
  23.                 g.drawImage(chicken, at, this);  
  24.             //draw the CANNON ARM
  25.             AffineTransform at2 = new AffineTransform();
  26.             at2.translate(-50-scrollPos, groundY-cannonarm.getHeight(this));            
  27.             at2.rotate(Math.toRadians(-1*cannonAngle),240, 55);
  28.             g.drawImage(cannonarm,at2,this);
  29.             //draw the CANNON
  30.             g.drawImage(cannonbase, -50-scrollPos, groundY-cannonbase.getHeight(this), this);            
  31.             //draw the GROUND
  32.             g.drawLine(0, groundY, this.getWidth(), groundY);                
  33.             //draw debug and STAT INFO
  34.             g.drawString("Distance Travelled: "+(distanceTravelled)+" ft.", 0, groundY+g.getFont().getSize());
  35.             //draw the POWER BAR                       
  36.             g.drawRect(20, getHeight()-20, 90, 20);
  37.             g.setColor(Color.RED);
  38.             g.fillRect(20, getHeight()-20, cannonPower*2-10, 20);
  39.             g.setColor(Color.WHITE);
  40.             g.drawString("POWER", 25, getHeight()-20);
  41.             g.setColor(Color.BLACK);
  42.             if(drawMax)
  43.             {
  44.                 g.setColor(Color.YELLOW);
  45.                 g.drawString("MAX POWER!!", 25, getHeight()+5-g.getFont().getSize());
  46.                 g.setColor(Color.black);
  47.             }
  48.             //if the character moves outside of the fram, draw his height
  49.             if(actor.getY()<0)
  50.             {
  51.                 g.fillRoundRect(0,15,110,60,10, 50);
  52.                 g.setColor(Color.WHITE);
  53.                 g.fillRoundRect(5, 20, 100,50, 10, 50);
  54.                 g.setColor(Color.BLACK);
  55.                 g.drawString("Height: "+(-1*actor.getY()+getHeight()-(getHeight()-groundY)), 15, 50);
  56.             }
  57.             //draw the objects
  58.             if(objects!=null)
  59.                 for(OtherObjects x : objects)
  60.                 {                    
  61.                     g.drawImage(x.getImage(), x.getX()-scrollPos, x.getY(),this);
  62.                 }
  63.                         //kickin chicken leg
  64.                 if(kickinChicken)
  65.                 {
  66.                     g.drawImage(leg, actor.getX()-scrollPos-leg.getWidth(this), actor.getY()-leg.getHeight(this),this);                    
  67.                 }
  68.             //debug string
  69.                 g.drawString("actor x: "+actor.getX()+"    actor y: "+actor.getY(),20,20);
  70.         }catch(Exception e){e.printStackTrace();}
  71.         finally {
  72.             // It is best to dispose() a Graphics object when done with it.
  73.             g.dispose();
  74.         }
  75.  
  76.         // Shows the contents of the backbuffer on the screen.
  77.         bf.show();
  78.         //Tell the System to do the Drawing now, otherwise it can take a few extra ms until 
  79.         //Drawing is done which looks very jerky
  80.         Toolkit.getDefaultToolkit().sync();    
  81.     }
every time i make a call to a method that generates for 10-100 new objects and adds them to a vector of objects to be rendered. how can i avoid thiese dropped frames?

thanks,
ken
Nov 30 '08 #1
Share this Question
Share on Google+
2 Replies


Expert 10K+
P: 11,448
This doesn't answer your question but if you use Swing you are already using double buffers; no need to use another off-screen buffer.

kind regards,

Jos
Nov 30 '08 #2

100+
P: 112
oh...lol good to know.

i think the problem may be when it does all the calculations for generating and creating the new objects, maybe the refresh thread catches up with the method that is generating the objects???

even if that was the case, how would i go about fixing something like that?
Nov 30 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.