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

2014/5/20

Saving color to make new background

cobarr cobarr

2014/5/20

#
Hi, I have gotten the current background color of my greenfoot game screen at 0,0 (what is "current" can be five possible colors) and want to be able to save it in my "colored square" actor so that I can then call on it in my Menu world which contains multiple levels. In all levels the new background will be called on. How do I code something in ColoredSquare which saves the information to be used in Menu World, level1 () {} for example? How would Menu world call on this information? Here is my code in "ColoredSquare" Actor, public void act:
1
2
3
4
5
6
7
8
9
10
11
12
if (Greenfoot.mouseClicked(this))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if (mouse != null)
            
                getWorld().setBackground(worldImage); 
                getWorld().getBackground().getColorAt(0, 0); //Get color of BG at (0,0)
 
                //getBackground, getColor at 0,0
                // call saveColorBG();
            }
        }
Main Problem aside: I have used getMouseInfo and Greenfoot.mouseClicked... is it unnecessary to use both? Is there a difference in using both or just Greenfoot.mouseClicked?
danpost danpost

2014/5/20

#
First off, I see no need to get the MouseInfo object (lines 3 and 4); secondly, where is 'worldImage' coming from (line 6); and finally, what are you doing with the returned color (line 7).
danpost danpost

2014/5/20

#
cobarr wrote...
Main Problem aside: I have used getMouseInfo and Greenfoot.mouseClicked... is it unnecessary to use both? Is there a difference in using both or just Greenfoot.mouseClicked?
After using Greenfoot.mouseClicked, the only reasons you might have for using 'getMouseinfo' would be to get more specific information about the mouse action (location, actor, or button info). If no extra info is required, then there is no need for getting a MouseInfo object.
cobarr cobarr

2014/5/20

#
Ok, here is the edited code for ColoredSquare:
1
2
3
4
5
6
7
8
9
10
11
12
public void act()
    {       
        if (Greenfoot.mouseClicked(this))
        {
              
                getWorld().setBackground(worldImage); 
                getWorld().getBackground().getColorAt(0, 0); //Get color of BG at (0,0)
 
                // call saveColorBG();
        }
 
    }
worldImage comes from my Menu world. An array of colors is created in Menu world and lined up. int y and int x are for the placement of my colored squares. Here is a section of my Menu world code where worldImage is found:
1
2
3
4
5
6
7
8
9
10
11
12
int y = 136; //y co-ordinate
        for (int i = 0; i < 4; i++)
        {
            int x = 69 + i * 40;
 
            GreenfootImage worldImage = new GreenfootImage("background-" + i + ".png");
            ColoredSquare square = new ColoredSquare(colors[i], worldImage);
 
            addObject(square, x, y);
            square.setLocation(x, y);
 
        }
With the returned color I am want to create a new background for the world when the user clicks the "play" button (present at the opening of the game) and enters level1. Before the user clicks the "play", I have a "background chooser" which customizes the background color for the rest of the game. As the user clicks to choose the color they would like, the background changes to the chosen color and can continue to change as the user continues to click on multiple colored squares, until the "play" button is clicked and the color is collected to use to fill the new background for level one in the Menu world. I hope this is not too confusing or unclear. Please let me know if there are parts I need to give a more thorough description of.
danpost danpost

2014/5/20

#
What does the rest of your ColoredSquare class look like? and is that class only used from the Menu world?
cobarr cobarr

2014/5/21

#
ColoredSquare Actor:
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
public class ColoredSquare extends Actor
{
    /* Act - do whatever the ColorSquares wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int x = 0
    private int y = 0;
    private GreenfootImage worldImage;
 
    public ColoredSquare(Color color, GreenfootImage worldImage)
    {
        GreenfootImage image = new  GreenfootImage(30, 30);
        image.setColor(color);
        image.fill();
        setImage(image);
 
        this.worldImage = worldImage;
 
    }
 
    public void act()
    {       
        if (Greenfoot.mouseClicked(this))
        {
              
                getWorld().setBackground(worldImage); 
                getWorld().getBackground().getColorAt(0, 0); //Get color of BG at (0,0)
 
                //getBackground, getColor at 0,0
                // call saveColorBG();
        }
 
    }
}
Yes, the ColorSquare class is only used from the Menu world. Once the user clicks the "play" button and the color of the background is taken to paint a new background for level1(){}, the ColoredSquare class is not used again or needed. Here is all the code in my Menu world:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
/**
 * Write a description of class Menu here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Menu extends World
{
    private Color bkColor = null;
     
    /*
     * Constructor for objects of class MathWorld.
     */
    public Menu()
 
