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

2015/5/26

Mouse Repel Problem

Tommy99 Tommy99

2015/5/26

#
Hello. I am almost finished my project, but I have one error. I am trying to have the mouse pointer repel all points within a range of 100 pixels. When I tried to write the code for that in the world class, I got the error "cannot find symbol: List<Point>objects = getObjectsInRange(range, Point.class);". Does anyone know how to fix this? Thank you. Here is the code for the World:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.lang.Object;
import java.util.List;
import java.util.*;
// Name: Tommy Guo (GrandMasterG)

public class Field  extends World
{
    // The Margin around the outer edges of the world:
    private static final int MARGIN = 100;
    private static final double REPULSION = 1.1;

    public Field()
    {    
        super(800, 600, 1); 
        addPoints(100);
    }

    public void addPoints(int n)
    {
        for(int i=0;i<n;i++) {
            int x = Greenfoot.getRandomNumber(getWidth()-MARGIN) + MARGIN/2;
            int y = Greenfoot.getRandomNumber(getHeight()-MARGIN) + MARGIN/2;
            addObject(new Point(),x,y);
        }
    }

    public void act()
    {
        if(Greenfoot.mouseClicked(this)) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(mouse!=null) {
                addObject(new Magnet(100),mouse.getX(),mouse.getY());
            }
        }
        mouseRepel();
    }

    public void mouseRepel()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        int x = mouse.getX();
        int y = mouse.getY();
        int range = 100;
        List<Point>objects = getObjectsInRange(range, Point.class);
        for(Point p: objects){ 
            double dx = p.getExactX()-this.getExactX();
            double dy = p.getExactY()-this.getExactY();
            int angle = (int)Math.toDegrees(Math.atan2(dy,dx));
            p.addForce(new Vector(angle, REPULSION));

            
        }
    }

    //Cast mouse, make list, if touching, make for loop, doubles, check to see if in range, double dx, dy, etc.
}
Super_Hippo Super_Hippo

2015/5/26

#
Try to change line 45 to this:
List<Point> objects = (Point) getObjectsInRange(range, Point.class);
danpost danpost

2015/5/26

#
Super_Hippo wrote...
Try to change line 45 to this:
List<Point> objects = (Point) getObjectsInRange(range, Point.class);
A List object cannot be cast as type Point. Try either this:
List<Point> objects = (List<Point>) getObjectsInRange(range, Point.class);
or replace lines 45 and 46 with either one of the following:
for (Object obj : getObjectsInRange(range, Point.class))
{
    Point p = (Point) obj;
or
for (Point p : (List<Point>) getObjectsInRange(range, Point.class))
Tommy99 Tommy99

2015/5/26

#
Oh okay. Thank you very much.
bzhang bzhang

2015/5/27

#
Maybe you should use the mouse method
You need to login to post a reply.