private String strFile;
private int xPos;
private int yPos;
private int counter;
private RideName myList[];
private String myListName[];
private FileDialog fd;
/**
* Act - do whatever the Load wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void performAction()
{
openFile();
}
public Load()
{
super("Load");
myList = new RideName[14];
}
public void openFile()
{
World MyWorld = getWorld();
fd = null;
fd = new FileDialog(fd, "Pick a file with data", FileDialog.LOAD);
fd.setVisible(true);
String fullName = fd.getDirectory() + fd.getFile();
File dataFile = new File(fullName);
Scanner Reader = null;
try
{
Reader = new Scanner(dataFile);
World w = getWorld();
MyWorld wor = (MyWorld) getWorld();
wor.removeAllButtons();
xPos = MyWorld.getHeight()-75;
yPos = 55;
counter = 0;
strFile = null;
while(Reader.hasNext())
{
strFile = Reader.nextLine();
if(counter < myList.length)
{
w.addObject(myList[counter] = new RideName(strFile), xPos+120, yPos);
}
yPos+=40;
counter++;
}
}
catch(FileNotFoundException fnfe)
{
}
}
public void mergeFile()
{
World MyWorld = getWorld();
fd = null;
fd = new FileDialog(fd, "Pick a file with data", FileDialog.LOAD);
fd.setVisible(true);
String fullName = fd.getDirectory() + fd.getFile();
File dataFile = new File(fullName);
Scanner Reader = null;
try
{
Reader = new Scanner(dataFile);
World w = getWorld();
MyWorld wor = (MyWorld) getWorld();
while(Reader.hasNext())
{
strFile = Reader.nextLine();
if(counter < myList.length)
{
w.addObject(myList[counter] = new RideName(strFile), xPos+120, yPos);
}
yPos+=40;
counter++;
}
}
catch(FileNotFoundException fnfe)
{
}
}public RideName(String txt)
{
rideName = txt;
setImage(new GreenfootImage(rideName, 13, Color.BLUE, Color.WHITE));
}
protected void addedToWorld(World w)
{
a = new Add(this);
getWorld().addObject(a, this.getX()-60, this.getY());
while(this.intersects(a))
{
setLocation(getX()+4, getY());
}
}
