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

2014/6/30

Broken Scenario

basil60 basil60

2014/6/30

#
Hi I've got this scenario (assume it came from this site, although I couldn't find it when I searched). Two problems I appear to have is that I can't seem to input in the text boxes - it just doesn't seem to enter or be visible. The other is when I try to un-comment the multiply method, it says it can't find the variables textbox3 or textbox2. I'd appreciate any help. The zip of the file is at: http://goo.gl/HMr6Gh
danpost danpost

2014/6/30

#
Please post the relevant code on this site. I, for one (as I am sure others are), am reluctant to download files from unknown sources).
basil60 basil60

2014/7/2

#
Apologies - trying to minimise code dumps. Also uploaded the scenario so people could see it - http://www.greenfoot.org/scenarios/11765 Label
public class Label extends Actor
{
    public static final int LEFT = 0;
    public static final int CENTER = 1;
    public static final int RIGHT = 2;
    
    // colours and other user definable attributes with default values
    private Color textColor = Color.BLACK;
    private Color backgroundColor = null;
    private Color borderColor = null;
    private int alignment = CENTER;
    
    // spacing, with defaiult values
    private int inset = 8;  // space between text and edge
    
    private String text;
    private int baseline;   // the y-coordinate of the text baseline
    private int textX;      // the x-coordinate of the left edge of the text
    private int width = -1;
    private int height = -1;
    
    /**
     * Create a label with a default text.
     */
    public Label()
    {
        this("Label");
    }
    
    /**
     * Create a label with a given text.
     */
    public Label(String text)
    {
        this.text = text;
        createImage();
    }
    
    /** 
     * Set the text to be displayed.
     */
    public void setText(String text)
    {
        this.text = text;
        createImage();
    }
    
    /**
     * Return this label's text.
     */
    public String getText()
    {
        return text;
    }
    
    /** 
     * Set the font for the label text.
     */
    public void setFont(Font font)
    {
        getImage().setFont(font);
        createImage();
    }
    
    /**
     * Return this label's font.
     */
    public Font getFont()
    {
        return getImage().getFont();
    }
    
    /** 
     * Set the background color.
     */
    public void setBackground(Color backgroundColor)
    {
        this.backgroundColor = backgroundColor;
        createImage();
    }
    
    /** 
     * Set the foreground color.
     */
    public void setForeground(Color foregroundColor)
    {
        this.textColor = foregroundColor;
        createImage();
    }
    
    /** 
     * Set the border color.
     */
    public void setBorder(Color borderColor)
    {
        this.borderColor = borderColor;
        createImage();
    }
    
    /** 
     * Set the text alignment. The value should be one of the alignment constants defined in this 
     * class (LEFT, CENTER, RIGHT).
     */
    public void setAlignment(int alignment)
    {
        this.alignment = alignment;
        createImage();
    }
    
    /** 
     * Set the width of this label to a fixed width.
     */
    public void setWidth(int width)
    {
        this.width = width;
        createImage();
    }
    
    /** 
     * Set the height of this label to a fixed height.
     */
    public void setHeight(int height)
    {
        this.height = height;
        createImage();
    }
    
    /**
     * Override setLocation to position the label's top left corner at the given location.
     */
    public void setLocation(int x, int y)
    {
        super.setLocation (x + getImage().getWidth()/2, y + getImage().getHeight()/2);
    }
    
    // --------------------------- private methods ---------------------------------
    /**
     * Paint the label image, including the button text and decorations.
     */
    private void repaint()
    {
        paintBackground();
        paintBorder();
        paintText();
    }

    /**
     * Paint the label's background.
     */
    private void paintBackground()
    {
        if (backgroundColor != null) {
            GreenfootImage img = getImage();
            img.setColor(backgroundColor);
            img.fill();
        }
    }
    
    /**
     * Paint the label's text.
     */
    private void paintText()
    {
        GreenfootImage img = getImage();
        img.setColor(textColor);
        img.drawString(text, textX, baseline);
    }
    
    /**
     * Paint the label's border.
     */
    private void paintBorder()
    {
        if (borderColor != null) {
            GreenfootImage img = getImage();
            img.setColor(borderColor);
            img.drawRect(0, 0, img.getWidth()-1, img.getHeight()-1);
        }
    }
        
