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

2014/4/21

Using input from user to change an image

stefi stefi

2014/4/21

#
Hi, I'm making a board game where the player chooses the colour of their counters at the start. However, I am having trouble using the response to the 'Choose your first colour' and making it change the image of the counter, as it says that the non-static method cannot be referenced from a static context. Here is the method in my 'Text' class that asks the user which colour they would like for their first counter and uses the response to change the image of the Counter class actor
public void P1Counter1()
    {

        Object[] selectionValues = { "Red", "Green", "Yellow", "Blue" };
        String initialSelection = "Blue";
        String s = (String) JOptionPane.showInputDialog(null, Player1Name+", choose your 1st colour!",
                "Colour", JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection);
        if (s=="Red")
        {
            Counter.Red();
        }
        if (s=="Yellow")
       
        {
            Counter.Yellow();

        }
        if (s=="Green")
        {
            Counter.Green();

        }
        if (s=="Blue")
        {
            Counter.Blue();

        }

    }
Here are the methods for the Counter class that should change the image
 public void Red()
    {
            setImage("Counter1.png");    
    }
    public void Blue()
    {
       setImage("Counter2.png");
    }
     public void Yellow()
    {
       setImage("Counter3.png");
    }
     public void Green()
    {
       setImage("Counter4.png");
    }
Thanks in advance!
danpost danpost

2014/4/21

#
The problem lies in the fact that the class itself does not have an image and therefore using 'setImage' on a class does not make any sense. You can set the image of the objects created from the class; but, you would need references to those objects. Could you please post the entire Counter class code.
stefi stefi

2014/4/21

#
Here you go :)
import greenfoot.*;
import javax.swing.JOptionPane;

/**
 * Player 1s counter in my snakes and ladders game
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    static int player1Position=0;
    static int diceNumber=0;
    public int Colour1;
    public int RED=0;
    public int BLUE=0;
    public int YELLOW=0;
    public int GREEN=0;
    public int Done=0; // if more than zero, stop asking questions

    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    { 
        Position();
    }    

    static void Dice()
    {
        /**
         * Rolls a 'dice' and sets the position of the player on my board, by using the coordinates
         */
        diceNumber = Greenfoot.getRandomNumber(6)+1;
        player1Position = diceNumber + player1Position;
    }
    
    public void Red()
    {
            setImage("Counter1.png");    
    }
    public void Blue()
    {
       setImage("Counter2.png");
    }
     public void Yellow()
    {
       setImage("Counter3.png");
    }
     public void Green()
    {
       setImage("Counter4.png");
    }


    public void Position()
    {
        if ( player1Position == 2 )
        {
            setLocation(151, 636);
        }
        if ( player1Position == 3 )
        {
            setLocation(243, 636);
            setLocation(452, 541);
            player1Position=8;
        }
        if ( player1Position == 4 )
        {
            setLocation(351, 636);
        }
        if ( player1Position == 5 )
        {
            setLocation(452, 636);
        }
        if ( player1Position == 6 )
        {
            setLocation(551, 636);
        }
        if ( player1Position == 7 )
        {
            setLocation(552, 541);
        }
        if ( player1Position == 8 )
        {
            setLocation(452, 541);
        }
        if ( player1Position == 9 )
        {
            setLocation(351, 541);
        }
        if ( player1Position == 10 )
        {
            setLocation(243, 541);
        }
        if ( player1Position == 11 )
        {
            setLocation(151, 541);
        }
        if ( player1Position == 12 )
        {
            setLocation(53, 541);
        }
        if ( player1Position == 13 )
        {
            setLocation(53, 355);
        }
        if ( player1Position == 14 )
        {
            setLocation(151, 449);
        }
        if ( player1Position == 15 )
        {
            setLocation(243, 449);
            setLocation(151, 541);
            player1Position=11;
        }
        if ( player1Position == 16 )
        {
            setLocation(351, 449);
            setLocation(551, 351);
            player1Position=19;
        }
        if ( player1Position == 17 )
        {
            setLocation(452, 449);
        }
        if ( player1Position == 18 )
        {
            setLocation(551, 449);
        }
        if ( player1Position == 19 )
        {
            setLocation(551, 351);
        }
        if ( player1Position == 20 )
        {
            setLocation(452, 351);
        }
        if ( player1Position == 21 )
        {
            setLocation(351, 351);
        }
        if ( player1Position == 22 )
        {
            setLocation(243, 351);
        }
        if ( player1Position == 23 )
        {
            setLocation(151, 351);
        }
        if ( player1Position == 24 )
        {
            setLocation(53, 351);
        }
        if ( player1Position == 25 )
        {
            setLocation(151, 245);
            setLocation(243, 155);
            player1Position=34;
        }
        if ( player1Position == 26 )
        {
            setLocation(243, 245);
        }
        if ( player1Position == 27 )
        {
            setLocation(351, 245);
        }
        if ( player1Position == 28 )
        {
            setLocation(452, 245);
        }
        if ( player1Position == 29 )
        {
            setLocation(551, 245);
        }
        if ( player1Position == 30 )
        {
            setLocation(547, 245);
        }
        if ( player1Position == 31 )
        {
            setLocation(555, 155);
        }
        if ( player1Position == 32 )
        {
            setLocation(452, 47);
            setLocation(452, 245);
            player1Position=28;
        }
        if ( player1Position == 33 )
        {
            setLocation(351, 155);
        }
        if ( player1Position == 34 )
        {
            setLocation(243, 155);
        }
        if ( player1Position == 35 )
        {
            setLocation(151,155);
        }
        if ( player1Position == 36 )
        {
            setLocation(52, 145);
        }
        if ( player1Position > 36 )
        {
            setLocation(52, 145);
            player1Position=36;
        }

    }

}
danpost danpost

