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

2014/12/16

Greenfoot Does Not Clear Screen

Kytuzian Kytuzian

2014/12/16

#
My project works in the Greenfoot editor, but when I export it to a .jar file, the Greenfoot does not repaint the screen, it just paints over the last frame. Any ideas?
danpost danpost

2014/12/16

#
What code are you trying to use? Please show the code for the class that performs (or is supposed to perform) that action.
Kytuzian Kytuzian

2014/12/16

#
I haven't tried to do anything special for the repainting, as I only just noticed it when I exported. I tried using the repaint() method and Greenfoot.delay(), but neither helped. I'm not really sure what you mean by the code for the class that is supposed to perform the action, do you mean the world?
danpost danpost

2014/12/16

#
If you cannot be sure of anything, then I must be even less sure. Maybe you should start with the world class code.
Kytuzian Kytuzian

2014/12/16

#
There is nothing that I can see in there that would change the repainting for the world. I can post the entire class if you like, but I don't see anything in it that would have that effect. I asked in case someone had seen anything like it before and could give me advice about it. Like I said, the strange part is that I have no issues with this in the editor, only once exported.
danpost danpost

2014/12/16

#
NVM info given in first post. Can you go ahead and post the initial world class code anyway?
Kytuzian Kytuzian

2014/12/16

#
Here you go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
import java.util.*;
 
public class SJHS extends World
{
    public int score, level;
     
    int playing;
     
    GreenfootSound backgroundMusic[] = {new GreenfootSound("yaketysax.mp3"), new GreenfootSound("yaketysax.mp3"), new GreenfootSound("yaketysax.mp3"), new GreenfootSound("yaketysax.mp3"), new GreenfootSound("yaketysax.mp3"), new GreenfootSound("yaketysax.mp3"), new GreenfootSound("yaketysax.mp3"), new GreenfootSound("yaketysax.mp3"), new GreenfootSound("yaketysax.mp3"), new GreenfootSound("yaketysax.mp3"), new GreenfootSound("yaketysax.mp3"), new GreenfootSound("yaketysax.mp3"), new GreenfootSound("centipede.mp3")};
     
    /**
     * Constructor for objects of class SJHS.
     *
     */
    public SJHS()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1100, 600, 1);
         
        addObject(new Moloney(), 300, 200);
         
        Generator.generateWorld(this, new ArrayList<Room>());
         
        playing = Greenfoot.getRandomNumber(backgroundMusic.length);
         
        for (int i = 0; i < backgroundMusic.length - 1; i++)
        {
            backgroundMusic[i].setVolume(45);
        }
         
        for (int i = backgroundMusic.length - 1; i < backgroundMusic.length; i++)
        {
            backgroundMusic[i].setVolume(100);
        }
         
        backgroundMusic[playing].play();
         
        updateScore();
    }
     
    public void act()
    {
        handleMusic();
         
        if (getObjects(Moloney.class).size() == 0)
        {
            getBackground().drawString("You lose!", getWidth() / 2, getHeight() / 2);
        }
        else if (getObjects(Student.class).size() == 0)
        {
            level++;
             
            Moloney moloney = (Moloney)getObjects(Moloney.class).get(0);
             
            for (int i = 0; i < Math.log(level * 12) / Math.log(1.2); i++)
            {
                Student placed = new Student();
                 
                addObject(placed, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
                 
                while (moloney.intersectingStudent())
                    placed.setLocation(Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            }
        }
    }
     
    public void handleMusic()
    {
        if (!backgroundMusic[playing].isPlaying())
        {
            playing = Greenfoot.getRandomNumber(backgroundMusic.length);
            backgroundMusic[playing].setVolume(45);
            backgroundMusic[playing].play();
        }
    }
     
    public void updateScore()
    {
        GreenfootImage background = new GreenfootImage(getWidth(), getHeight());
         
        Font larger = background.getFont().deriveFont(28.0f);
         
        background.setFont(larger);
        background.setColor(Color.black);
        background.drawString("Click to shoot Dress Codes at people. Don't let the students run into you!", 45, 50);
        background.drawString("Score: " + score, 45, 100);
        background.drawString("Level: " + level, 45, 150);
         
        setBackground(background);
    }
}
danpost danpost

2014/12/16

#
Try changing line 81 to this:
1
GreenfootImage background = getBackground();
Then try exporting it. Let me know if that fixed it and then I will try to explain.
Kytuzian Kytuzian

2014/12/16

#
It did fix it (with the addition of clearing the background, because it was drawing the text over the old text). The only explanation I can think of is that it's not allocating more new memory, but I'm not really sure. Edit: Just saw you said you would explain. Please do!
danpost danpost

2014/12/16

#
The background is called a background for a reason. It is like a backdrop -- the canvas of the easel that you will be painting on. There cannot be any transparency in its image -- it would be like a hole in the canvas and who knows what might show up on the other side of it (oftentimes, when the mouse passes over a button or something that has some mouse action attached to it, its image might appear in the transparent part of the image). Anyway, when you created a new image for the background on line 81, you introduced a transparent image as the background image when the image was set to the background on line 91. You could just as well have set the drawing color of the image to white and 'fill'ed the image before setting the drawing color to black and used the image you were creating on line 81 for the background.
Kytuzian Kytuzian

2014/12/16

#
Ah, I see. I didn't know that creating a new GreenfootImage() would make it transparent (although now I do see it in the documentation). Thanks for your help
You need to login to post a reply.