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

2015/11/30

Adding a button to the screen and changing its image?

hoveeagle hoveeagle

2015/11/30

#
I have been trying to develop a program that allows the user to enter their colour scheme preferences. I have run into trouble when trying to make the button itself and then assigning an image to that object just made. I have been trying to look up examples where this is done but everything I look at is slightly different to what I am trying to achieve. Any help on this great so thanks in advance. This is what I have been trying to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import greenfoot.*;
public class Mu_Main extends MuFe_ScreenFrame
{
 
    /**
     * Constructor for objects of class Mu_Main.
     *
     */
    public Mu_Main()
    {
        Bn_PlayGame PlayGame = new Bn_PlayGame();
         
        switch (ColourScheme) {
            case 1:  PlayGame.setImage(new GreenfootImage("Button PlayGame 1.png"));
                     break;
            default: ColourScheme = 1;
                     break;
                    }
    }
}
danpost danpost

2015/11/30

#
First, I do not see where 'ColourScheme' is declared in your code. Second, I always get a bit suspicious when a class extends another one that is unknown to me. Oftentimes, I find that the class structure is flawed in that a subclass is not a more specific type of its superclass, but it describes some part of the full object its superclass represents. For example, a button may be a part of a menu, but a button, in itself, is not a menu; therefore the Button class should not extend the Menu class. I cannot say one way or another whether this is an issue in your case without further information, however. At any rate, you may find my Value Display Tutorial scenario of some help.
hoveeagle hoveeagle

2015/12/1

#
Maybe it would be of value for me to post the super class even though it doesn't really effect this section of code? ColourScheme is declared within the MuFe_ScreenFrame and the class you are seeing is one designed to be my main menu. Here is simply want to call a new version of one of my button classes and then assign it a picture based on the value stored within ColourScheme. When run this code throws no syntax errors but instead just doesn't create the object. I have tried to use other code to make the object but I then don't have a way that I know of to manipulate the image over the top of that as I have no way to reference the object made. I am sorry if this is unclear and I appreciate the help. If you are still unclear on what I am trying to do then please ask away! I also cannot view the tutorial as the network I have to use for this work blocks many java web applications.
hoveeagle hoveeagle

2015/12/1

#
Just to illustrate my point of having tried other techniques I have used the code
1
addObject(new MyClass(), x, y);
But then I have no idea how to set the image of the object that has been made as a result. I may be missing something obvious here so let me know if I am.
hoveeagle hoveeagle

2015/12/1

#
I DID IT! After tinkering for what has been just over a day I managed to do what I was asking for, but thanks for the response. Just for the curious this is what I was trying to do:
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
import greenfoot.*;
public class Mu_Main extends MuFe_ScreenFrame
{
 
    /**
     * Constructor for objects of class Mu_Main.
     *
     */
    public Mu_Main()
    {
        Bn_PlayGame PlayGame = new Bn_PlayGame();
        addObject(PlayGame,100,100);
        switch (ColourScheme) {
            case 1:  PlayGame.setImage(new GreenfootImage("Button PlayGame 1.png"));
                     break;
            case 2:  PlayGame.setImage(new GreenfootImage("Button PlayGame 2.png"));
                     break;
            case 3:  PlayGame.setImage(new GreenfootImage("Button PlayGame 3.png"));
                     break;
            case 4:  PlayGame.setImage(new GreenfootImage("Button PlayGame 4.png"));
                     break;                    
            default: ColourScheme = 1;
                     break;
                    }
    }
}
danpost danpost

2015/12/1

#
I am still not sure if the MuFe_ScreenFrame class should be what Mu_Main extends. If one is a screen and the other is a menu, then they should not extend one another. An object of a subclass must be said to an object of the class it extends. Subclassing is used to create the same type of object with extra states or behaviors. Going the other way, an object of a subclass should have all the states and behaviors of any object of its superclass. As far as needing a reference:
1
2
3
4
5
6
7
// add an instance field
private Actor myClass;
// for creating
myClass = new MyClass();
addObject(myClass, x, y);
// later
myClass.setImage(< image >);
hoveeagle hoveeagle

2015/12/2

#
Sorry maybe I have confused things. My screen frame as simply an initializer for screen settings such as width and height as well as colours to be used on the screen drawn. Mu main is simply an individual world that I use for drawing and containing buttons and other interactive elements. Thanks for the concern.
danpost danpost

