How can i use an array so that when i choose a certain text file to read from to create a object those object created are placed from the left to right depending on the order they are read from the text file?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Scanner;
import java.awt.FileDialog;
import java.io.*;
//import java.awt.Color;
import java.util.ArrayList;
/**
* Write a description of class Load here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Load extends Button
{
// private Scanner reader;
private Color col;
public Load(){
super("Load");
}
/**
* The handle method will do whatever the Button is intended to do
* *** remember to write your own version of this method in a
* *** subclass !!!
*/
public void handle(){
FileDialog fd = null;
fd = new FileDialog(fd, "Choose a file", FileDialog.LOAD); // titles the GUI choose a file
fd.setVisible(true);
String fname = fd.getDirectory() + fd.getFile();
File myFile = new File(fname); // finds the files name and path and stores it as myFile
Scanner reader=null; //scanner is empty
try
{
reader = new Scanner(myFile);
}
catch (FileNotFoundException e)
{
System.out.println("I am sorry but " + fname + " is not a valid file!");
return;
}
//ArrayList<String> list = new ArrayList<String>();
while(reader.hasNext()){
//list.add(reader.next());
String a = reader.next(); // when text file is read sets the first word to string a
if (a.equals("red")){ // is string a is exactly equal to the word
int width = reader.nextInt(); // sets the second piece of info to int xLoc
int height = reader.nextInt(); // sets the third piece of info on the same line
MyWorld w;
w = (MyWorld)getWorld();
col = Color.RED;
Present gifts = new Present(width, height, col);
w.addObject(gifts, width, height);
}
if (a.equals("blue")){ // is string a is exactly equal to the word
int width = reader.nextInt(); // sets the second piece of info to int xLoc
int height = reader.nextInt(); // sets the third piece of info on the same line
MyWorld w;
w = (MyWorld)getWorld();
col = Color.BLUE;
Present gifts = new Present(width, height, col);
w.addObject(gifts, width, height);
}
if (a.equals("green")){ // is string a is exactly equal to the word
int width = reader.nextInt(); // sets the second piece of info to int xLoc
int height = reader.nextInt(); // sets the third piece of info on the same line
World w;
w = this.getWorld();
col = Color.GREEN;
Present gifts = new Present(width, height, col);
w.addObject(gifts, width, height);
}
}
reader.close(); // closes the scanner
}
}
//line 48 int x = 20, y = 200; //location of the first present //when adding w.addObject(gifts, x, y); //after line 86 x+=50; //next present 50 cells right from this one
//line 51 int width=0, height=0; //remove 'int' from lines 54, 55, 66, 67, 78 and 79 //after line 86 x+=width;
if (x+width > getWidth())
//or if you don't want half of the present to be outside of the world
if (x + width/2 > getWidth())
{
x = width/2; //maybe you also want to add the first present at half width, too
y+=100; //need to use a fixed height here because not all presents have same height and then the next line might be in the middle of the other one if the last present was small
}public void sortByColor(Color[] colorOrder)
{
int x=0, y=200;
for (int col=0; col<colorOrder.length; col++)
{
// -- start at the begin of the text file
Color currentColor = colorOrder[col];
while(reader.hasNext())
{
int width=0, height=0;
String a = reader.next();
String color = null;
if (currentColor.equals(Color.RED)) color = "red";
else if (currentColor.equals(Color.BLUE)) color = "blue";
else if (currentColor.equals(Color.GREEN)) color = "green";
else if (currentColor != null)
{
System.out.println("Color has to be red, blue or green");
return;
}
else return;
if (color.equals(a))
{
width = reader.nextInt();
height = reader.nextInt();
x += width/2;
if (x + width/2 > getWorld().getWidth) {y+=100; x = width/2;}
getWorld().addObject(new Present(width, height, currentColor), x, y);
x += width/2;
}
}
}
}