2012-03-04 5 views
2

학교에서 큰 독점 게임을하고 있는데 큰 성능 문제가 발생했습니다. 지금은 전화가 걸릴 때마다 전체 보드를 그리는 페인트 방법이 있습니다 ... 보드가 처음에는 한 번만 그려지기 때문에 누군가가 집이나 다른 것을 구입할 때만 큰 문제입니다. 가장 많이 쓰고 싶은 부분은 플레이어입니다. 왜냐하면 가장 많이 움직이고 페인트해야하기 때문입니다. 여기 자바 스윙 게임 보드

내 보드의 페인트 방법 : 보드를 대표하는 텍스트의

public void paint(Graphics g){ 
     super.paint(g); 
     Graphics2D g2d = (Graphics2D)g; 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     //Draw all the spots 
     for(int i = 0; i < spots.length; i++){ 
      g2d.setColor(Color.white); 
      g2d.fill(spots[i].getRect()); 
      if(spots[i] instanceof Property){ 
       if(i < 10){ 
        g2d.setColor(spots[i].getColor()); 
        Rectangle temp = new Rectangle(spots[i].getRect().x,spots[i].getRect().y,spots[i].getRect().width,spots[i].getRect().height/3); 
        g2d.fill(temp); 
        g2d.setColor(Color.black); 
        g2d.drawString(((Property)spots[i]).getName(), spots[i].getRect().x, spots[i].getRect().height); 
       }else if(i >= 10 && i < 20){ 
        g2d.setColor(spots[i].getColor()); 
        Rectangle temp = new Rectangle(spots[i].getRect().x+spots[i].getRect().width-spots[i].getRect().width/3,spots[i].getRect().y,spots[i].getRect().width/3,spots[i].getRect().height); 
        g2d.fill(temp); 
       }else if(i >= 20 && i < 30){ 
        g2d.setColor(spots[i].getColor()); 
        Rectangle temp = new Rectangle(spots[i].getRect().x,spots[i].getRect().y+spots[i].getRect().height-spots[i].getRect().height/3,spots[i].getRect().width,spots[i].getRect().height/3); 
        g2d.fill(temp); 
        g2d.setColor(Color.black); 
        g2d.drawString(((Property)spots[i]).getName(), spots[i].getRect().x, spots[i].getRect().y); 
       }else if(i >= 30 && i < 40){ 
        g2d.setColor(spots[i].getColor()); 
        Rectangle temp = new Rectangle(spots[i].getRect().x,spots[i].getRect().y,spots[i].getRect().width/3,spots[i].getRect().height); 
        g2d.fill(temp); 
       } 
      }else if(spots[i] instanceof Railroad){ 
       if(i == 5) 
        g2d.drawImage(imgTrain3, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/4, null); 
       else if(i == 15) 
        g2d.drawImage(imgTrain4, spots[i].getRect().x+spots[i].getRect().width/4, spots[i].getRect().y, null); 
       else if(i == 25) 
        g2d.drawImage(imgTrain1, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/4, null); 
       else if(i == 35) 
        g2d.drawImage(imgTrain2, spots[i].getRect().x+spots[i].getRect().width/4, spots[i].getRect().y, null); 
      }else if(spots[i] instanceof Chance){ 
       if(i == 7) 
        g2d.drawImage(imgChance2, spots[i].getRect().x, spots[i].getRect().y, null); 
       else if(i == 22) 
        g2d.drawImage(imgChance2, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/3, null); 
       else if(i == 36) 
        g2d.drawImage(imgChance3, spots[i].getRect().x, spots[i].getRect().y, null); 
      }else if(spots[i] instanceof Community){ 
       if(i == 2) 
        g2d.drawImage(imgComm1, spots[i].getRect().x, spots[i].getRect().y+spots[i].getRect().height/3, null); 
       else if(i == 17) 
        g2d.drawImage(imgComm2, spots[i].getRect().x, spots[i].getRect().y, null); 
       else if(i == 33) 
        g2d.drawImage(imgComm3, spots[i].getRect().x+spots[i].getRect().width/3, spots[i].getRect().y, null); 
      }else{ 
       g2d.setColor(spots[i].getColor()); 
       g2d.fill(spots[i].getRect()); 
      } 
     } 
     //Draw the outline of every spot 
     g2d.setColor(Color.black); 
     for(Spot index : spots) 
      g2d.draw(index.getRect()); 
     //Draw the outline of the whole board 
     g2d.drawRect(newX, newY, boardSize, boardSize); 
     //Draw the Players location 
     for(Player index : players) 
      g2d.drawImage(index.getImage(), index.getLoc().x, index.getLoc().y, null); 
    } 