2014/4/21

#
Ok. Now that I know what you have available to work with there, next, I will need to see more of the class that your 'P1Counter1' method is in, the Text class (mainly the fields and where you assign values to the counters).
stefi stefi

2014/4/21

#
Here is the Text class
import javax.swing.JDialog;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.JOptionPane;

/**
 * Creates a text box to ask the player their name
 */
public class Text extends Actor
{
    static String Player1Name;   // Player 1 Name
    static String Player2Name;
    static String p1counter1;    //Player 1's 1st counter
    static String p2counter1;
    static String p1counter2;
    static String p2counter2;
    public int player1Name=0;    // 0 - no name entered
    public int player2Name=0;
    public int P1counter1=0;
    public int P2counter1=0;
    public int P1counter2=0;
    public int P2counter2=0;

    /**
     * Sees if player 1 and player 2 have entered name
     */
    public void act()
    {
        if ( player1Name==0)
        {
            P1Name();
            player1Name ++;
        }
        if ( player2Name==0)
        {
            P2Name();
            player2Name ++;
        }
        if ( P1counter1==0)
        {
            P1Counter1();
            P1counter1 ++;
        }
        if ( P2counter1==0)
        {
            P2Counter1();
            P2counter1 ++;
        }
        if ( P1counter2==0)
        {
            P1Counter2();
            P1counter2 ++;
        }
        if ( P2counter2==0)
        {
            P2Counter2();
            P2counter2 ++;
        }

    }

    /**
     * Gets player 1 and player 2 to input their names
     */

    public void P1Name()
    {
        Player1Name = JOptionPane.showInputDialog(
            "Player 1, what is your name?");

    }

    public void P2Name()
    {
        Player2Name = JOptionPane.showInputDialog(
            "Player 2, what is your name?");

    }

    public void P1Counter1()
    {

        Object[] selectionValues = { "Red", "Green", "Yellow", "Blue" };
        String initialSelection = "Blue";
        String s = (String) JOptionPane.showInputDialog(null, Player1Name+", choose your 1st colour!",
                "Colour", JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection);
        if (s=="Red")
        {
            Counter.Red();
        }
        if (s=="Yellow")
       
        {
            Counter.Yellow();

        }
        if (s=="Green")
        {
            Counter.Green();

        }
        if (s=="Blue")
        {
            Counter.Blue();

        }

    }

