我有一个家庭作业问题,我已经放弃了一段时间了。我正在创建形状的对象,然后将它们放入 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");
}
}
}
图片类
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;
}
}