0

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;
}

}

1 Answer 1

1

The problem is that pictures = new Picture<Shape>(); doesn't affect the global value of pictures; it only affects the local value of pictures in txtAnalysis(). A simple code shift should get you the result you're looking for, by setting the value of pictures in a place where it will actually stick:

static void readLines(ArrayList<Picture> collection) throws Exception {
    Picture<Shape> pictures = null; //Just do null here
    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);
        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 {
            txtAnalysis(line, collection,pictures);
        }
    }

    // close file
    inFile.close();

}

public static void txtAnalysis(String name, ArrayList<Picture> collection, Picture<Shape> pictures ) {


    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");

    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I knew I was close. What would I have to do to call the Picture (collection of shapes) and draw them.` for(Picture item : collection) { if (item.getName.equals(label)) { System.out.println("calling add " + label); pictures.draw(label); } }`
You could put that for loop right below readLines(collection);. In terms of actually drawing them to the GUI, I haven't done that in a little while in Java, so start with Google then post a new question if necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.