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

2015/6/12

Need Help with GreenfootImage wont change when i click a button

pururu08 pururu08

2015/6/12

#
i have a problem with refresh (redrawing) greenfootimage. i have a class showTextField which is showing the text i want to display. right here
public class showTextField extends Actor
{
    private String text="";
    
    public showTextField(String tks) {
       GreenfootImage img = new GreenfootImage(tks, 30, Color.BLACK, new Color(0,0,0,0));
       setImage(img);
    }
    
    public void redraw() {
            GreenfootImage newimg = getImage();
            newimg.clear();
            newimg = new GreenfootImage(text, 30, Color.BLACK, new Color(0,0,0,0));
            setImage(newimg);
    }
    
    public void setText() {
        fillInTheBlank fib = new fillInTheBlank();
        this.text = fib.lirikLagu(); //will give a random string
        System.out.println(text); //checking is the text changing or not
    }
}
and i have a button which will showing some of gameobject i want to show. right here.
public class startBtn extends Actor
{
    /**
     * Act - do whatever the startBtn wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       if(Greenfoot.mouseClicked(this)) {
           showTextField stf = new showTextField(new fillInTheBlank().lirikLagu());
           getWorld().addObject(stf, 300, 140);
           TextField textfield = new TextField();
           getWorld().addObject(textfield, 292, 242);
           submit submit = new submit();
           getWorld().addObject(submit, 96, 350);
           getWorld().removeObject(this);
        }
       if(Greenfoot.mouseMoved(this))
           setImage("startBtnHover.png");
       if(Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))
           setImage("startBtn.png");
    }    
}
i have one more button which is changing the showTextField text. right here
public class submit extends Actor
{
    private int score;
    showTextField stf = new showTextField(new fillInTheBlank().lirikLagu());
     
    public void act() 
    {
       if(Greenfoot.mouseClicked(this)) {
           stf.setText();
        }
        
       if(Greenfoot.mouseMoved(this))
           setImage("submithover.png");
           
       if(Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))
           setImage("submit.png");
    }    
    
    private int cekLirik(String lirik, String txt) {
        if(txt == lirik) {
            score ++;
        }
        
        return score;
    }
}
what i want is when i click the button, the text is changing. but it wont change on display, only on debugger. i was trying about 3 hours to clear this problem. im so confuse why it wont change
danpost danpost

2015/6/12

#
It might help if you redraw the image when setting the text.
pururu08 pururu08

2015/6/12

#
tried, but still won't change. the reason why i add the startBtn is because when i construct the world then i add that showTextField it is appear java.lang.OutOfMemoryError: Java heap space. it is work when i paused the greenfoot then i call the method one by one
danpost danpost

2015/6/12

#
The OutOfMemoryError issue sounds like a problem in you world subclass. The text of your showTextField object not changing is in its class. You did not show what you tried after my suggestion to fix it, so I cannot say where you went wrong in trying to fix it.
pururu08 pururu08

2015/6/12

#
i add redraw() at the bottom line.
public void setText() {
        fillInTheBlank fib = new fillInTheBlank();
        this.text = fib.lirikLagu(); //will give a random string
        System.out.println(text); //checking is the text changing or not
        redraw();
    }
public void change() {
        showTextField stf = new showTextField();
        if(create == true) {
           addObject(stf, 300, 140);
           stf.setText();
           create = false;
        } else {
            stf.setText();
        }
    }
i called this at the prepare() then the outofmemoryerror appear. so i try to make a button (startBtn) then the error not popping up again but the text wont show and wont change
danpost danpost

2015/6/12

#
The fix to the 'setText' method seems to be alright. Why are you calling 'change' from the 'prepare' method? I would think that is something you would do from the act method when a specific trigger is set. Maybe you should show your entire world class code.
pururu08 pururu08

2015/6/12

#
import greenfoot.*;
import java.util.Random;

/**
 * Write a description of class fillInTheBlank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class fillInTheBlank extends World
{
    private String Lirik[] = {"text1",
            "text11","text111",
            "text1111","text111111"};
    private int i;
    boolean create = true;
    
    public fillInTheBlank()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        prepare();
    }

    private void prepare()
    {
        startBtn startbtn = new startBtn(); //make the start button
        addObject(startbtn, 306, 184);
        
        //i commented this bcs this make memory out error appear
        /*change();
        TextField textfield = new TextField();
        addObject(textfield, 292, 242);
        submit submit = new submit();
        addObject(submit, 96, 350);*/
    }

    public String lirikLagu() {
        String lirik = Lirik[randomNumber()];
        return lirik;
    }
    
    private int randomNumber() {
        i = new Random().nextInt(4);
        return i;
    }
    
    public void change() {
        showTextField stf = new showTextField();
        if(create == true) {
           addObject(stf, 300, 140);
           stf.setText();
           create = false;
        } else {
            stf.setText();
        }
    }
}

i called it because i try to display text when the world is loaded. i already commented the change() call at prepare because it make the error not appear again.
danpost danpost

2015/6/12

#
The OutOfMemory error is due to creating a new fillInTheBlank world in the showTextField class while creating a fillInTheBlank world (this causes an infinite loop -- create world>creates object>creates world>creates object>etc.). I do not understand why you need the 'create' boolean field. Nor do I understand why your Lirik strings are not in your showTextField class. Moving them into your 'showTextField' class would remove the need to create a new world to get a new random string. A similar issue, though not as destructive, is that you were creating a new Random object each time you get a new random number (you can use the same object to produce multiple random numbers). In short, you only need create and add one 'showTextField' object into the world in the constructor of the world (or in the prepare method). The constructor of the 'showTextField' class will set the initial string and a simple method can be used to have the text changed.
pururu08 pururu08

2015/6/13

#
ah i see, i know my fault now. thank you very much danpost!
You need to login to post a reply.