1

我有这个作业,我一直在做。继承是最容易的部分。然后她扔了一堆其他的东西使它变得困难。它不会一直影响我的成绩,但我花了很多时间,很想知道为什么它没有画出正确的对象。我正在驱动程序中制作图片(形状)的集合(主图片),而不是通过文件中的标签命名它们。这一切都有效,除非我尝试使用 getName() AND 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;
}

}
4

1 回答 1

0

我没有你的 Shape 类来测试这个,但我觉得问题可能在于你的 Driver 类的 readLines 方法。请注意,您已将图片变量的实例化放置在 while 循环中,因此每次您有一行以“开始图片”开头的行时都会重写它。

于 2013-10-16T00:20:08.387 回答