    {   
        // set starting state
        super(530, 440, 1);
        setBackground("background1.png");
        populate();
 
    }
 
    public void populate()
    {
 
        //actors that should be seen when game starts
        //array of background colors
        Color[] colors = new Color[] {
            new Color (255, 255, 102),
            new Color (200, 255, 150),
            new Color (255, 150, 150),
            Color.ORANGE
        };
                 
        //coloring table
        int y = 136; //y co-ordinate
        for (int i = 0; i < 4; i++)
        {
            int x = 69 + i * 40;
 
            GreenfootImage worldImage = new GreenfootImage("background-" + i + ".png");
            ColoredSquare square = new ColoredSquare(colors[i], worldImage);
 
            addObject(square, x, y);
            square.setLocation(x, y);
 
        }
 
        PlayButton playbutton = new PlayButton();
        addObject(playbutton, 135, 211);
        playbutton.setLocation(133, 209);
 
    }
     
    public void saveColorBG(Color color) 
    
        bkColor = color;
    }
 
    public void level1()
    {
         
        addObject(new Counter(), 450, 34);
         
    }
danpost danpost

2014/5/21

#
Ok. There seems to be a lot of jumping around with the code, a lot of unnecessary code and code that could be better placed. Since your ColoredSquare class is only used by the Menu class, the bulk of the code dealing with the color would be better placed in the ColoredSquare class and not in the Menu class.
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
import greenfoot.*;
import java.awt.Color;
 
public class ColoredSquare extends Actor
{
    public static final Color[] colors = {
        new Color (255, 255, 102),
        new Color (200, 255, 150),
        new Color (255, 150, 150),
        Color.orange };
    public static int colorChoice;
 
    public int colorValue;
 
    public ColoredSquare(int colorNum)
    {
        colorValue = colorNum;
        GreenfootImage image = new  GreenfootImage(30, 30);
        image.setColor(colors[colorValue]);
        image.fill();
        setImage(image);
    }
 
    public void act()
    {       
        if (Greenfoot.mouseClicked(this))
        {
            colorChoice = colorValue;
            getWorld().setBackground("background-"+(colorValue+1)+".png")
    }
}
Then your Menu class can just be this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import greenfoot.*;
import java.awt.Color;
 
public class Menu extends World
{
    public Menu()
    {   
        super(530, 440, 1);
        setBackground("background-"+(ColoredSquare.colors[ColoredSquare.colorChoice]+1)+".png");
        populate();
    }
 
    public void populate()
    {
        for (int i=0; i<4; i++) addObject(new ColoredSquare(i), 69+i*40, 136);
        addObject(new PlayButton(), 133, 209);
    }
}
The code in the PlayButton class should set a new Level1 world active for when its object is clicked on. I am not sure why you had the 'level1' method to add a Counter object into the Menu world; so, I removed it.
cobarr cobarr

2014/5/21

#
I have a total of 3 levels. Does that mean I will need three other Level worlds? How can I stick to just one world (Menu world) ? I thought that maybe creating methods within the Menu world would allow me to make the changes need (since there are not any drastic changes). So far in the 'Level1" method I only add a counter, but later on I plan to also add a timer, math questions..etc.
cobarr cobarr

2014/5/21

#
Do you mind explaining to me why you used "public static final"? What does "final" do?
danpost danpost

2014/5/21

#
You will not need to add extra Level worlds to your project. The 'level1' method in the Menu class, lines 64 through 69, as is, will add a Counter to your Menu world, not to one of your level worlds. You should work on finishing the workings of the Menu then move on the working with the levels.
danpost danpost

2014/5/21

#
cobarr wrote...
Do you mind explaining to me why you used "public static final"? What does "final" do?
Declaring the field as 'final' prevents any changes in its value -- the field holds, in essence, a constant.
You need to login to post a reply.