2014-12-07 1 views
3

나는이 간단한 서문을 작성하기 위해 내가 만든 간단한 게임에 애니메이션을 추가하려고합니다. 나는 애니메이션을 테스트하기 위해 별도의 클래스를 만들었고 두 개를 제외하고는 모든 이미지를 표시했습니다. 내 현재 코드입니다 : 내가 그러나 일식 힙 공간을 증가하고 배가 얼마나 고개를 한자바 이미지를 가져 오는 힙 공간 문제

Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space 
Exception in thread "Image Fetcher 2" java.lang.OutOfMemoryError: Java heap space 

: 내가 말하는 두 가지 오류를 얻고있다

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.net.URL; 

import javax.swing.JApplet; 

public class GooAnimation extends JApplet implements Runnable { 

Image[] eastbarf = new Image[22]; 
Image[] east2barf = new Image[10]; 
boolean enter = false; 
int counter = 0; 
Image img1; 
URL url; 

public void init() { 

    eastbarf[0] = getImage(getDocumentBase(), "eastbarf1.png"); 
    eastbarf[1] = getImage(getDocumentBase(), "eastbarf2.png"); 
    eastbarf[2] = getImage(getDocumentBase(), "eastbarf3.png"); 
    eastbarf[3] = getImage(getDocumentBase(), "eastbarf4.png"); 
    eastbarf[4] = getImage(getDocumentBase(), "eastbarf5.png"); 
    eastbarf[5] = getImage(getDocumentBase(), "eastbarf6.png"); 
    eastbarf[6] = getImage(getDocumentBase(), "eastbarf7.png"); 
    eastbarf[7] = getImage(getDocumentBase(), "eastbarf8.png"); 
    eastbarf[8] = getImage(getDocumentBase(), "eastbarf9.png"); 
    eastbarf[9] = getImage(getDocumentBase(), "eastbarf10.png"); 
    eastbarf[10] = getImage(getDocumentBase(), "eastbarf11.png"); 
    eastbarf[11] = getImage(getDocumentBase(), "eastbarf12.png"); 
    eastbarf[12] = getImage(getDocumentBase(), "eastbarf13.png"); 
    eastbarf[13] = getImage(getDocumentBase(), "eastbarf14.png"); 
    eastbarf[14] = getImage(getDocumentBase(), "eastbarf15.png"); 
    eastbarf[15] = getImage(getDocumentBase(), "eastbarf16.png"); 
    eastbarf[16] = getImage(getDocumentBase(), "eastbarf17.png"); 
    eastbarf[17] = getImage(getDocumentBase(), "eastbarf18.png"); 
    eastbarf[18] = getImage(getDocumentBase(), "eastbarf19.png"); 
    eastbarf[19] = getImage(getDocumentBase(), "eastbarf20.png"); 
    eastbarf[20] = getImage(getDocumentBase(), "eastbarf21.png"); 
    eastbarf[21] = getImage(getDocumentBase(), "eastbarf22.png"); 

    east2barf[0] = getImage(getDocumentBase(), "east2barf1.png"); 
    east2barf[1] = getImage(getDocumentBase(), "east2barf2.png"); 
    east2barf[2] = getImage(getDocumentBase(), "east2barf3.png"); 
    east2barf[3] = getImage(getDocumentBase(), "east2barf4.png"); 
    east2barf[4] = getImage(getDocumentBase(), "east2barf5.png"); 
    east2barf[5] = getImage(getDocumentBase(), "east2barf6.png"); 
    east2barf[6] = getImage(getDocumentBase(), "east2barf7.png"); 
    east2barf[7] = getImage(getDocumentBase(), "east2barf8.png"); 
    east2barf[8] = getImage(getDocumentBase(), "east2barf9.png"); 
    east2barf[9] = getImage(getDocumentBase(), "east2barf10.png"); 

} 

@Override 
public void run() { 
    while (true) { 
     repaint(); 
     try { 
      Thread.sleep(1000); 

     } catch (InterruptedException e) { 

     } 
    } 

} 

@Override 
public void start() { 
    Thread thread = new Thread(this); 
    thread.start(); 
} 

@Override 
public void paint(Graphics g) { 
    // for (int i = 0; i < eastbarf.length; i++) { 
    if (enter) { 
     if (counter < 10) { 
      g.setColor(Color.WHITE); 
      g.drawRect(0, 0, 10000, 10000); 
      g.fillRect(0,0,10000,10000); 
      g.drawImage(east2barf[counter], 300, 0, 1000, 1000, this); 
      System.out.println(eastbarf[counter].getHeight(null)); 
     } 
     if (counter == 10) { 
      counter = 0; 
      enter = false; 
     } 
    } 
    if (!enter) { 
     if (counter < 22) { 
      // if ((i - 1)%2 == 1) { 
      // 

      // } 
      System.out.println(counter); 
      if (eastbarf[counter] == null) { 
       System.out.println("Its null!"); 
      } 
      // if (counter % 2 == 1) { 
      // g.setColor(Color.YELLOW); 
      // } 
      // else { 
      // g.setColor(Color.MAGENTA); 
      // } 
      g.setColor(Color.WHITE); 
      g.drawRect(0, 0, 10000, 10000); 
      g.fillRect(0,0,10000,10000); 
      g.drawImage(eastbarf[counter], 0, 0, 1000, 1000, this); 
      System.out.println(eastbarf[counter].getHeight(null)); 

      // } 
     } 

     // } 

     if (counter > 21) { 
      enter = true; 
      counter = 0; 
     } 
    } 
    counter += 1; 
    // g.drawImage(img1, 0, 0, null); 


} 

}

