我有这个我一直在做的功课。它是继承,这是容易的部分。然后她扔了一堆其他东西让它变硬。这不会影响我的成绩,因为它不能一直工作,但我花了很多时间,并且很想知道为什么它没有画出正确的对象。我在驱动程序中制作图片(形状)的集合(主图片),而不是通过文件中的标签命名它们。这一切都有效,除非我尝试使用getName()和draw()获取名称时,它始终打印放入数组的最后一个集合。我要把它绳死了。如果有人能为我解决这个问题,我肯定会给你买啤酒。
示例文本文件输入
start picture A
rectangle 10 50 120 30
end picture
start picture A
rectangle 10 50 120 30
end picture
draw picture A red 10 100
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
private static String fileName;
private static String line;
public static void main(String[] args) {
// creates a list of a collection of pictures (shapes)
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 = 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>(picName);
pictures.setName(picName);
} else if (line.startsWith("draw picture")) {
String label = line.split(" ")[2];
//This is my PROBLEM !!! grrr
for (Picture item : collection) {
if (item.getName().equals(label)) {
pictures.draw(item);
}
}
}
else if (line.startsWith("dance picture")) {
String label = line.split(" ")[2];
for (Picture item : collection) {
if (item.getName().equals(label)) {
pictures.dance(item);
}
}
}
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
pictures.addShape(c1);
} else if (line.startsWith("Sshape")) {
String[] parts = line.split(" ");
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
int z = Integer.parseInt(parts[3]);
// new object MyShape
MyShape s1 = new MyShape("myshape", x, y, z);
// adds custom shape
pictures.addShape(s1);
} else if (line.startsWith("ColoredCircle")) {
String[] parts = line.split(" ");
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
int z = Integer.parseInt(parts[3]);
String color = (parts[4]);
// new object MyShape
ColoredCircle cc1 = new ColoredCircle("myshape", x, y, z, color);
// add a MyShape to the picture
pictures.addShape(cc1);
}
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 a rectangle to the pictures
pictures.addShape(r1);
}
else if (line.startsWith("end")) {
// adds pictures to my collection
collection.add(pictures);
}
else {
}
}
}
图片类
import java.awt.Graphics;
import java.util.*;
public class Picture <E extends Shape> {
private ArrayList<Shape> shapes;
private String name;
public Picture(String name) {
shapes = new ArrayList<Shape>();
this.name = name;
}
public String getName() {
System.out.println("getting the name test " + name);
return name;
}
public String setName(String name) {
this.name = name;
return name;
}
public boolean addShape(E newA) {
boolean b = shapes.add(newA);
return b;
}
public void draw(Picture<E> E) {
DrawingPanel panel = new DrawingPanel(600, 600);
Graphics g = panel.getGraphics();
for (Shape shape : shapes) {
shape.draw(g, 100, 100);
}
}
public void dance(Picture<E> E) {
DrawingPanel panel = new DrawingPanel(600, 600);
Graphics g = panel.getGraphics();
for (Shape shape : shapes) {
shape.dance(g, 100, 100, 10, 10);
}
}
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;
}
}
答案 0 :(得分:0)
我没有你的Shape类来测试它,但我觉得问题可能在于你的Driver类的readLines方法。请注意,您已将图片变量的实例化放置在while循环中,因此每次有一行以“start picture”开头时都会重写它。