coder04 wrote...
I changed what you said and added a act method what do I put In it?
public void act()
{
String codedString = Cipher.converString("hello", a);
String decodedString = Cipher.convertString(codedString, -key);
}import greenfoot.*;
/**
* creates a simple textbox object
*/
public class Textbox extends Actor
{
private String text = ""; // the current text
/** calls String argument constructor with an empty string */
public Textbox()
{
this("");
}
/**
* creates initial image of textbox and sets initial text
* @param txt the initial text string value for this textbox
*/
public Textbox(String txt)
{
GreenfootImage image = new GreenfootImage(300, 30);
image.fill();
image.setColor(java.awt.Color.white);
setImage(image);
setText(txt);
}
/**
* sets text value and adds image of text to textbox image
* @param the text string value to be assigned to this textbox
*/
public void setText(String txt)
{
text = txt;
getImage().fillRect(2, 2, 296, 26);
GreenfootImage tImg = new GreenfootImage(text, 24, null, null);
int x = tImg.getWidth() < 290 ? 5 : 290-tImg.getWidth();
getImage().drawImage(tImg, x, 3);
getImage().fillRect(2, 2, 2, 26);
}
/**
* returns the current text string value assigned to this textbox
* @return the current value of the text string assigned to this textbox
*/
public String getText()
{
return text;
}
}import greenfoot.*;
/**
* a world to demonstrate Ceasar Ciphering
*/
public class CipherWorld extends World
{
private Textbox textboxA, textboxB; // the text boxes
private int codeKey; // the cipher key
/** prepares world */
public CipherWorld()
{
super(380, 500, 1);
addObject(textboxA = new Textbox(), getWidth()/2, 40);
addObject(textboxB = new Textbox(), getWidth()/2, 80);
}
/** processes keyboard input and maintains string values of textboxes */
public void act()
{
String key = Greenfoot.getKey();
if (key != null)
{
if ("space".equals(key)) key = " ";
if ("backspace".equals(key) && textboxA.getText().length() > 0)
{
String text = textboxA.getText();
text = text.substring(0, text.length()-1);
textboxA.setText(text);
}
if ("escape".equals(key)) textboxA.setText("");
if ("up".equals(key)) codeKey = (codeKey+1)%26;
if ("down".equals(key)) codeKey = (codeKey+25)%26;
if (key.length() == 1) textboxA.setText(textboxA.getText()+key);
textboxB.setText(Cipher.convertString(textboxA.getText(), codeKey));
}
}
}public abstract class Cipher
{
public static char convertChar(char c, int offset)
{
if (c<=64 || (c>90 && c<=96) || c>122) return c;
int base = 32*((c-1)/32);
return (char)(base+1+(((c-1)-base+26+offset)%26));
}
public static String convertString(String str, int offset)
{
String converted = "";
for (int i=0; i<str.length(); i++) converted += convertChar(str.charAt(i), offset);
return converted;
}
}