problem with flickering graphics

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drsmooth
    New Member
    • Oct 2007
    • 112

    problem with flickering graphics

    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
    [CODE=java]
    public void draw()
    {
    BufferStrategy bf = this.getBufferS trategy();
    Graphics2D g = null;
    if(scrollPos>la stCoveredPos) lastCoveredPos = initializeRando mObjects();
    try {
    g = (Graphics2D)bf. getDrawGraphics ();
    g.clearRect(0, 0, getWidth(), getHeight());
    //draw with g here
    g.drawImage(bac kground,(0-scrollPos)%(get Width()*2),0, this);
    if(scrollPos%(g etWidth()*2)>=g etWidth())
    {
    g.drawImage(bac kground,getWidt h()-(scrollPos%(get Width())),0,thi s);
    }
    //draw the ACTOR
    AffineTransform at = new AffineTransform ();
    at.translate(ac tor.getX()-scrollPos, actor.getY()-actorSize);
    at.rotate(Math. toRadians(r),ac torSize/2.0, actorSize/2.0);
    r%=360;
    if(actor.getSpe edX()!=0)
    r+=actor.getSpe edX();
    if(fired)
    g.drawImage(chi cken, at, this);
    //draw the CANNON ARM
    AffineTransform at2 = new AffineTransform ();
    at2.translate(-50-scrollPos, groundY-cannonarm.getHe ight(this));
    at2.rotate(Math .toRadians(-1*cannonAngle), 240, 55);
    g.drawImage(can nonarm,at2,this );
    //draw the CANNON
    g.drawImage(can nonbase, -50-scrollPos, groundY-cannonbase.getH eight(this), this);
    //draw the GROUND
    g.drawLine(0, groundY, this.getWidth() , groundY);
    //draw debug and STAT INFO
    g.drawString("D istance Travelled: "+(distanceTrav elled)+" ft.", 0, groundY+g.getFo nt().getSize()) ;
    //draw the POWER BAR
    g.drawRect(20, getHeight()-20, 90, 20);
    g.setColor(Colo r.RED);
    g.fillRect(20, getHeight()-20, cannonPower*2-10, 20);
    g.setColor(Colo r.WHITE);
    g.drawString("P OWER", 25, getHeight()-20);
    g.setColor(Colo r.BLACK);
    if(drawMax)
    {
    g.setColor(Colo r.YELLOW);
    g.drawString("M AX POWER!!", 25, getHeight()+5-g.getFont().get Size());
    g.setColor(Colo r.black);
    }
    //if the character moves outside of the fram, draw his height
    if(actor.getY() <0)
    {
    g.fillRoundRect (0,15,110,60,10 , 50);
    g.setColor(Colo r.WHITE);
    g.fillRoundRect (5, 20, 100,50, 10, 50);
    g.setColor(Colo r.BLACK);
    g.drawString("H eight: "+(-1*actor.getY()+ getHeight()-(getHeight()-groundY)), 15, 50);
    }
    //draw the objects
    if(objects!=nul l)
    for(OtherObject s x : objects)
    {
    g.drawImage(x.g etImage(), x.getX()-scrollPos, x.getY(),this);
    }
    //kickin chicken leg
    if(kickinChicke n)
    {
    g.drawImage(leg , actor.getX()-scrollPos-leg.getWidth(th is), actor.getY()-leg.getHeight(t his),this);
    }
    //debug string
    g.drawString("a ctor x: "+actor.getX()+ " actor y: "+actor.getY(), 20,20);
    }catch(Exceptio n e){e.printStack Trace();}
    finally {
    // It is best to dispose() a Graphics object when done with it.
    g.dispose();
    }

    // Shows the contents of the backbuffer on the screen.
    bf.show();
    //Tell the System to do the Drawing now, otherwise it can take a few extra ms until
    //Drawing is done which looks very jerky
    Toolkit.getDefa ultToolkit().sy nc();
    }[/CODE]

    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
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • drsmooth
      New Member
      • Oct 2007
      • 112

      #3
      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?

      Comment

      Working...