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

2017/4/26

Help with storing values into an Array of an object to be used by another class

1
2
danpost danpost

2017/4/28

#
I would switch the order of the two ifs and add an 'else' between them. You still need that 'incCaught' line in the one if. Please post the entire Ball class code for review as to why you are getting the NullPointerException.
Asiantree Asiantree

2017/4/28

#
Okay, I found the problem. I was using another BallInfo instance variable that wasn't being used inside the Ball constructor class. Now I need help with sorting the ballinfo objects. But first I need to increase the total amount a particular ball spawns in. I tried this in the Ball's act method, but it increased it every second:
1
ballData.incTotal();
danpost danpost

2017/4/28

#
Asiantree wrote...
I need to increase the total amount a particular ball spawns in.
Please rephrase that: 'the total amount a particular ball spawns in. Balls are not spawning anything in; and, if you mean that you want a particular color to have more chance of being chosen than another -- be specific (and why would you want this to begin with).
I tried this in the Ball's act method, but it increased it every second:
1
ballData.incTotal();
What you tried in the Ball's act method should only be called by a ball when created (from the constructor) -- it is not called to create a ball (and not from the act method).
Asiantree Asiantree

2017/4/28

#
My apologies. What I meant was when a red ball, for example, gets spawned into the world, the red BallInfo object adds one to its count. Then when another red ball gets spawned in, the count increases again. The same can be said for any other ball that spawns in (blue, black, green, purple). I have put this:
1
ballData.incTotal();
inside the Ball constructor, but it is still increasing the total amounts much faster than there are Balls present. For example, when a red ball spawns in, the total amount of red Balls keeps increasing even though there is only one red ball present.
danpost danpost

2017/4/28

#
Asiantree wrote...
when a red ball spawns in, the total amount of red Balls keeps increasing even though there is only one red ball present.
Then, you did not put the line in the constructor or you have the same line elsewhere (where it should not be).
Asiantree Asiantree

2017/4/28

#
I will take a deeper look at it. But the last task I am to accomplish is to sort the BallInfo objects by default by the Total amount. When I toggle a button, the BallInfo objects should then by sorted by amounts caught. Basically when the Balls are spawned in, the BallInfo objects should be sorted by total amount, starting with lowest to highest going down. Then, when a button is toggled, the BallInfo objects should be sorted by amount caught from lowest to highest going down. I have a toggle button class if you would like to take a look at it.
danpost danpost

2017/4/28

#
You could have them sort themselves depending on a boolean field that the button can toggle. Each BallInfo object (except for the top one) can compare whatever field needs compared for the sorting and, if needed, swap locations with the one above it; or you can just have the world sort all of them at once when the sort field changes. The first way would take less coding, but take a short amount of time to complete (you would actually see them sorting for a half a second or so).
Asiantree Asiantree

2017/4/28

#
I also have a button sub-class, so I am not sure if I should take that into consideration when working with the button. Here is the main ToggleButton 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
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
108
109
110
111
112
public abstract class ToggleButton extends Actor
{
    private String originalText;  // label text as it appears in the button initially
    private String toggledText;   // label text as it appears in the button when toggled
    private String currText;      // text currently in the label
    /*
     * the following is a bit "kludge-like" to handle the difference between the two
     * current Greenfoot versions, which differ in how they name Color constants.
     */
    private static Color WHITE = new Color(255,255,255);
    private static Color BLACK = new Color(0,0,0);
    private static Color GRAY = new Color(200,200,200);
     
    /**
     * EasyButton default constructor
     *
     * @param text1 the text to place as the button label initally and every even numbered click
     * @param text2 the text to place as the button label on odd numbered clicks
     */
    public ToggleButton(String text1, String text2)
    {
        originalText = text1; // remember the intended original label value
        toggledText = text2;  // remember the intended toggled label value
         
        currText = originalText; // starts untoggled.
         
        redraw(WHITE, BLACK); // draw the button (white BG, black FG)
    }
     
    // redraws the button
    //   bg - the background color to use
    //   fg - the text color to use
    private void redraw(Color bg, Color fg)
    {
 
         
        // text to add will be padded with a space on either side
        String drawnString = "" + currText + "";
         
        int textWidth = drawnString.length()*6;
        int textHeight = 13;
        // make image just a bit too tall/wide ... it looks nicer that way
        int imgW = textWidth+4;
        int imgH = textHeight+4;
         
        // get the image
        GreenfootImage img = getImage();
         
        // if the image is null or does not meet (changed) text requirements
        if (img==null||
            img.getWidth()!=imgW ||
            img.getHeight()!=imgH )
        {
            // build an image of (more?) appropriate size
            img = new GreenfootImage(imgW, imgH);
            setImage(img);
        }
         
        // fill in the background
        img.setColor(bg);
        img.fill();
         
        // draw a rectangle around the edge of the button
        img.setColor(BLACK);
        img.drawRect(0,0,img.getWidth()-1,img.getHeight()-1);
         
        // draw in the text in selected font
        img.setColor(fg);
        img.drawString(drawnString,  2,textHeight);
    }
     
    /**
     * handle does the work that clicking the button should do.
     *
     * You MUST overload this method in a subclass.
     */
    public abstract void handle();
     
    /**
     * getText is an accessor for the text in the button label
     * @return the String containing the text in the button label
     */
    public String getText()
    {
        return currText;
    }
     
    /**
     * Act - process a button click
     */
    public void act()
    {
       
        // if this button was clicked (i,e, toggled)...
        if (Greenfoot.mouseClicked(this))
        {     
            // if it is currently original text ...
            if (currText.equals(originalText))
            {              
                currText = toggledText; // switch to toggled text
                redraw(GRAY, WHITE);    // redraw as "toggled"
            }
            else // toggling back to original text ...
            {               
                currText = originalText; // switch to original text
                redraw(WHITE, BLACK);
            }
             
            handle(); // button was clicked ... do whatever it is suposed to do.
        }
    }   
}
And here is the sub-class for ToggleButton called ButtonSub:
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
public class ButtonSub extends ToggleButton
{
    private BallInfo info[];
    public ButtonSub(String text1, String text2)
    {
        super(text1, text2);
    }
     
    public void handle()
    {
         
    }
     
    /**public void bubbleSort()
    {
        boolean notSorted = true;
        while (notSorted)
        {
            notSorted = false;
            for (int index=0; index<arrayData.length; index++)
            {
                if (info[index] > info[index+1])
                {
                    swap(index, index+1);
                    notSorted = true;
                }
            }
        }
    }
    **/
}
Where handle is the method called that will toggle the button and change the sorting of the BallInfo objects. I have listed above a particular way to sort called "BubbleSort" but I am confused on how to access the BallInfo objects located in the world, because they are not currently in an array.
You need to login to post a reply.
1
2