    public void P2Counter1()
    {
        JDialog.setDefaultLookAndFeelDecorated(true);
        Object[] selectionValues = { "Red", "Green", "Yellow", "Blue" };
        String initialSelection = "Blue";
        Object selection = JOptionPane.showInputDialog(null, Player2Name+", choose your 1st colour!",
                "Colour", JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection);
        (selection) = p2counter1;
    }

    public void P1Counter2()
    {
        JDialog.setDefaultLookAndFeelDecorated(true);
        Object[] selectionValues = { "Red", "Green", "Yellow", "Blue" };
        String initialSelection = "Blue";
        Object selection = JOptionPane.showInputDialog(null, Player1Name+", choose your 2st colour!",
                "Colour", JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection);
        (selection) = p1counter2;

    }

    public void P2Counter2()
    {
        JDialog.setDefaultLookAndFeelDecorated(true);
        Object[] selectionValues = { "Red", "Green", "Yellow", "Blue" };
        String initialSelection = "Blue";
        Object selection = JOptionPane.showInputDialog(null, Player2Name+", choose your 2st colour!",
                "Colour", JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection);
        (selection) = p2counter2;

    }

}
Thanks :)
danpost danpost

2014/4/21

#
I do not see any place at which you are creating any Counter objects for either player. Where is that code?
stefi stefi

2014/4/21

#
I think the code that you're after is in my Board class, as that is the only place where I have added any counter objects
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This is the board for my snakes and ladders , the aim of the game is to get to the finish but if you land on a 
 * whale you go down and if you land on a boat you move forward.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Board extends World
{

    /**
     * Constructor for objects of class Board.
     * 
     */
    public Board()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 700, 1); 

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {

        Counter counter = new Counter();
        addObject(counter, 55, 554);

        Counter2 counter2 = new Counter2();
        addObject(counter2, 28, 571);

        Counter counter3 = new Counter();
        addObject(counter3, 59, 666);

        Counter4 counter4 = new Counter4();
        addObject(counter4, 31, 634);  

        Boat boat = new Boat();
        addObject(boat, 348, 598);
        Boat boat2 = new Boat();
        addObject(boat2, 159, 215);

        Boat boat3 = new Boat();
        addObject(boat3, 458, 408);

        Whale whale = new Whale();    
        addObject(whale, 195, 513);
        Whale whale2 = new Whale();
        addObject(whale2, 403, 225);

        Button button = new Button();
        addObject(button, 104, 41);

        TurnButton turnbutton = new TurnButton();
        addObject(turnbutton, 499, 44);

        GameOver gameover = new GameOver();
        addObject(gameover, 299, 349);

        Dice dice = new Dice();
        addObject(dice, 301, 39);

        Text text = new Text();
        addObject(text, 76, 475);

        Rules rules = new Rules();
        addObject(rules, 547, 678);

    }
}
:)
danpost danpost

2014/4/21

#
Okay. It looks like you are adding four counters into your world and two of them are created with the same Counter class. That in itself will make it difficult to determine (programmatically) which counter is which unless you hold references to them in fields in either your world class or split among your player classes (each player holding references to its own counters). One way to get around this, considering what you have so far, is to pass the counters to the Text object when you create it:
// change line 69 of Board class to:
Text text = new Text(counter, counter2, counter3, counter4);
Then, in your Text:
// add the following fields at line 22
static Actor player1Counter1;
static Actor player2Counter1;
static Actor player1Counter2;
static Actor player2Counter2;
// add the following constructor
public Text(Actor p1c1, Actor p2c1, Actor p1c2, Actor p2c2)
{
    player1Counter1 = p1c1;
    player2Counter1 = p2c1;
    player1Counter2 = p1c2;
    player2Counter2 = p2c2;
Finally, replace the last line in each of the P?Counter? methods with
player?Counter?.setImage("Counter"+("RED   BLUE  YELLOWGREEN ".indexOf((String)selection)/6+1)+".png");
Now you can * remove lines 86 through 105 of the Text class; * remove lines 39 through 54 of the Counter class; * remove similar code in your Counter2 and Counter4 classes Hope this makes sense and helps.
stefi stefi

2014/4/22

#
Thank you!!:)
You need to login to post a reply.