3

To preface this I am a beginner programmer wishing to add an animation to a simple game that I have made. I made a separate class to test my animations, and every image displayed except for two. My current code is:

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);


}

}

I am getting two errors that say:

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

I have however looked up how to increase eclipse heap space and have doubled it. I am not sure if that is adequate. I would ideally have 4 barf lists of the same length and 4 2barf lists. As you can see, I have separated all of the names of the pictures out, for easier conceptualization. I am pretty new and am not aware of best practices image wise or JApplet/Swing stuff. I would appreciate it if someone could help figure out why I am losing out on two images. The images that are missing are the sixth and eighth image in eastbarf2. I would really appreciate some help. Thanks Edit: The images are around 80-150 kb each. In eclipse these are the commands I put in the arguments part of the run configurations. -Xms1024M -Xmx2048M. They did not appear to change anything.

eweiner
  • 37
  • 6
  • 1
    How large are the images? – BitNinja Dec 07 '14 at 23:02
  • 1
    How large is the heap? [Profile](http://stackoverflow.com/q/2064427/230513) for details. – trashgod Dec 07 '14 at 23:10
  • I added the information, trashgod, do you think that just using these would solve the problem? – eweiner Dec 08 '14 at 22:40
  • 1
    as @trashgod mentioned profile your application to find out why your heap size is being eaten. NetBeans has a very good [profiler](https://profiler.netbeans.org/) included with it. I believe it uses the _jvisualvm_ under the hood. With a profiler, you can try to find where many objects are being created, when objects get garbage collected, and more – AR5HAM Dec 09 '14 at 00:02
  • @Arsham I am using Eclipse on a mac, is there an equivalent? – eweiner Dec 09 '14 at 12:43
  • you can directly use jvisualvm since it is java based it should run on MAC. I know APPLE is weird and they install their own version of java on the machine, but theoretically it should work. – AR5HAM Dec 13 '14 at 01:25
  • @Arsham your info about Java on Mac OS X is out of date. As of Yosemite Apple does not supply Java, although they still make it available as "Legacy Java". Oracle now supplies Java for OS X. And it includes jvisualvm – Steve McLeod May 29 '15 at 04:47
  • Where do you call init()? I suspect you are calling it multiple times, leading to new instances of the images filling up memory. You should also post the code for getImage() – Steve McLeod May 29 '15 at 04:49

1 Answers1

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

you are filling 10k x 10k pixels on the screen and after that you draw the images with size of 1000 x 1000 it surely will blow your memory. If you want a big map for your game you must implement an tileset map with orthogonal camera that will render only the screen the player are in, for instance change all Image to BufferedImage and just try that:

[...]

    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);

[...]

Hllink
  • 918
  • 5
  • 17