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

2015/12/30

Setting an image by top left coord

Chapaladeen Chapaladeen

2015/12/30

#
Okay, so I am creating a list of items in cluedo, and it would really help to be able to have them aligned on the left. Just showing you what it looks like at the moment. Can fix the overlapping text but want it to look neat as well.
danpost danpost

2015/12/30

#
Show the code you are currently using for creating and displaying the text you want left-aligned.
fejfo fejfo

2015/12/31

#
the problem is probebly that actors get set at the center of there coordinates so you need to add half the width and half the height to the location of the actor hope this helps
Chapaladeen Chapaladeen

2015/12/31

#
Here is the code. It is a bit FUBAR atm, but now it displays all the values as one thing so I only have to align this.
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import greenfoot.*;
import java.awt.Color;
/**
 * Write a description of class cardListText here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class cardListText extends Actor
{
    static String CardHolder;
    //declaring ints known & unknown and initialising them to 0. Declaring coordX and coordY.
    //coordX is a constant
    static public int coordX = 174, coordY, Scarlet=0,Peacock=0,Plum=0,White=0,Mustard=0,Green=0, unknown=0;
    /**
     * Act - do whatever the cardListText wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    public void act()
    {
        int holder;
        GreenfootImage thePlayer;
        if(gameWorld.listCards < 21){
            if (Game.cardArray[gameWorld.listCards][Characters.ActivePlayer] == 0){
                System.out.println(gameWorld.listCards);
                cardListMakeText(CardHolder + " Unknown");
            }
            if (Game.cardArray[gameWorld.listCards][Characters.ActivePlayer] == 1){
                cardListMakeText(CardHolder + " Miss Scarlet");
            }
            if (Game.cardArray[gameWorld.listCards][Characters.ActivePlayer] == 2){
                cardListMakeText(CardHolder + " Col. Mustard");
            }
            if (Game.cardArray[gameWorld.listCards][Characters.ActivePlayer] == 3){
                cardListMakeText(CardHolder + " Mrs White");
            }
            if (Game.cardArray[gameWorld.listCards][Characters.ActivePlayer] == 4){
                cardListMakeText(CardHolder + " Reverend Green");
            }
            if (Game.cardArray[gameWorld.listCards][Characters.ActivePlayer] == 5){
                cardListMakeText(CardHolder + " Mrs Peacock");
            }
            if (Game.cardArray[gameWorld.listCards][Characters.ActivePlayer] == 6){
                cardListMakeText(CardHolder + " Prof. Plum");
            }
            if (Game.cardArray[gameWorld.listCards][Characters.ActivePlayer] == 7){
                cardListMakeText(CardHolder + "7");
            }
        }
        if(gameWorld.listCards < 21){
            if (gameWorld.listCards == 1){
                CardHolder = "Colonel Mustard:";
            }
            if (gameWorld.listCards == 2){
                CardHolder = "Miss Scarlet:";
            }
            if (gameWorld.listCards == 3){
                CardHolder = "Professor Plum:";
            }
            if (gameWorld.listCards == 4){
                CardHolder = "Mrs. Peacock:";
            }
            if (gameWorld.listCards == 5){
                CardHolder = "Reverend Green:";
            }
            if (gameWorld.listCards == 6){
                CardHolder = "Mrs. White:";
            }
            if (gameWorld.listCards == 7){
                CardHolder = "Candlestick:";
            }
            if (gameWorld.listCards == 8){
                CardHolder = "Dagger:";
            }
            if (gameWorld.listCards == 9){
                CardHolder = "Lead Piping:";
            }
            if (gameWorld.listCards == 10){
                CardHolder = "Revolver:";
            }
            if (gameWorld.listCards == 11){
                CardHolder = "Rope:";
            }
            if (gameWorld.listCards == 12){
                CardHolder = "Spanner:";
            }
            if (gameWorld.listCards == 13){
                CardHolder = "Hall:";
            }
            if (gameWorld.listCards == 14){
                CardHolder = "Lounge:";
            }
            if (gameWorld.listCards == 15){
                CardHolder = "Dining Room:";
            }
            if (gameWorld.listCards == 16){
                CardHolder = "Kitchen:";
            }
            if (gameWorld.listCards == 17){
                CardHolder = "Ballroom:";
            }
            if (gameWorld.listCards == 18){
                CardHolder = "Conservatory:";
            }
            if (gameWorld.listCards == 19){
                CardHolder = "Billiard Room:";
            }
            if (gameWorld.listCards == 20){
                CardHolder = "Library:";
            }
            if (gameWorld.listCards == 21){
                CardHolder = "Study:";
                gameWorld.listCards = 0;
            }
            gameWorld.listCards++;
        }
   
    }   
    private void cardListMakeText(String text) {
        GreenfootImage image = new GreenfootImage(text, 20, Color.WHITE, new Color(0, 0, 0, 0));
        setImage(image);
    }
    static public void cardListText(){
        int holder;
        unknown++;
        if(unknown > 0){
            holder = unknown * 24;
            coordY = holder + 150;
        }
        if(unknown > 6){
            holder = unknown * 24;
            coordY = holder + 200;
        }
        if(unknown > 12){
            holder = unknown * 24;
            coordY = holder + 250;
        }
    }
}
Chapaladeen Chapaladeen

2015/12/31

#
**Changed the code around so that the string part is before the part to generate the text. I've also been having a problem where the
1
2
3
if (gameWorld.listCards == 18){
    CardHolder = "Billiard Room:";
}
part are all meant to be one less with the == and it is meant to start with 0. If I do that then the text runs up the screen constantly like a wheel (moves off the top and starts from the bottom position... Hoping I can fix that but if not then i'll need some advice.
Chapaladeen Chapaladeen

2015/12/31

#
**Fixed the moving text issue.
danpost danpost

2015/12/31

#
Drawing the text image at the left side of a set length image would left-align the text of the actors. Adjust the x-location of the actor and the size of 'image' as needed:
1
2
3
4
5
6
private void cardListMakeText(String text) {
    GreenfootImage txtImg = new GreenfootImage(text, 20, Color.WHITE, null);
    GreenfootImage image = new GrenfootImage(200, txtImg.getHeight());
    image.drawImage(txtImg, 0, 0);
    setImage(image);
}
Chapaladeen Chapaladeen

2015/12/31

#
Awesome! That works! Thanks very much.
You need to login to post a reply.