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
This is part of a method inside my picture class.
Here is my Text class
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.
Text message = new Text(text);
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;
}
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();
}
