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

设置arraylist对象的名称-java-作业,java,Java,我有一个作业问题,我已经做了一段时间了。我创建的对象是形状,然后将它们放入arrayList,然后将这些形状的集合放入另一个数组列表。然后一次将形状全部绘制为图片。我需要命名它们,这样我就可以使用收集图片的名称来绘制文本文件。我让程序工作(它正确地绘制了形状),但是当设置图片的名称后,它返回null。我相信这是因为我的方法我没有正确地传递名称。当然,我们将非常感谢您的帮助 //read in text from file start picture A // I want to name t

我有一个作业问题,我已经做了一段时间了。我创建的对象是形状,然后将它们放入arrayList,然后将这些形状的集合放入另一个数组列表。然后一次将形状全部绘制为图片。我需要命名它们,这样我就可以使用收集图片的名称来绘制文本文件。我让程序工作(它正确地绘制了形状),但是当设置图片的名称后,它返回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");

    }

}

}
//从文件中读入文本
开始图片A//我想将图片命名为A
圈100 100 20
矩形100 100 20 30
我想画一幅画
导入java.awt.Graphics;
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.util.*;
公务舱司机{
私有静态字符串文件名;
专用静态字符串线;
私有静态图形;
私有静态字符[]名称1;
公共静态void main(字符串[]args){
ArrayList集合=新建ArrayList();
试一试{
阅读线(收集);
}捕获(例外e){
e、 printStackTrace();
}
}
静态void readLines(ArrayList集合)引发异常{
图片=新图片();
扫描仪输入=新扫描仪(System.in);
System.out.print(“请输入文件名-->”;
fileName=input.next();
FileReader fr=新的FileReader(文件名);
BufferedReader infle=新的BufferedReader(fr);
//环线
而((line=infle.readLine())!=null){
系统输出打印项次(行);
TXT分析(线条、集合、图片);
}
//关闭文件
infle.close();
}
公共静态void txtAnalysis(字符串名称、ArrayList集合、图片){
if(行startsWith(“开始图片”)){
字符串picName=line.split(“”[2];
图片=新图片();
//在这里设置名称
图片。设置名称(picName);
//它在这里工作
System.out.print(pictures.getName());
}
else if(第行开始(“圆圈”)){
String[]parts=line.split(“”);
intx=Integer.parseInt(部分[1]);
int y=Integer.parseInt(部分[2]);
intz=Integer.parseInt(部分[3]);
//新对象圆
圆圈c1=新圆圈(“圆圈”,x,y,z);
//添加对象
System.out.println(“调用add”+c1);
图片。addShape(c1);
}
else if(line.startsWith(“矩形”)){
String[]parts=line.split(“”);
intx=Integer.parseInt(部分[1]);
int y=Integer.parseInt(部分[2]);
intz=Integer.parseInt(部分[3]);
//新对象矩形
矩形r1=新矩形(“矩形”,x,y,z);//小且高
//添加对象
图片。addShape(r1);
}
else if(线条起始(“绘制”)){
收藏。添加(图片);
//这里有问题
pictures.draw(pictures.getName());
//返回空值!!!!!
System.out.print(pictures.getName());
行=空;
}
否则{
System.out.println(“循环结束”);
}
}
}
图片类

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

}
导入java.awt.Graphics;
导入java.util.*;
公开课图片{
私有数组列表形状;
私有字符串名称;
公共图片(){
形状=新的ArrayList();
}
公共字符串getName(){
//System.out.println(“获取名称”);
返回名称;
}
公共字符串集合名(字符串名){
//System.out.println(“设置名称”+名称);
this.name=名称;
返回名称;
}
公共布尔addShape(E newA){
//System.out.println(“添加形状”+newA);
布尔b=形状。添加(newA);
返回b;
}
公共无效绘图(字符串名称){
DrawingPanel=新的DrawingPanel(600600);
Graphics g=panel.getGraphics();
用于(形状:形状){
系统输出打印(“这很酷”);
形状绘制(g,0,0);
}
}
公共字符串toString(字符串名称){
String s=“Picture”+name+“承载这些形状:\n”;
对于(int i=0;i
问题是
图片=新图片()
不影响
图片的全局值
;它只影响
txtlanalysis()
图片的局部值。通过将
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");

    }

}
static void readLines(ArrayList集合)引发异常{
Picture pictures=null;//只需在此处执行null
扫描仪输入=新扫描仪(System.in);
System.out.print(“请输入文件名-->”;
fileName=input.next();
FileReader fr=新的FileReader(文件名);
BufferedReader infle=新的BufferedReader(fr);
//环线
而((line=infle.readLine())!=null){
系统输出打印项次(行);
if(行startsWith(“开始图片”)){
字符串picName=line.split(“”[2];
图片=新图片();
//在这里设置名称
图片。设置名称(picName);
//它在这里工作
System.out.print(pictures.getName());
}
否则{
TXT分析(线条、集合、图片);
}
}
//关闭文件
infle.close();
}
公共静态void txtAnalysis(字符串名称、ArrayList集合、图片){
if(带(“圆”)的行开始){
String[]parts=line.split(“”);
intx=Integer.parseInt(部分[1]);
int y=Integer.parseInt(部分[2]);
intz=Integer.parseInt(部分[3]);
//新对象圆
圆圈c1=新圆圈(“圆圈”,x,y,z);
//添加对象
System.out.println(“调用add”+c1);
图片。addSha