    /**
     * Create the actor's image at the right size. If the size has been set explicitly, that size 
     * is used. If not, the size is calculated  to fit the label text. The size calculation takes 
     * the text on the label and the current font into account.
     */
    private void createImage()
    {
        int imgWidth = width;
        int imgHeight = height;
        
        GreenfootImage img = new GreenfootImage(10, 10);
        Graphics gc = img.getAwtImage().getGraphics();
        FontMetrics fm = gc.getFontMetrics();
        
        int textWidth = fm.stringWidth(text);
        if (imgWidth <= 0) {
            imgWidth = textWidth + 2 * inset;
        }
        if (imgHeight <= 0) {
            imgHeight = fm.getHeight() + 2 * inset;
        }
        
        baseline = (imgHeight/2) + (fm.getAscent()/2);
        if (alignment == LEFT) {
            textX = inset;
        }
        else if (alignment == RIGHT) {
            textX = imgWidth - inset - textWidth;
        }
        else { // center
            textX = (imgWidth/2) - (textWidth/2);
        }
        img.scale(imgWidth, imgHeight);
        setImage(img);
        repaint();
    }
}
TextBox
public class TextBox extends Actor
{
    public static final int LEFT = 0;
    public static final int CENTER = 1;
    public static final int RIGHT = 2;

    private static final String INSERTION_CHAR = "\u2502";

    // colours and other user definable attributes with default values
    private Color textColor = Color.BLACK;
    private Color backgroundColor = Color.WHITE;
    private Color backgroundColorNormal = backgroundColor;
    private Color backgroundColorFocused = Color.CYAN;
    private Color borderColor = null;
    private int alignment = CENTER;

    // spacing, with defaiult values
    private int inset = 8;  // space between text and edge

    private String text;
    private int baseline;   // the y-coordinate of the text baseline
    private int textX;      // the x-coordinate of the left edge of the text
    private int width = -1;
    private int height = -1;

    private greenfoot.MouseInfo mouse;
    private Actor mTarget; 
    private Actor mOver; 
    private int posFromEnd = 0;
    private boolean hasFocus = false;
    private Font myFont = new Font("Calibri", Font.BOLD, 12);
    
    /**
     * Create a label with a default text.
     */
    public TextBox()
    {
        this("TextBox");
    }

    /**
     * Create a Text Box with a given text.
     */
    public TextBox(String text)
    {
        this.text = text;
        createImage();
    }

    public void act() {
        checkFocus();   // To check if we have focus
        if (hasFocus) {
            changeText();
        }
    }

    /** 
     * Set the text to be displayed.
     */
    public void setText(String text)
    {
        this.text = text;
        createImage();
    }

    /**
     * Return this label's text.
     */
    public String getText()
    {
        return text;
    }

    /** 
     * Set the font for the label text.
     */
    public void setFont(Font font)
    {
        getImage().setFont(font);
        createImage();
    }

    /**
     * Return this label's font.
     */
    public Font getFont()
    {
        return getImage().getFont();
    }

    /** 
     * Set the background color.
     */
    public void setBackground(Color bgColor)
    {
        this.backgroundColor = bgColor;
        createImage();
    }

    /** 
     * Set the foreground color.
     */
    public void setForeground(Color fgColor)
    {
        this.textColor = fgColor;
        createImage();
    }

    /** 
     * Set the border color.
     */
    public void setBorder(Color borderColor)
    {
        this.borderColor = borderColor;
        createImage();
    }

    /** 
     * Set the text alignment. The value should be one of the alignment constants defined in this 
     * class (LEFT, CENTER, RIGHT).
     */
    public void setAlignment(int alignment)
    {
        this.alignment = alignment;
        createImage();
    }

    /** 
     * Set the width of this label to a fixed width.
     */
    public void setWidth(int width)
    {
        this.width = width;
        createImage();
    }

    /** 
     * Set the height of this label to a fixed height.
     */
    public void setHeight(int height)
    {
        this.height = height;
        createImage();
    }

