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

2012/9/7

Showing messages from Classes and updated messages from Counter

1
2
3
4
ManiHallam ManiHallam

2012/9/7

#
I have got problem about displaying messages on the image of Screen class, which is plain white colour, and want to display different messages on it from different instructions. Any help please? this is one of my Money's Subclass' code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Chocolate Dispenser Machine
 * @version 1.0
 */
public class FiftyPence extends Money
{
    private int n = 50;  
    private long markTime = 0;
    GreenfootImage image = null;  

    public FiftyPence()  
    {  
        image = new GreenfootImage(getImage());  
    }  

    public void act()   
    {  
        if (markTime == 0 && Greenfoot.mouseClicked(this))  
        {  
            setLocation(517, 314);  
            credit(); 
            markTime = System.currentTimeMillis();  
        }  

        if (markTime != 0 && System.currentTimeMillis() - markTime > 1000)  
        {  
            markTime = 0;  
            setLocation(555, 453);  
            setImage(new GreenfootImage(image));  
        }  
    }  

    private void credit()
    {
        GreenfootImage img = getImage();
        img.setColor(Color.BLUE);
        img.drawString("Credit: " + n, 593, 252);
    }
}
and this one is Message Class, Actor's subClass, code:
public class Message extends Actor
{
    /**
     *  
     * 
     */
    public void act()
    {
        //
    }
    
    public Message(String text)  
    {  
        updateImage(text);  
    }  
  
    private void updateImage(String message)  
    {  
        setImage(new GreenfootImage(message, 20, Color.BLUE, new Color(0, 0, 0, 0)));  
    }  
  
    public void setText(String text)  
    {  
        updateImage(text);  
    }  
}  
danpost danpost

2012/9/7

#
Get rid of the Message class and use the new Screen class in the other discussion. And delete line 40 in the Counter class, and insert at that location the new 'drawString' and 'setImage' lines that you need there. That should fix your displaying of texts.
ManiHallam ManiHallam

2012/9/7

#
Thanks, i'll try it now
ManiHallam ManiHallam

2012/9/7

#
just to let you know that, the default image for Screen class as the start message to give instruction to user is as: Background= White, and some instruction text on it. (Ill send you link for the image) Also I love your SuDoku Scenario, the design is intelligent and mathematical, well done. but the trick is so easy. ;-)
ManiHallam ManiHallam

2012/9/7

#
Can I use it as default Screen image just for start of the application? I think it should be alright, as much as I know.
danpost danpost

2012/9/7

#
In the constructor for the Screen class, use the following for the String parameter in the call to 'setText' (instead of " "): "Credit: 0\nPlease click\non appropriate\nmoney to start." The '\n' starts a new line. Also, with 4 lines of text, change the fontSize to 'int fontSize = 20;'.
danpost danpost

2012/9/7

#
What do you mean by 'the trick is so easy' as far as my SuDoku scenario?
ManiHallam ManiHallam

2012/9/7

#
wow! good Idea Thanks
ManiHallam ManiHallam

2012/9/7

#
what I actually find, if you find out the number for centre block, so you can fill in the 9 blocks in the centre. but to fill others, what I did for an unsolved 9 blocks, you can add a3 blocks column from the next solved 9 ones and fill the opposite column. That was the way I filled some of them.
danpost danpost

2012/9/7

#
Yes, well that sort of goes along with the rules. Since you already have those numbers in that column or row in one block, the column or row in an adjacent block must have those numbers in a different column or row. Try several puzzles, you will find some to be very difficult (since they are created randomly). And, by the way, the program actually creates the puzzles, they are not chosen from any list.
ManiHallam ManiHallam

2012/9/7

#
But it was interesting for me that Grennfoot has got that potential. Also definitely you are a genius, nobody can do it except likes Maths. Actually I believe that Programming and Overall Computing means Maths.
ManiHallam ManiHallam

2012/9/7

#
danpost wrote...
And delete line 40 in the Counter class, and insert at that location the new 'drawString' and 'setImage' lines that you need there.
I couldn't get it. I am actually confused, because in drawString it has the location, so we don't need to use setImage() to set the location for image. What do you thing? If I am wrong could you please let me know how?
danpost danpost

2012/9/7

#
'drawString' draws one image onto another. The location is the offset from the top-left corner of the image being drawn on. So, if you are drawing the image of 'txtImg' onto the image 'image', the location is the offset of the top-left corner of 'image' to where you want the top-left corner of 'txtImg'. Under normal circumstances, if the image to be drawn on ('image') is of size (wide, high), the value of the offsets to draw the image ('txtImg') would be within the following ranges: the horizontal offset: (0) to (wide - txtImg.getWidth()) the vertical offset: (0) to (high - txtImg.getHeight()) assuming the the image being drawn onto is large enough to hold the image being drawn. The 'drawString' statement just changes the appearance of the GreenfootImage object 'image'; we still need to change the image of the Screen object with 'setImage' (maybe you are confused because when drawing on the world background image, which is a 'static' object, you do not need to 'setBackground' after changing the image). There is no 'location' in the 'setImage' statement, so I do not understand why you say 'so we don't need to use setImage() to set the location for image'. Hope this clear things up for you.
danpost danpost

2012/9/7

#
To elaborate, the image of the background of the world is inherently 'part' of the world itself. Whereas, the image that is set to an actor is not 'part' of the actor, just something the actor uses. If you use 'GreenfootImage bg = getBackground();' in the world, you are accessing the background image that belongs to the world. Altering the appearance of 'bg' will automatically change the background image of the world. However, if you use 'GreenfootImage image = getImage();' in an actor sub-class, you are accessing the GreenfootImage that has previously been set to the actor (like getting a copy of it), and altering it has no effect on the image of the actor; so, we need to re-set the image of the actor image after altering it.
davmac davmac

2012/9/8

#
If you use 'GreenfootImage bg = getBackground();' in the world, you are accessing the background image that belongs to the world. Altering the appearance of 'bg' will automatically change the background image of the world. However, if you use 'GreenfootImage image = getImage();' in an actor sub-class, you are accessing the GreenfootImage that has previously been set to the actor (like getting a copy of it), and altering it has no effect on the image of the actor; so, we need to re-set the image of the actor image after altering it.
That's not correct; getImage() behaves the same for an actor as it does for the world.
There are more replies on the next page.
1
2
3
4