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

2013/11/22

Need help understanding code

Gummy Gummy

2013/11/22

#
Hello, I am new to Greenfoot and Java programming and I have a question as to what exactly is going on when I execute this line of code
Text message = new Text(text);
This is part of a method inside my picture class.

   public void encryptText(Picture pic, String text)
   {
       Picture oPic = pic; //original picture
       Text message = new Text(text);
       int x1,
           y1,
           x2 = 0,
           y2 = 0,
           picWidth = oPic.getImage().getWidth(),
           picHeight = oPic.getImage().getHeight(),
           msgWidth = message.getImage().getWidth(),
           msgHeight = message.getImage().getHeight();
       
       if(oPic.getImage().getWidth() % 2 != 0)
           picWidth = oPic.getImage().getWidth() - 1;
       if(oPic.getImage().getHeight() % 2 != 0)
           picHeight = oPic.getImage().getHeight() - 1;
       if(message.getImage().getWidth() % 2 != 0)
           msgWidth = message.getImage().getWidth() - 1;
       if(message.getImage().getHeight() % 2 != 0)
           msgHeight = message.getImage().getHeight() - 1;
       
       for(x1 = 0; x1 < picWidth; x1++)
       {
           for(y1 = 0; y1 < picHeight; y1++)
           {
               Pixel pix = new Pixel(oPic, x1, y1);
               
               if(pix.getRed() % 2 != 0)
               {
                   pix.setRed(pix.getRed() - 1);//if it's odd, make it even
               }
               
               if(  
                    x1 >= picWidth/2 - msgWidth/2
                    &&
                    x1 <= picWidth/2 + msgWidth/2
                    &&
                    y1 >= picHeight/2 - msgHeight/2
                    &&
                    y1 <= picHeight/2 + msgHeight/2 )
               {
                   Pixel msgPix = new Pixel(message, x2, y2);
                   if(msgPix.getRedImg() == 255)// it's a message pixel!
                   {
                       if(pix.getRed() == 0)
                           pix.setRed(1);
                       if(pix.getRed() % 2 == 0)
                           pix.setRed(pix.getRed() - 1); // if pic is even make it odd
                   }
                   
                   if(y2 == msgHeight)
                   {
                        y2 = 0;
                        x2++;
                   }
                   else
                        y2++;
               }
               
           }
       }
       pic = oPic;
   }
   
Here is my Text class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Text here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Text extends Picture
{
    /**
     * Act - do whatever the Text wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Text(String text)
    {
        GreenfootImage img = new GreenfootImage(text.length()*10, 30);
        img.setColor(new Color(255,255,255));
        img.fill();
        img.setColor(new Color(0,0,0));
        img.drawString(text, 20, 2);
        setImage(img);
        this.img = img;
    }

    public void act()
    {
    }
    private GreenfootImage img = getImage();
}
When I double left click and add a new Text object, it creates a box of text and prompts me for a string; It then creates the text box. I am confused as to what it is doing when I create a Text object using actual code. When the code is compiled my picture object will change; However, I do not have a text box appear as I do when I manually double left click and drag and drop it to my world. If someone could help me to understand what exactly it is I am doing, I would be very appreciative.
danpost danpost

2013/11/22

#
When the scenario is stopped and you right click on the 'Text' class icon, then click somewhere in the world and enter your String data, you are actually adding the object into the world at the location you clicked on in the world. In the code in question, you are creating the object; but, it does not automatically get added into the world. You must use the World class method 'addObject' to add the actor into the world.
Gummy Gummy

2013/11/22

#
Okay, so when I create the object with code, I have to add the object to the world for it to appear? How would that code work since I can't directly say
addObject(message, 1 , 1);
?
danpost danpost

2013/11/22

#
You would use both lines of code. The initial line in question to create the object followed by the statement to add it into the world. Review the code that is put into the 'prepare' method when you 'Save the world'.
You need to login to post a reply.