设置arraylist对象java的名称 - 家庭作业

时间:2013-10-15 01:38:22

标签: java

我有一个家庭作业问题,我一直在努力退出一段时间。我创建的对象是形状,然后将它们放在arrayList中,然后将这些形状的集合放在另一个数组列表中。然后一次性将所有形状绘制成图片。我需要命名它们,以便我可以使用集合Picture的名称来绘制文本文件。我让程序工作(它正确地绘制形状)但是当设置图片的名称然后得到它,它返回null。我相信这是因为我的方法我没有正确地传递这个名字。当然,非常感谢帮助。

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

    }

}

}

图片类

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 个答案:

答案 0 :(得分:1)

问题是pictures = new Picture<Shape>();不会影响pictures的全球价值;它仅影响picturestxtAnalysis()的本地值。一个简单的代码转换可以通过在pictures实际粘贴的位置设置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"); } } 的值来获得您正在寻找的结果:

{{1}}