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

2015/2/26

Need comments on code; I have no idea what it is saying.

ValeroDeniro ValeroDeniro

2015/2/26

#
Hi there, I really need help understanding what these lines of code say. Would really appreciate if anyone could help me out. Thak you!
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
*/
public class Counter extends Actor
{
    private static final Color textColor = new Color(255, 180, 150);
     
    public int target = 0;
    private int stringLength;
     
    private String text;
     
    public Counter()
    {
        this("");
    }
     
    public Counter(String prefix)
    {
        text = prefix;
        stringLength = (text.length() + 2) * 10;
 
        setImage(new GreenfootImage(stringLength, 16));
        GreenfootImage image = getImage();
        image.setColor(Color.BLACK);
 
        updateImage();
    }
     
    public void act()
    {
        updateImage();
    }
     
    public void add(int score)
    {
        target += score;
    }
     
    private void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text + target, 1, 12);
    }
}
danpost danpost

2015/2/26

#
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
import greenfoot.*; // includes the greenfoot package (makes available classes provided by greenfoot)
import java.awt.Color; // includes the Color class
 
/**
 * creates a counter that displays a text string and a value associated with it;
 * examples: "Score: 25"; "Health: 100"; "Lives: 3"; "Exp. 425"; "Level 5";
 */
public class Counter extends Actor
{
    /**
     * class color constant (not used); all objects created may have been intended to
     * use this color for their text;
     */
    private static final Color textColor = new Color(255, 180, 150);
     
    /** mis-named instance field ('score' would be a more appropriate name) */
    public int target = 0;
 
    /** instance field that is not needed (should be local variable in constructor) */
    private int stringLength; 
 
    /** instance field for text to precede the current value of the counter */
    private String text;
      
    /** basic constructor for objects of this class */
    public Counter()
    {
        this(""); // calls the next constructor supplying an empty string for the text
    }
      
    /**
     * main constructor for objects of this class; initializes a new Counter object
     * @param prefix the displayed text that is to precede the value of the counter
     */
    public Counter(String prefix)
    {
        text = prefix; // saves preceding text in instance field
        stringLength = (text.length() + 2) * 10; // calculates current length of displayed text
        setImage(new GreenfootImage(stringLength, 16)); // creates image of currently appropriate size
        GreenfootImage image = getImage(); // gets a local reference to the new image
        image.setColor(Color.BLACK); // sets the color for future drawing on the image
        updateImage(); // initializes the displayed text of the image
    }
      
    /**
     * constantly updates the displayed text of the image;
     * note: would be better to update the text only when the score is changed
     * (by removing this method and adding a call to 'updateImage' in the 'add' method)
     */
    public void act()
    {
        updateImage();
    }
      
    /**
     * allows the value of the score ('target' field value) to be adjusted
     * @param score (better named 'amount' or 'change') the value to adjust the score by
     */
    public void add(int score)
    {
        target += score; // adds adjustment to the value of the 'target' field value
    }
      
    /** updates the displayed text on the image of the counter object */
    private void updateImage()
    {
        GreenfootImage image = getImage(); // gets a local reference to the image
        image.clear(); // clears all color from the image (makes transparent)
        image.drawString(text + target, 1, 12); // draws text on image using current values
    }
}
You need to login to post a reply.