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

2012/9/6

Counter Class error

1
2
3
4
ManiHallam ManiHallam

2012/9/6

#
in the Counter when compile it, this error comes up "incompatible types - found java.lang.Object but expected Money" because of int value in line 33 "get()"
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Chocolate Dispenser Machine
 *
 */
public class Counter extends Actor
{
    private int credit;
    private boolean moneyFound  = false;

    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        coinFound();
        noteFound();
        getCredit();
        update();
    }

    /**
     * Counting all Coins in Money class, which are inserted into the Coin Insertion.
     */
    public void coinFound()
    {
        if (!moneyFound && !getWorld().getObjectsAt(517, 314, Money.class).isEmpty())
        {
            moneyFound = true; 
            Money money = getWorld().getObjectsAt(517, 314, Money.class).get(0);
            credit += money.getValue();
        }

        if (moneyFound && getWorld().getObjectsAt(517, 314, Money.class).isEmpty())
        {
            moneyFound = false;
        }
    }

    /**
     * Counting all Notes in Money class, which are inserted into the Note Insertion.
     */
    public void noteFound()
    {
        if (!moneyFound && !getWorld().getObjectsAt(517, 314, Money.class).isEmpty())
        {
            moneyFound = true;
            Money money = getWorld().getObjectsAt(517, 314, Money.class).get(0);
            credit += money.getValue();
        }

        if (moneyFound && getWorld().getObjectsAt(517, 314, Money.class).isEmpty())
        {
            moneyFound = false;
        }
    }
    
    /**
     * 
     */
    public int getCredit()
    {
        return credit;
    }
    
    /**
     * 
     */
    public void update()
    {
        img.drawString("Credit: " + credit, 10, 60);
        setImage(img);
    }
}
MatheMagician MatheMagician

2012/9/6

#
First, import java.util.List. Second, replace line 33 with this:
List<Money> money2 = getWorld().getObjectsAt(517, 314, Money.class); 
Money money = money2.get(0); 
and that should do it.
danpost danpost

2012/9/7

#
ManiHallam, what happened to the rest of your 'update()' method? Change line 33 to:
Money money = (Money) getWorld().getObjectsAt(517, 314, Money.class).get(0);
Just needed to cast the object with '(Money)'.
ManiHallam ManiHallam

2012/9/7

#
MatheMagician wrote...
First, import java.util.List. Second, replace line 33 with this:
List<Money> money2 = getWorld().getObjectsAt(517, 314, Money.class); 
Money money = money2.get(0); 
and that should do it.
Thanks MatheMagician, just could you let me know that, what you mean by "List<Money>?
ManiHallam ManiHallam

2012/9/7

#
Thanks topmost, another error comes up which is about "getValue()" in line 34, because getValue is not a method
danpost danpost

2012/9/7

#
danpost wrote...
You will have to add the following method in the Money class:
public int getValue()
{
    return n;
}
The above was in the other discussion. Make sure you put it in the 'Money' class (not in any of its sub-classes).
ManiHallam ManiHallam

2012/9/7

#
at the moment I have still problem about getting message on the world. I am wondering that, can you send a text or message like updating Counter to be displayed on another class image? or it should be only on the world image?
ManiHallam ManiHallam

2012/9/7

#
oh, sorry, It was my fault. I had typed get value instead of capital letter. Thanks.
danpost danpost

2012/9/7

#
It is best to not use the world image, as removing it completely could be troublesome (if the background is not a flat color; if it is, it would require refilling that area with the color, where an object you can just remove or change the image of easily). Usually, for any object that changes appearance, moves, or shows and hides, it is best to have an Actor sub-class for it. What object are you referring to? and what is it used for?
ManiHallam ManiHallam

2012/9/7

#
Actually, I was thinking of Screen class which has got a plain white colour. I thought it might be the reason that I couldn't get any messages on it when I click on coins and notes, or even nothing from Counter
danpost danpost

2012/9/7

#
Can you post the code for the Screen class?
ManiHallam ManiHallam

2012/9/7

#
Dear topmost, you said about update, do you mean update about Message class? anyway I attach the code here. just in case if you meant.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Chocolate Dispenser Machine
 *
 */
public class Message extends Actor
{
    /**
     *  
     * 
     */
    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);  
    }  
}  
ManiHallam ManiHallam

2012/9/7

#
there is nothing in it, just:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Chocolate Dispenser Machine
 *
 */
public class Screen extends Body
{
    /**
     * Act - do whatever the Screen wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
    
    
}
danpost danpost

2012/9/7

#
ManiHallam wrote...
Dear topmost, you said about update, do you mean update about Message class?
I was referring to your Counter class. You had more code to create the image for it, but it is not showing on the initial post on this discussion (just the change part is showing).
danpost danpost

2012/9/7

#
Use this for your screen class:
import greenfoot.*;
import java.awt.Color;

public class Screen extends Body
{
    public Screen()
    {
        setText(" ");
    }

    public void setText(String text);
    {
        GreenfootImage image = new GreenfootImage(185, 75); // whatever size the image is
        image.setColor(Color.white);
        image.fill();
        int fontSize = 36; // adjust to fit
        GreenfootImage txtImg = new GreenfootImage(text, fontSize, Color.blue, Color.white);
        int across = (image.getWidth() - txtImg.getWidth()) / 2;
        int down = (image.getHeight() - txtImg.getHeight()) / 2;
        image.drawImage(txtImg, across, down);
        setImage(image);
    }
}
Create only one of this type object at world creation, and any class can then use:
Screen screen = (Screen) getWorld().getObjects(Screen.class).get(0);
screen.setText("New text");
If in the world class, drop 'getWorld().'.
There are more replies on the next page.
1
2
3
4