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

2017/4/29

Array and addObject Help

UnusualGorilla UnusualGorilla

2017/4/29

#
So I am trying to make it so that if I press the "L" key a file dialog will pop up, and take three integers (the rgb colors) to create a new ball. However, the only thing that happens is the first ball will be added to the world, but the rest of the balls are not added to the world. Could anyone help me figure out what I am doing wrong? I have included the three classes that are needed for this code.
public class MyWorld extends World
{
    private BallInfo[] bArray;
    private int counter;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
          //Create a new world with 800x600 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        
        addObject(new Paddle(), 395, 580);
    }
    
    /**
     *   YOU WILL MOST LIKLEY WANT TO ADD SOME CODE HERE. 
     */
    public void act()
    {
        if (Greenfoot.isKeyDown("l") || Greenfoot.isKeyDown("L")) {
      
        FileDialog fd = null;
        fd = new FileDialog(fd, "Choose A File", FileDialog.LOAD);
        fd.setVisible(true);
        String fname = fd.getDirectory() + fd.getFile();
        File f = new File(fname);
            
        Scanner s;
        try{
         s= new Scanner(f);
        }
        catch (FileNotFoundException e)
        {
                     return;
        }
        bArray = new BallInfo[1000]; 
        counter=0;
        
        while (s.hasNext() ) {
                   int redValue, greenValue, blueValue;
                   int xPos = 10;
                   redValue = s.nextInt();
                   greenValue = s.nextInt();
                   blueValue = s.nextInt();
                   
                   BallInfo bInfo = new BallInfo(redValue, greenValue, blueValue);
                   bArray[counter] = bInfo;
                   Ball bNew = new Ball(bArray[counter]);
                   addObject(bNew, xPos, 50);
                   counter++;
                   xPos = xPos +10;
         }
        s.close();
        }
     }
 }
public class BallInfo extends Actor
{
    private Color ballColor;  // color of the balls being summarized
    private int totalCount;   // number of balls of this color ever seen in the world
    private int caughtCount;  // number of balls of this color ever "caught" 
    
    public BallInfo(int redValue, int greenValue, int blueValue)
    {
        // initially, all counts will be zero.
        totalCount=0;
        caughtCount=0;
        
        // use specified color to create (redraw) image
        ballColor = new Color(redValue,greenValue,blueValue); 
        redraw();
    }
    
    // private method to redraw the stats image.
    private void redraw(Color col)
    {
        // build a new image for this object
        GreenfootImage img = new GreenfootImage(150,12);
        
        // fill in the background color. 
        img.setColor(new Color(200,200,200)); // could use Color.GRAY
        img.fill();
        
        // add an "icon" that shows the corresponding color
        GreenfootImage ball = new GreenfootImage(10,10);
        ball.setColor(col);
        ball.fillRect(0,0, ball.getWidth(), ball.getHeight());
        img.drawImage(ball, 1, 1);
        
        // add in the count statistics for this ball color
        img.setColor(new Color(0,0,0) ); // could use Color.BLACK
        String stats = String.format("#:%6d", totalCount);
        img.drawString(stats, 21, 11);
        stats = String.format("C:%6d", caughtCount); 
        img.drawString(stats, 85, 11);

        
        setImage(img); // use the image we've built up. 
    }
    
    // redraw the image with the current ball calor. 
    private void redraw()
    {
        redraw(ballColor);
    }
    
    /**
     * returns the color for this ball type. Might be useful to help draw a ball in the game
     *   @return the color for this type
     */
    public Color getColor()
    {
        return ballColor;        
    }
    
    /**
     * returns the number of times a ball of this color has been added to the world
     *   @return the count of the number of balls of this color ever seen in the world
     */
    public int getTotal()
    {
        return totalCount;
    }
    
    /**
     * returns the number of times a ball of this color has been caught
     *   @return the count of the number of balls of this color ever been caught
     */
    public int getCaught()
    {
        return caughtCount;
    }
    
    /** 
     * adds one to the total number of balls of this color seen in the world.
     */
    public void incTotal()
    {
        totalCount++;
        redraw();
    }
    
    /**
     * adds one to the total number of balls of this color that have been caught
     */
    public void incCaught()
    {
        caughtCount++;
        redraw();
    }    
}
public class Ball extends Actor
{
    // constants that you may find usefull while coding. 
    public static final int DIAMETER = 50;       // diameter of a Ball
    public static final int RADIUS = DIAMETER/2; // radius of a Ball
    
    private BallInfo ballData; // a link to the associated ball info for this colored ball
    /** 
     * builds a ball with the color specified by the parameter
     * 
     * @param bi the BallInfo associted with this object.
     */
    public Ball(BallInfo bi)
    {
        ballData = bi; // store (reference to) this ball's info. You may find this usefull later 

        // build an image for this ball.
        GreenfootImage img = new GreenfootImage(DIAMETER, DIAMETER);
        
        // draw an appropriately colored circle in the image
        img.setColor(bi.getColor());
        img.fillOval(0,0,DIAMETER,DIAMETER);
        
        // use the image just drawn as this ball's image
        setImage(img);
    }

    public void act() 
    {

        setLocation(getX(), getY()+2);
        Paddle p = (Paddle) getOneIntersectingObject(Paddle.class);
        if (p!=null) {
            BallInfo bInfo = new BallInfo(233, 0, 0);
            bInfo.incCaught();
            getWorld().removeObject(this);
        }
    }    
}
Thanks in advance!
You need to login to post a reply.