This site requires JavaScript, please enable it in your browser!
Greenfoot back
GRIFFIN
GRIFFIN wrote ...

2014/5/13

java.lang.OutOfMemoryError: Java heap space

1
2
3
GRIFFIN GRIFFIN

2014/5/13

#
I am making a game in which there is a health bar for actor. Basically what I'm doing is creating an image, setting the X value to the health, and when the health gets lowered, the image gets smaller. After that I move the image to the left so it covers the space on the left side. It was working fine until a few hours ago, when I made some other changes to my game, (which is pretty big), and it gave an error. I have already tried not making a new image every time but that still doesn't work. Here is the code for the health bar:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class Healthbar extends Tank
{
    double RealHealth = 0, MaxHealth = 0;
    int perc = 0;
    double small;
    Tank theTank;
    boolean once = true;
    int currentX = 0;
    
    
    public Healthbar(Tank partheTank){
        super("",200);
        //where = parWhere;
        theTank = partheTank;   
        
    }
    public void act() 
    {
        if(once == true){
          
            MaxHealth = theTank.maxHealth;
            RealHealth = theTank.realHealth;
            getWorld().addObject(new Outline(0), getX(), getY());
            currentX = getX();
            once = false;
        }
        setStuff();
        checkDead();
        if(!dead){
            setImage();
        }
    }      
    public void checkDead(){
        if(perc <= 1){
            theTank.dies();
        }
    }
    public void setStuff(){
        MaxHealth = (double)theTank.maxHealth;
        RealHealth = (double)theTank.realHealth;          
        small = RealHealth/MaxHealth;          
        perc = (int) (small * 200);
        int num = 200-perc;
        setLocation((currentX)-num/2, getY());
    }
    public void setImage(){
        GreenfootImage image = new GreenfootImage(perc, 15);//This is the line that brings the error
        if(perc >= 100){
            image.setColor(java.awt.Color.GREEN);
        }
        else if(perc < 100&& perc > 70){
           image.setColor(java.awt.Color.YELLOW); 
        }
        else if(perc <= 70 && perc > 50 ){
            Color color = new Color(255,60,30);
            image.setColor(color);
        }
        else if(perc <= 50){
           image.setColor(java.awt.Color.RED);  
        }
        image.fill();
        setImage(image);
    }
}
Thanks for help in advance!
danpost danpost

2014/5/14

#
First of all, you cannot say that a healthbar is a specific type of tank; therefore, Healthbar should not extend Tank. Only if you can truthfully say that one object is a specific type of another, should the one extend the other. Secondly, there is an 'addedToWorld' method you can use to perform your one-time actions. Finally, you should only update the image of the healthbar when the value of the 'realHealth' of the tank changes. However, none of the above would contribute to your memory problem (at least, not by itself). You should look at the areas where you were adding/changing things (not it something that was already working).
davmac davmac

2014/5/14

#
It would be helpful if you post the actual stack trace that you get.
GRIFFIN GRIFFIN

2014/5/14

#
Here is the error:
java.lang.OutOfMemoryError: Java heap space
	at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:75)
	at java.awt.image.Raster.createPackedRaster(Raster.java:470)
	at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
	at java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:186)
	at greenfoot.util.GraphicsUtilities.createCompatibleTranslucentImage(GraphicsUtilities.java:179)
	at greenfoot.GreenfootImage.<init>(GreenfootImage.java:131)
	at Healthbar.setImage(Healthbar.java:53)
	at Healthbar.act(Healthbar.java:36)
	at greenfoot.core.Simulation.actActor(Simulation.java:568)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2014/5/14

#
Try adding as the first statement in the 'setImage' method the following line and report back:
if (perc == 0) perc = 1;
I am wondering if the problem could somehow be related to its value being zero. If that fixes the problem, change that line to this:
if (perc == 0) return;
GRIFFIN GRIFFIN

2014/5/14

#
I tried both of those but unfortunately, they didn't work. Was this what you trying to get me to do?
public void setImage(){  
        if(perc == 0) perc = 1;
        GreenfootImage image = new GreenfootImage(perc, 15);//This is the line that brings the error  
        if(perc >= 100){  
            image.setColor(java.awt.Color.GREEN);  
        }  
        else if(perc < 100&& perc > 70){  
           image.setColor(java.awt.Color.YELLOW);   
        }  
        else if(perc <= 70 && perc > 50 ){  
            Color color = new Color(255,60,30);  
            image.setColor(color);  
        }  
        else if(perc <= 50){  
           image.setColor(java.awt.Color.RED);    
        }  
        image.fill();  
        setImage(image);  
    }
danpost danpost

2014/5/15

#
Yes. Ok. Well, I see that was pointless (I just realized that the method is not called when the value of 'perc' is zero or less). Do this and report back. Run the scenario to the error, then right click on the healthbar and select 'Inspect' from the pop-up menu. Report back the value of 'perc'.
GRIFFIN GRIFFIN

2014/5/15

#
It is zero. When I press the act button it doesn't change. Thanks for the help by the way:)
GRIFFIN GRIFFIN

2014/5/15

#
Would it be helpful to post the scenario on this website so you could look at it?
danpost danpost

2014/5/15

#
GRIFFIN wrote...
Would it be helpful to post the scenario on this website so you could look at it?
That is always easier than getting bits and pieces within the discussion posts.
GRIFFIN GRIFFIN

2014/5/15

#
Ok I will post it. I'll call it Tank Wars(Broken). This is my first time posting so it might take a bit.
danpost danpost

2014/5/15

#
Make sure you check the 'Publish source code' checkbox.
GRIFFIN GRIFFIN

2014/5/15

#
Um... sorry about this, I can't figure out how to upload it.
GRIFFIN GRIFFIN

2014/5/15

#
Nevermind i have a friend who knows how
danpost danpost

2014/5/15

#
Click the 'Share' button on the top right corner of the greenfoot window; enter a title, check the 'Publish source code' checkbox, enter your greenfoot login password and click on the 'Share' button.
There are more replies on the next page.
1
2
3