    /**
     * Override setLocation to position the label's top left corner at the given location.
     */
    public void setLocation(int x, int y)
    {
        super.setLocation (x + getImage().getWidth()/2, y + getImage().getHeight()/2);
    }

    // --------------------------- private methods ---------------------------------

    /**
     * 
     * Work out if this TextBox has focus
     */

    private void checkFocus() {
        mouse = Greenfoot.getMouseInfo();
        if (mouse != null) {
            mOver = mouse.getActor();
            if (mOver == null) {
                if (hasFocus == true) {
                    lostFocus();
                    hasFocus = false;
                }
            }
            else {
                if (mouse.getClickCount() > 0) {
                    mTarget = mOver;
                    if (mTarget == this) {
                        if (hasFocus == false) {
                            gainedFocus();
                        }
                        hasFocus = true;
                    }
                    else {
                        if (hasFocus == true) {
                            lostFocus();
                        }
                        hasFocus = false;    
                    }
                }
            }
        }
    }

    /** 
     * 
     * Any actions required to draw user attention to the fact that we have focus
     */
    private void gainedFocus() {
        posFromEnd = 0;
        insertChar(INSERTION_CHAR, posFromEnd);
        backgroundColor = backgroundColorFocused;
        setBackground(backgroundColor);
    }


    /** 
     * 
     * Reverse effect of gainedFocus()
     */
    private void lostFocus() {
        removeInsertionChar();
        backgroundColor = backgroundColorNormal; 
        setBackground(backgroundColor);
    }

    /**
     * Actually change the text in the box
     * 
     */
    private void changeText() {
        if (hasFocus == true) {
            String S = Greenfoot.getKey();
            if (S != null) {
                if (S ==  "backspace") {
                    if (this.text.length() > 0) {
                        removeInsertionChar();
                        int len = this.text.length();
                        int pos = len + posFromEnd;
                        if (pos < 1) {
                            return; // We're at start so can't backspace
                        }
                        if (pos >= len) {
                            pos = len;
                        }
                        setText(this.text.substring(0,pos-1)+this.text.substring(pos));
                       
                    }
                    insertChar(INSERTION_CHAR, posFromEnd);
                    return;
                }
                if (S ==  "left") {
                    posFromEnd--;
                    insertChar(INSERTION_CHAR, posFromEnd);
                    return;
                }
                if (S ==  "space") {
                    S = " ";
                }
                if (S ==  "escape") {
                    lostFocus();
                    return;
                }
                if (S ==  "right") {
                    posFromEnd++;
                    insertChar(INSERTION_CHAR, posFromEnd);
                    return;
                }
                if (posFromEnd == 0) {
                    setText(getText() + S);
                    return;
                }
                else {
                    insertChar(S + INSERTION_CHAR, posFromEnd);
                }
            }
        }
    }


    /**
     * Remove the char which shows a user where in the string text will be inserted
     * 
     */

    private void removeInsertionChar() {
        String tmpString = getText().replace(INSERTION_CHAR,"");
        setText(tmpString);

    }

    /**
     * Show a char to tell the user where new text will be inserted
     */

    private void insertChar(String newChar, int posFromEnd) {
        removeInsertionChar();
        int len = getText().length();
        int splitPos = len + posFromEnd;
        if (splitPos < 0) {
            splitPos = 0;
        }
        if (splitPos >= len) {
            splitPos = len;
        }
        String S1 = getText().substring(0,splitPos);
        String S2 = getText().substring(splitPos,len);
        setText(S1 + newChar + S2);

    }

    /* METHODS FROM LABEL */ 

    /**
     * Paint the label image, including the button text and decorations.
     */
    private void repaint()
    {
        paintBackground();
        paintBorder();
        paintText();
    }

    /**
     * Paint the label's background.
     */
    private void paintBackground()
    {
        if (backgroundColor != null) {
            GreenfootImage img = getImage();
            img.setColor(backgroundColor);
            img.fill();
        }
    }

    /**
     * Paint the label's text.
     */
    private void paintText()
    {
        GreenfootImage img = getImage();
        img.setColor(textColor);
        img.setFont(myFont);
        img.drawString(text, textX, baseline);
    }

