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

2012/12/8

Switching images (creating a menu)

TisLars TisLars

2012/12/8

#
Hello, The goal is as followed: On the right side you have a small interface with 3 icons. When the user clicks on 1 of the 3 icons, a small menu with multiple text-lines appears underneath it. When the user clicks on a different icon, the previous list of text (menu) gets replaced by the text that is related to the currently clicked icon. I have the following code:
import greenfoot.*; public class btnPolice extends Buttons { public btnPolice() { if (Greenfoot.mouseClicked(this)) { ShowMenu show = new ShowMenu(); //getWorld().addObject(show, 650, 100); show._Selection = 1; } } }
import greenfoot.*; import java.awt.color.*; import java.awt.Color; public class ShowMenu extends Buttons { int _Selection; public void act() { ShowSelection(); } public void ShowSelection() { if (_Selection == 1) { setImage(new GreenfootImage("Hello world!", 18, Color.GREEN, Color.BLACK)); } else if (_Selection == 2) { setImage(new GreenfootImage("Hello world!\n" + "selection 2", 18, Color.GREEN, Color.BLACK)); } else if (_Selection == 3) { setImage(new GreenfootImage("Hello world!\n" + "selection 3", 18, Color.GREEN, Color.BLACK)); } else { } } }
The problem is, when I click the icon (First code in this post) nothing happens. What am I doing wrong when developing code in the Actor classes? When developing in the World classes, objects do get added. Thanks!
danpost danpost

2012/12/8

#
You cannot check for a click on an object that is not yet in the world. I do not think you intended for the btnPolice object to have a constructor that does this. You probably wanted 'public void act()' instead of 'public btnPolice()'
TisLars TisLars

2012/12/8

#
The object btnPolice is placed in the world. So how would I get this object to act like this: When user clicks btnPolice > Show the selection X in object ShowMenu?
danpost danpost

2012/12/8

#
Let me understand what you want, so I can give you the best course of action. Let me know if I am correct or incorrect on the following (1) You are going to have three btnPolice objects in the world (2) Each one will cause a ShowMenu object to display different text(s) (3) You want the text to disappear if the icon is clicked again (just a guess) If any or all are incorrect, please list what it is you do want.
TisLars TisLars

2012/12/8

#
(1) Practically correct, although there is a btnPolice, btnHospital and a btnFiremen. (2) Correct. The menu that will be displayed is triggered by the int _Selection. (3) Is an extra, but does provide some bonus towards the usability.
danpost danpost

2012/12/8

#
Change your 'btnPolice' class code to the following (instead of changing the text of the ShowMenu object, this removes any ShowMenu object from the world and creates a new one)
import greenfoot.*;

public class btnPolice() extends Button
{
    int selection = 1;

    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            if (getWorld().getObjects(ShowMenu.class).isEmpty())
            {
                getWorld().addObject(new ShowMenu(selection), 650, 100);
            }
            else
            {
                getWorld().removeObjects(getWorld().getObjects(ShowMenu.class));
            }
        }
    }
}
For the btnHospital and btnFiremen classes, you can copy the same code into them. Just change the name of the class from btnPolice to whatever and the value of the 'selection' field. Next, change your ShowMenu class to the following:
import greenfoot.*;
import java.awt.Color;

public class ShowMenu extends Buttons
{
    public ShowMenu(int _Selection) 
    {
        String imgText = "";
        if (_Selection == 1) imgText = "Hello world!";
        if (_Selection == 2) imgText = "Hello world!\n" + "selection 2";
        if (_Selection == 3) imgText ="Hello world!\n" +"selection 3";
        setImage(new GreenfootImage(imgText, 18, Color.GREEN, Color.BLACK));
    }
}
It is the same as what you had, except that since only the text was changing, I determined the text first, then set the image with that text. Also, there is no need change the text since the object will be replaced with a new one if needed.
TisLars TisLars

2012/12/8

#
Thanks, it works as intended. The code is perfect to understand, as I'm quite new, but I don't need any explanation for this so far! Thanks very much. Now I will continue to actually set different actions with every line of text.
danpost danpost

2012/12/8

#
Just to let you know, as an alternative (and this makes the coding even more simplistic): you could have the following:
import greenfoot.*;

public class btnPolice() extends Button
{
    String text = "Hello world!";

    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            if (getWorld().getObjects(ShowMenu.class).isEmpty())
            {
                getWorld().addObject(new ShowMenu(text), 650, 100);
            }
            else
            {
                getWorld().removeObjects(getWorld().getObjects(ShowMenu.class));
            }
        }
    }
}
Of course changing the 'selection' to 'text' in the other btn... classes. And then for the ShowMenu class:
import greenfoot.*;
import java.awt.Color;

public class ShowMenu extends Buttons
{
    public ShowMenu(String imgText) 
    {
        setImage(new GreenfootImage(imgText, 18, Color.GREEN, Color.BLACK));
    }
}
This makes the ShowMenu class a bit more usable (the object will be able to display any text). Notice, also, that there are no 'int's for the selection, which helps to simplify the code. We could make it even more usable by not limiting the font size and the colors within the ShowMenu class, but for your scenario, I doubt that would be neccessary (since I see no changes in that regard). If you are interested in seeing how far this could go, check out the Button class in my Menu Demo scenario.
You need to login to post a reply.