. 나는 그것이 적절한 지 확신하지 못한다. 이상적으로 같은 길이의 4 barf 목록과 4 2 barf 목록을 가지고 있습니다. 보시다시피, 저는 개념화를 쉽게하기 위해 그림의 모든 이름을 구분했습니다. 나는 꽤 새롭고 모범 사례 이미지가 현명하거나 JApplet/Swing에 대해 알지 못합니다. 왜 누군가가 두 이미지를 잃어 버리고 있는지 알아내는 데 도움을 주시면 감사하겠습니다. 누락 된 이미지는 eastbarf2의 여섯 번째와 여덟 번째 이미지입니다. 정말 도움이됩니다. 감사합니다 편집 : 이미지는 약 80-150 kb입니다. 이클립스에서 이것은 실행 구성의 인수 부분에 넣은 명령입니다. -Xms1024M -Xmx2048M. 그들은 아무것도 바뀌지 않는 것처럼 보였다.

+1

얼마나 큰 이미지입니까? – BitNinja

+1

힙 크기는 얼마나됩니까? [프로필] (http://stackoverflow.com/q/2064427/230513) 자세한 내용은. – trashgod

+0

나는 정보를 추가했다, trashgod, 당신은 단지 이것을 사용하는 것이 문제를 해결할 것이라고 생각합니까? – eweiner

답변

1
g.drawRect(0, 0, 10000, 10000); 
g.fillRect(0,0,10000,10000); 
g.drawImage(east2barf[counter], 300, 0, 1000, 1000, this); 

당신은 화면에 10,000 X 10,000 픽셀을 작성하고, 그 후 당신은 X 1000은 확실히 당신의 기억을 날려 1000 크기의 이미지를 그립니다. 게임에 큰지도가 필요한 경우 플레이어가있는 화면 만 렌더링하는 직각 카메라로 타일셋 맵을 구현해야합니다. 예를 들어 은 모든 이미지를 BufferedImage으로 변경하고 다음을 시도합니다.

[...] .]

g.drawRect(0, 0, 800, 600); 
    g.fillRect(0,0,800,600); 
    g.drawImage(east2barf[counter], 300, 0, east2barf[counter].getWidth(), east2barf[counter].getHeight(), this); 

[...]

 g.setColor(Color.WHITE); 
     g.drawRect(0, 0, 800, 600); 
     g.fillRect(0,0,800,600); 
     g.drawImage(eastbarf[counter], 0, 0, east2barf[counter].getWidth(), east2barf[counter].getHeight(), this); 

[...]