    /**
     * Paint the label's border.
     */
    private void paintBorder()
    {
        if (borderColor != null) {
            GreenfootImage img = getImage();
            img.setColor(borderColor);
            img.drawRect(0, 0, img.getWidth()-1, img.getHeight()-1);
        }
    }

    /**
     * Create the actor's image at the right size. If the size has been set explicitly, that size 
     * is used. If not, the size is calculated  to fit the label text. The size calculation takes 
     * the text on the label and the current font into account.
     */
    private void createImage()
    {
        int imgWidth = width;
        int imgHeight = height;

        GreenfootImage img = new GreenfootImage(10, 10);
        Graphics gc = img.getAwtImage().getGraphics();
        FontMetrics fm = gc.getFontMetrics();

        int textWidth = fm.stringWidth(text);
        if (imgWidth <= 0) {
            imgWidth = textWidth + 2 * inset;
        }
        if (imgHeight <= 0) {
            imgHeight = fm.getHeight() + 2 * inset;
        }

        baseline = (imgHeight/2) + (fm.getAscent()/2);
        if (alignment == LEFT) {
            textX = inset;
        }
        else if (alignment == RIGHT) {
            textX = imgWidth - inset - textWidth;
        }
        else { // center
            textX = (imgWidth/2) - (textWidth/2);
        }
        img.scale(imgWidth, imgHeight);
        setImage(img);
        repaint();
    }

}
World
public class test_world  extends World
{
    int i = 0;
    
    /**
     * Constructor for objects of class test_world.
     * 
     */
    public test_world()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
       

        //TextBox tbOne = new TextBox("Hello World");
        //addObject(tbOne, 100,100);

        //TextBox tbTwo = new TextBox("Goodbye");
        //addObject(tbTwo, 100,200);
        
        //initialise label Pay Calculator
        /*Label lOne = new Label("Pay Calculator");
        addObject(lOne, 100,200);
        //initialise Input A
        TextBox tbThree = new TextBox("");
        addObject(tbThree, 100,200);
        
        //initialise label Hours
        Label lTwo = new Label("Hours");
        addObject(lTwo, 100,200);
        prepare();
        
        //initialise Input B
        TextBox tbFour = new TextBox("");
        addObject(tbFour, 100,200);
        
        //initialise label Pay
        Label lThree = new Label("Hours");
        addObject(lThree, 100,200);*/
        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        /*TextBox textbox = new TextBox();
        addObject(textbox, 290, 164);
        textbox.setText("Jump");
        TextBox textbox2 = new TextBox();
        addObject(textbox2, 297, 95);
        textbox2.setText("Lorem Ipsum");
        removeObject(textbox2);
        removeObject(textbox);*/
        
          //create label Pay Calculator
        Label label1 = new Label();
        addObject(label1, 128,2);
        label1.setText("Pay Calculator");
        
        //create Input box A
        TextBox textbox3 = new TextBox();
        addObject(textbox3, 2,42);
        textbox3.setText("");
      
         //create label Hours
        Label label2 = new Label();
        addObject(label2, 52,42);
        label2.setText("Hours");
        
        //create Input box B
        TextBox textbox4 = new TextBox();
        addObject(textbox4, 160,42);
        textbox4.setText("");
      
         //create label Pay
        Label label3 = new Label();
        addObject(label3, 202,42);
        label3.setText("Pay");
        
         //create label Total Pay
        Label label4 = new Label();
        addObject(label4, 128,102);
        label4.setText("Total Pay");
        
         //create label ro Display Total Pay
        Label label5 = new Label();
        addObject(label5, 128,142);
        label5.setText("");
    }
}
danpost danpost

2014/7/2

#
I found the scenario with the Label class (UILibrary), but it does not contain a TextBox class. It appears someone may have taken the Label class and modified it in an attempt to make a TextBox class out of it. Obviously, the class has bugs. I think the TextBox objects lose focus immediately after gaining it. When I press a number and then click on a textbox, then the number will appear.
danpost danpost

2014/7/2

#
As far as the 'multiply' method -- I do not see one anywhere.
You need to login to post a reply.