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

2015/11/3

Using an Image/Actor to draw a String

EgoInfinite EgoInfinite

2015/11/3

#

LONG STORY

I can not for the life of me figure this thing out. Although I've seen many post on drawString(), I have yet to see a simple one with the exact code I need. I created an Actor, class MASTER, and a subclass for MASTER, MASTERSAYS, which is just a white rectangle, sort of like Simon Says. I wanted to at the very least find the correct code for drawing a string on an Image/Actor and I can work by myself from there. MASTERSAYS will obtain a String from master and that String should appear in MASTERSAYS exclusively wherever MASTERSAYS is located, because its a text box, but I have yet to figure this out please help.

SHORT STORY

I'm trying to make text appear on this Actor/Image, but I don't know how.

CODE

I don't think my code is necessary for a solution, but here it is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// MASTER creates MASTERSAYS directly below
 if(SCRIPT == 3)
            {      
                int X = getX();
                int Y = getY();
                getWorld().addObject(new MASTERSAYS(), X, Y+200);
            }
 
// This is where MASTERSAYS will insert the first sentence
    protected void addedToWorld()
    {
        int X = getX();
        int Y = getY();
        getImage().drawString("I JUST F*CKING DID IT", X, Y);
    }
// P.S. I did not do anything it does not work.
davmac davmac

2015/11/3

#
The x and y parameters to the drawString method are co-ordinates in the image, not in the world. You're passing the result of getX() and getY() - which are world coordinates. Try passing x = 0 and y = 20 (you may have to adjust the y value to position the text correctly). And please refrain from profanity (even semi-censored) in this forum.
1
getImage().drawString("I JUST DID IT", 0, 20);
EgoInfinite EgoInfinite

2015/11/5

#
Dear davman, Thanks for the response, but nothing is showing up at all, It could be the font is white and the string is being typed, but the image itself is a white box, also, the font could be black and is also being typed, but my world is completely black, thus there is no way to see the string. I'm going to change the font color and see if that works. P.S. Excuse my language, I will watch my mouth from now on. P.S.S. Here's my entire code in case you're interested.
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
113
114
115
116
117
118
119
120
121
// Code for the world which is a giant black square
import greenfoot.*;
public class START extends World
{
    public START()
    {
        super(1600, 900, 1);
        addObject(new MASTER(), 800, 400);
    }
}
 
// MASTER works perfectly fine
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;
public class MASTER extends Actor
{
    int TIME = 255;
    int SCRIPT = 0;
    public String TEXT = "";
    protected void addedToWorld()
        {
            int X = getX();
            int Y = getY();
        }
    public void act()
    {
        if(Greenfoot.mouseClicked(this) && SCRIPT == 0)
            {
                Greenfoot.playSound("THEME.mp3");
                while(TIME > 0)
                    {                          
                        Greenfoot.delay(1);
                        TIME--;
                        getImage().setTransparency(TIME);
                    }
                SCRIPT = 1;
            
        Greenfoot.delay(100);
        if(TIME == 0)
            {
                this.setLocation(1400, 800);
                Greenfoot.delay(1);
                getWorld().addObject(new INTRO(), 800, 350);
                Greenfoot.delay(1);
                getWorld().addObject(new LOGO(), 800, 350);
                while(TIME < 255)
                    {  
                    Greenfoot.delay(1);
                    this.setImage("MASTERPLAY.png");
                    TIME++;
                    getImage().setTransparency(TIME);
                    }
            }
        Greenfoot.delay(100);
        if(SCRIPT == 1)
            {
                Greenfoot.delay(1);
                getWorld().removeObjects(getWorld().getObjects(INTRO.class));
                while(TIME > 0)
                    {
                        Greenfoot.delay(1);
                        TIME--;
                        getImage().setTransparency(TIME);
                        SCRIPT = 2;
                    }
            }
        Greenfoot.delay(1);
        if(SCRIPT == 2)
            {
                this.setLocation(800, 450);
                this.setImage("MASTER.png");
                while(TIME < 255)
                    {
                        Greenfoot.delay(1);
                        TIME++;
                        getImage().setTransparency(TIME);
                        SCRIPT = 3;
                    }
            }
        Greenfoot.delay(1);
        if(SCRIPT == 3)
            {      
                int X = getX();
                int Y = getY();
                getWorld().addObject(new MASTERSAYS(), X, Y+200);
            }
    }
}
 
// The code for MASTERSAYS
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;
public class MASTERSAYS extends MASTER
{
    protected void addedToWorld()
    {
        int X = getX();
        int Y = getY();
        getImage().drawString("I JUST DID IT", 0, 30);
    }   
}
 
// Code for an image (2 images) that fades into the world then disappear
import greenfoot.*;
public class INTRO extends Actor
{
    int TIME = 0;
    protected void addedToWorld(World world)
        {
            getImage().setTransparency(TIME);
            Greenfoot.delay(1);
            while(TIME < 255)
                {
                    Greenfoot.delay(1);
                    TIME++;
                    getImage().setTransparency(TIME);
                }
        }
}
EgoInfinite EgoInfinite

2015/11/5

#
P.S.S.S. I don't know how to change the font color.
danpost danpost

2015/11/5

#
EgoInfinite wrote...
P.S.S.S. I don't know how to change the font color.
There is a 'setColor' method of the GreenfootImage class which sets the color for future drawing commands (like 'drawString').
You need to login to post a reply.