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

2014/1/25

Change font size

1
2
3
kasperk22 kasperk22

2014/1/25

#
Hey guys. I want to change my font size, but can't seem to find any working solutions in here, that fit my code. here's the code
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) 
import java.awt.Color;
public class Start extends Actor
{
    private int score = 3;
 
    public Start()
    {
        score = 3;
        setImage(new GreenfootImage("String " , 20, Color.GREEN, Color.BLUE));
        update();
    }
 
    public void addScore()
    {
        score--;
        update();
    }
 
    /**
     * Return the current counter value.
     */ 
    public int getValue() 
    
        return score; 
    
 
    /**
     * Set a new counter value.
     */ 
    public void setValue(int newValue) 
    
        score = newValue; 
        update(); 
    
 
    public void update()
    {
        GreenfootImage img = getImage();
        img.clear();
        img.setColor(Color.BLACK);
        img.drawString ("Lives:" + score, 3, 20);
    }
}
bourne bourne

2014/1/25

#
Before you call img.drawString, change the Font that the GreenfootImage has:
1
img.setFont(img.getFont().deriveFont(12f)); // Where 12 is the desired size.
kasperk22 kasperk22

2014/1/25

#
Oh it worked! however the text is still limited within a very small area... how do i make that area larger :)
bourne bourne

2014/1/25

#
setImage(new GreenfootImage("String " , 20, Color.GREEN, Color.BLUE));
The GreenfootImage constructor used above, creates the image with dimensions just large enough to fit the text as described. So what you could do is just setting the image again, to a new one with a different Font size (instead of the 20).
danpost danpost

2014/1/25

#
A couple things about this class code that does not seem right: (1) the name of the class is 'Start', yet it is in no way close to having 'start' associated with it; it looks like a Counter class that is specific for tracking the number of lives for a character (or, a LifeCounter class); (2) the 'addScore' method does not add any score, but instead decreases the score (or lives) by one; method names should reflect, or give an idea of, what the method actually does; 'decrementScore', or even 'decScore', would be more appropriate. You may find that the size of the image created for the object at line 10 may not be large enough to display the entire text after changing the font size. It would not hurt to create and set a new GreenfootImage for the image of the object in the 'update' method and remove line 10 altogether (it is not like the image of the object will be changing every act frame):
1
2
3
4
private void update()
{
    setImage(new GreenfootImage("Lives: "+score,, 20, Color.black, null));
}
Also, the font can be adjusted to suit right there without any worries about the text fitting the image.
kasperk22 kasperk22

2014/1/25

#
Yea I was actually trying to make a counter for lives, but i also needed a start text+difficulty(speed of game)... but thanks this works! - but now i am stuck trying to make it decrease each time an object becomes "outOfWorld" do you have any ideas how to do that? i could send all the code if it would help more :)
kasperk22 kasperk22

2014/1/25

#
And by the way, is there an easier way to chat... i'm not good at checking sites like this unless i get a sound or popup each time a new message occurs :D
danpost danpost

2014/1/25

#
This is what there is as fare as chatting. I mean, there are some scenario on the site that can be used for instant messaging or private messaging, but it is always better to use the discussion threads as other can learn from them. If you are only going to create one instance of the Start class (or what was the Start class if you had renamed it), then:
1
((Start)getWorld().getObjects(Start.class).get(0)).decScore();
would be sufficient to do what you need. If you have more than one object of that class, then you will need instance fields to hold the different objects and refer to the fields needed:
1
((WorldClassName)getWorld()).startReferenceName.decScore();
supplying the appropriate names.
kasperk22 kasperk22

2014/1/25

#
and where would i put the last code? if i just use the first code, i get an nullpointerexception error :)
danpost danpost

2014/1/26

#
You would have to create the object and save it in an instance field in the world class; the name you give it is the name you refer to it as ('startReferenceName'). The code itself would be in an actor class (probably your main character that the lives counter is being used for).
kasperk22 kasperk22

2014/1/26

#
i'm sorry but i don't understand completely... the code for my World is following:
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.*;   
     
public class MyWorld extends World   
{   
    public int a = 0
    private int threshold = 100; //increase to make the time until a random thing is added longer...   
    private int thresholdWave = 3; //increase to make the time until a random thing is added longer...  
    GreenfootSound backgroundMusic = new GreenfootSound("BACKGROUND.wav");
    Counter counter = new Counter();
/**
 * Counter counter = new Counter(); does three things:
 * it declares a variable (so it can be used later in the code)
 * It sets a java objerator keyword ( this creates the object)
 * The operator is followed by a call to a contructor. This initializes (gives an initial value) the new object
 */
    /**
     * Creates a world with the size of 1100x500 cells, where every cell is just 1 pixel
     */ 
       
    public MyWorld() 
    {     
        super(1100, 500,1); 
        addObject(counter, 50, 25); 
        background();
        addObject (new Instructions(), 95, 67);
        addObject (new Start(), 565, 40);
    
       
/**
 * Each time an act is happening, one is added to a. If a becomes equal to threshold, it will execute RandomPlacement and then set a as 0 again (loop)
 */
    public void act()
    {   
         a++;   
        if(a == threshold)   
        {  
           RandomPlacement();  
            a = 0;   
        }   
        youmiss();
         
    }   
    /**
     * initializes x as a random number from 0 to the width of the map abd y as 10.
     * then an object is generated with reference to counter in order for the object to refer codes to the codes written in counter.
     * The placement of the object is set by the initialized numbers.
     */
    public void RandomPlacement()   
    {     
        int x = Greenfoot.getRandomNumber(getWidth());   
        int y = 10;   
    addObject (new Rock(counter),x,y); 
    
 
public void youmiss()
{
    if (Greenfoot.mouseClicked(this))
    {
    Greenfoot.playSound("MISS.wav");
}
}
 
/**
 * set the sound to play in a loop with 50% volume
 */
public void background()
{
    backgroundMusic.playLoop();
    backgroundMusic.setVolume(20);
}
}
what exactly am i supposed to write in this code? i'm not sure that it means to save it in an instance field :)
danpost danpost

2014/1/26

#
In general terms (using your world class code as an example), when you create an object (like the world that is created when you reset (or re-compile) your project), all non-static fields declared in that class are created, initialized and given to that object. This is done for each object created from that class. These non-static fields are also called instance fields because each instance of that class (each object created from that class) will have fields with those names. You have at line 9 declared an instance field for a Counter object. When a new instance of that class is created, it is given a field called 'counter' to hold an object of type Counter and a new Counter object is created and the value of the 'counter' field is set to reference that Counter object. You can add to your world an instance field to hold a Start object whose value references a new Start object. Then line 26 can add that Start object into the world instead of a new one. Just for further reference, an instance field declaration statement is formatted as follows: ClassName referenceName; // assigning a value is optional examples: Counter counter; Counter counter = new Counter(); Start lifeCounter; String highscoreFilename = "gamescores.txt"; String soundFilenames = new String; // or fieldType fieldname; // assigning a value is optional int speed; int speed = 1; double realX, realY; // declaring more than one 'double' field
kasperk22 kasperk22

2014/1/26

#
hmm i tried doing this with Start (changed it's name to lifeCounter)... now in the Rock method, i get following error:cannot find symbol - class lifecounter.
danpost danpost

2014/1/26

#
If you change 'Start' to something else, change it everywhere. But change it to something that begins with an uppercase letter, please. 'lifeCounter' is the name that I gave the field that is to hold a Start object (not the name of the class).
kasperk22 kasperk22

2014/1/26

#
okay i changed the name to LifeCounter :)
There are more replies on the next page.
1
2
3