I have a homework problem that I have been working on for quit a while now. I am creating objects which are shapes then putting them in an arrayList then putting a collection of those shapes in another array list. Then drawing the shape as a picture all at one time. I need to name them so I can use the name of the collection Picture to draw the text file. I got the program working (it draws the shapes correctly) but when set the name of the picture then get it, it returns null. I believe that it is because my method I am not properly passing the name. Of Course help would be Greatly appreciated.
//read in text from file
start picture A // I want to name the picture A
circle 100 100 20
rectangle 100 100 20 30
draw A // I want to draw picture A
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
public class Driver {
private static String fileName;
private static String line;
private static Graphics g;
private static char[] name1;
public static void main(String[] args) {
ArrayList<Picture> collection = new ArrayList<Picture>();
try {
readLines(collection);
} catch (Exception e) {
e.printStackTrace();
}
}
static void readLines(ArrayList<Picture> collection) throws Exception {
Picture<Shape> pictures = new Picture<Shape>();
Scanner input = new Scanner(System.in);
System.out.print("Please enter the file name ---> ");
fileName = input.next();
FileReader fr = new FileReader(fileName);
BufferedReader inFile = new BufferedReader(fr);
// loop through lines
while ((line = inFile.readLine()) != null) {
System.out.println(line);
txtAnalysis(line, collection,pictures);
}
// close file
inFile.close();
}
public static void txtAnalysis(String name, ArrayList<Picture> collection, Picture<Shape> pictures ) {
if (line.startsWith("start picture")) {
String picName = line.split(" ")[2];
pictures = new Picture<Shape>();
//set the name here
pictures.setName(picName);
//here it works
System.out.print(pictures.getName());
}
else if (line.startsWith("circle")) {
String[] parts = line.split(" ");
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
int z = Integer.parseInt(parts[3]);
//new object circle
Circle c1 = new Circle("circ", x, y, z);
//add object
System.out.println("calling add " + c1);
pictures.addShape(c1);
}
else if (line.startsWith("rectangle")) {
String[] parts = line.split(" ");
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
int z = Integer.parseInt(parts[3]);
//new object rectangle
Rectangle r1 = new Rectangle("rect", x, y, z); // small and upper
//add object
pictures.addShape(r1);
}
else if (line.startsWith("draw")) {
collection.add(pictures);
//problem here
pictures.draw(pictures.getName());
//returns null!!!!!
System.out.print(pictures.getName());
line = null;
}
else {
System.out.println("end of loop");
}
}
}
Picture class
import java.awt.Graphics;
import java.util.*;
public class Picture <E extends Shape> {
private ArrayList<Shape> shapes;
private String name;
public Picture() {
shapes = new ArrayList<Shape>();
}
public String getName() {
//System.out.println("getting the name");
return name;
}
public String setName(String name) {
// System.out.println("setting the name " + name);
this.name = name;
return name;
}
public boolean addShape(E newA) {
// System.out.println("add shape" + newA);
boolean b = shapes.add(newA);
return b;
}
public void draw(String name) {
DrawingPanel panel = new DrawingPanel(600, 600);
Graphics g = panel.getGraphics();
for (Shape shape : shapes) {
System.out.print("this is cool");
shape.draw(g, 0, 0);
}
}
public String toString(String name) {
String s = "Picture " + name + " hosts these shapes:\n";
for (int i = 0; i < shapes.size(); i++) {
s += " " + shapes.get(i).toString() + "\n";
}
return s;
}
}