2015/12/2

#
hoveeagle wrote...
Sorry maybe I have confused things. My screen frame as simply an initializer for screen settings such as width and height as well as colours to be used on the screen drawn. Mu main is simply an individual world that I use for drawing and containing buttons and other interactive elements. Thanks for the concern.
Please post the class code of the screen frame. I still have the feeling something is amiss with it.
hoveeagle hoveeagle

2015/12/4

#
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import greenfoot.*;
import java.io.*;
import java.util.*;
import java.lang.*;
 
public class MuFe_ScreenFrame extends World
{
    //Declairing world property variables for later use.
    static final int WorldWidth = 1920;
    static final int WorldHeight = 1080;
    //Declairing the user preference variables.
    int ColourScheme;
    int SoundToggle;
    int MusicToggle;
    //Declaring the sound files for use by buttons and the MuFe_ScreenFrame function
    GreenfootSound Song = new GreenfootSound("BackgroundSong.mp3");
    GreenfootSound Button = new GreenfootSound("ButtonPress.mp3");
    GreenfootSound Building = new GreenfootSound("BuildingPlacement.mp3");
    GreenfootSound Error = new GreenfootSound("Error.mp3");
    public MuFe_ScreenFrame()
    
        //Decliaring the world size using the pre defined size variables. I used variables here instead of dropping numbers straight in so that i could have an adjustable window if time permited its developement at a later date.
        super(WorldWidth,WorldHeight,1);
        //Declairing file reading classes as variables for later use
        String fileName = "Settings.txt";
        String line = null;
        File FileExistance = new File("Settings.txt");
        //This entire if statement checks for the existance of the settings file and then assigns its contained values to variables for later use.
        if(FileExistance.exists())
        {
            try
            {
                FileReader fileReader = new FileReader(fileName);
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                 
                ColourScheme = Integer.parseInt(bufferedReader.readLine());
                SoundToggle = Integer.parseInt(bufferedReader.readLine());
                MusicToggle = Integer.parseInt(bufferedReader.readLine());
                 
                bufferedReader.close();
            }
            //These errors are printed to console in the event of an issue.
            catch(FileNotFoundException ex) {
                System.out.println("Unable to open the settings file");
            }
            catch(IOException ex) {
                System.out.println("Error reading the settings file");                 
            }
        }
        //If a settings file does not exist then the program makes one with default values, which is simply a series of 1s.
        else
        {
            try
            {
                FileWriter fileWriter = new FileWriter(fileName);
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                bufferedWriter.write("1");
                bufferedWriter.newLine();
                bufferedWriter.write("1");
                bufferedWriter.newLine();
                bufferedWriter.write("1");
                bufferedWriter.close();
            }
            //This error is printed to console in the event of an issue.
            catch(IOException ex) {
                System.out.println("Error writing to file '" + fileName + "'");
            }
        }
        //After taking the value previously read from the settings file this case statement sets a background colour that corrisponds to the selected option.
        switch (ColourScheme) {
            case 1:  setBackground("Background 1.png");
                     break;
            case 2:  setBackground("Background 2.png");
                     break;
            case 3:  setBackground("Background 3.png");
                     break;
            case 4:  setBackground("Background 4.png");
                     break;
            default: ColourScheme = 1;
                     break;
        }
        //Like the colour scheme selection above this code uses the settings file number to decide whether the volumes for all sound effects should be 100 (audible )or 0 (inaudible).
                switch (SoundToggle) {
            case 1:  Button.setVolume(100);
                     Building.setVolume(100);
                     Error.setVolume(100);
                     break;
            case 2:  Button.setVolume(0);
                     Building.setVolume(0);
                     Error.setVolume(0);
                     break;
            default: SoundToggle = 1;
                     break;
        }
        //Once again this just takes the number from the settings file, and turns the volume either up to 100% or 0%.
                switch (MusicToggle) {
            case 1:  Song.setVolume(100);
                     break;
            case 2:  Song.setVolume(0);
                     break;
            default: MusicToggle = 1;
                     break;
        }
        //Finally this just starts the song playing on a loop no matter what menu is open. This will be heard if the MusicToggle variable was 1
        Song.playLoop();
    }
}
hoveeagle hoveeagle

2015/12/4

#
Hope this ^^ helps
You need to login to post a reply.