2016-07-19 9 views
0

paintComponentの間に私のonFrameメソッドがオブジェクトの位置を変更している問題があります。私のJPanelはオブジェクトfocusの位置を基準にしてシーンをレンダリングするので、ペイント中にフォーカス位置が移動すると、シーンの一部が残りのシーンから変換されます(これはプレーヤに白い線で見えますスクリーン)。したがってpaintComponentの部分でfocusが変更されないようにしたいと思います。ここで paintComponent(Java)中の更新を防止する

は塗料コンポーネントです:

renderメソッドが呼び出されて
@Override 
public void paintComponent(Graphics g) { 

    if (focus == null) { 
     return; 
    } 

    super.paintComponent(g); 
    synchronized (focus) { 
     universe.getCurrentWorld().render(this, g, true); //draws scene around focus 
    } 
    focus.getInventory().render(this, g); 
    focus.renderHealthBar(g); 

    if (dialogQ.peek() != null) { 
     dialogQ.peek().render(this, g); 
    } 

} 

public void render(FocusedWindow g, Graphics gr, boolean showOutlands) { 

    level.renderMap(g, gr, showOutlands); 
    col.renderAround(g, gr); 

    for (Detector p : colliders.toArray(new Detector[0])) { 
     p.renderAround(g, gr); 
    } 

} 

レベルが実際にfocus使用方法レンダリング:

public void renderMap(FocusedWindow g, Graphics gr, boolean renderOutside) { 
    int cushion = 4; 
    int scaleFactor = 2; 
    int minX = g.getFocus().getPos().getX()/Tile.WIDTH - g.getDimensions().getX()/Tile.WIDTH/scaleFactor - cushion; 
    int maxX = g.getFocus().getPos().getX()/Tile.WIDTH + g.getDimensions().getX()/Tile.WIDTH/scaleFactor + cushion; 
    int minY = g.getFocus().getPos().getY()/Tile.WIDTH - g.getDimensions().getY()/Tile.WIDTH/scaleFactor - cushion; 
    int maxY = g.getFocus().getPos().getY()/Tile.WIDTH + g.getDimensions().getX()/Tile.WIDTH/scaleFactor + cushion; 

    for (int i = minX; i < maxX; i++) { 
     for (int j = minY; j < maxY; j++) { 
      if (i >= 0 && i < map.length) { 
       if (j >= 0 && j < map[i].length) { 
        map[i][j].drawTile(g, gr, new Vector(i,j)); 
       } 
       else if (renderOutside) { 
        map[0][0].drawTile(g, gr, new Vector(i,j)); 
       } 
      } 
      else if (renderOutside) { 
       map[0][0].drawTile(g, gr, new Vector(i,j)); 
      } 
     } 
    } 

} 
+0

'同期 'ブロック内で' focus'が使用されていません。私はそれがすぐ下で使用されて参照してください。同期ブロックをそれらのステートメントに移動させてください –

+0

すぐに役立つようにするには、[MCVE]または[ショート、自己完結型、正しい例](http://www.sscce.org/)を投稿してください。 –

+0

@VinceEmigh 'universe.getCurrentWorld()。render'メソッドは渡されたウィンドウからフォーカスを受け取ります(この場合は' this') –

答えて

0

ああ、私もする必要が世界を更新するonFrameのコードを同期させてください。 2つの同期ブロックを追加した後、正常に動作します。

@Override 
protected void onFrame() { 

    long time = getTimestamp(); 
    double deltaTime = (time - lastTime)/1000000000d; 
    lastTime = time; 

    if (deltaTime > 0.2) { //this is shitty 
     Main.log("high delta time detected (" + deltaTime + " sec)"); 
    } 

    if (dialogQ != null && dialogQ.peek() != null && dialogQ.peek().update(deltaTime)) { 
     dialogQ.poll(); 
    } 

    if (isPaused() || isEditingInventory()) { 
     return; 
    } 

    if (!hasDied && focus != null && focus.isDead()) { 
     hasDied = true; 
     Main.setOverlay(new DeathOverlay()); 
    } 

    if (universe == null || universe.getCurrentWorld() == null) { 
     return; 
    } 

    repaint(); 
    synchronized (this) { 
     universe.getCurrentWorld().update(deltaTime); 
    } 

} 

@Override 
public void paintComponent(Graphics g){ 

    if (focus == null) { 
     return; 
    } 

    super.paintComponent(g); 
    synchronized (this) { 
     universe.getCurrentWorld().render(this, g, true); 
    } 
    focus.getInventory().render(this, g); 
    focus.renderHealthBar(g); 

    if (dialogQ.peek() != null) { 
     dialogQ.peek().render(this, g); 
    } 

} 
関連する問題