기본적으로 톤, 이것은 매번 보드 다시 그리기를 수행하고있다. 어떤 충고?

보너스 질문 : 플레이어가 굴러 난 후 (현재는 대상으로 바로 이동) 이동 애니메이션을 작업하기 시작했습니다. 1 초마다 1 초 걸리는 타이머를 만들었습니다 (예 : 5 초를 굴리는 경우 5 초가 걸렸습니다). 문제는 시작 지점에서 끝까지 플레이어 조각이 느리게 움직이는 것을 보여주는 좋은 아이디어가 없다는 것입니다. 나는 누군가에게 기본적인 생각을주기 위해 올바른 방향으로 나아갈 수 있습니다.

+0

+1, 굉장합니다. 나는 단지 나를 시작하게하는 아이디어를 찾고 있었다. 독점 게임은 나의 새로운 프로젝트에 대해 뛰어나다. – ApprenticeHacker

+0

감사! 그것은 모두가 함께 할 때 확실히 개발하고 지켜 보는 재미있는 게임입니다. – mbreen

답변

4

BufferedImage에 보드를 칠하십시오. 페인트 방법에서 보드의 이미지를 그린 다음 위에 조각을 그립니다.

BTW - 스윙을 사용할 때 최상위 컨테이너에 페인트하지 말고 대신 JComponent 또는 JPanel을 사용하십시오. 후자의 두 경우에는 paint(Graphics) 대신 paintComponent(Graphics)을 무시하십시오.

+0

그래도 모든 페인트 호출마다 보드가 그려 지지만 이미지이기 때문에 보드가 더 효율적입니까? 현재 플레이어가 움직일 때 시각적 지연을 볼 수 있습니다. BufferedImage에서 눈에 띄지 않습니다? – mbreen

+1

@mbreen : 사용해보기 :-). 네, 자바 윈도우가 다른 윈도우에 의해 가려 질 때를 대비하여 가끔 보드 전체를 그릴 필요가 있습니다. –

+0

BG를 그릴 때까지 오랜 시간이 걸린다면 그렇습니다. 그러나 눈에 띄는 지연없이 페인트 메소드에 대한 호출 당 수백 개의 객체를 가져올 수 있어야합니다. 아마도 [SSCCE] (http://sscce.org/)를 게시하는 것이 더 좋을 것이므로 사용되는 전체 프로세스를 더 자세히 볼 수 있습니다. 또한, toto2가 찾은 튜토리얼에서 렌더링 예제를 쫓아내는 것이 좋습니다. 그림 1에서 볼 (선, .. 무엇이든)을 그림의 점수로 늘려보고 어떻게 수행하는지보십시오. –

1

jComponent.repaint(someRectangle)으로 전체 표면을 다시 그리지 않아도됩니다. 이 내용은 example을 참조하십시오.

그러나 각 동작 사이에 업데이트해야하는 사각형 (또는 직사각형)을 직접 계산해야합니다. 새로운 것들이 나타나는 곳에 직사각형이 필요할뿐만 아니라 사라지는 곳도 필요합니다.

스윙의 애니메이션은 tutorial입니다. 이 게시물에 링크 된 두 예가 모두 Swing tutorial의 공식임을 알 수 있습니다.