0

I am confused on how to use an array list and objects. I have made objects and can add them to my generic class's array list. But the teacher wants us to read a text file such as this

 start picture A
 circle 0 100 20
 rectangle 100 100 20 30
 end picture
 draw picture A blue 10 10

I have no idea how to deal or reference with objects once I load them into the array list. Basically I do not understand the generics class Picture. Here's a link to the assignment. Any clarity on what I need to do in my Picture class to call and manipulate the objects would be greatly appreciated.

http://www.cs.csustan.edu/~mthomas/cs3100/cs3100_hwk1.html

import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

public class TestCircle {

private static String fileName;
private static String line;

public static void main(String[] args) {

    try {
        readLines();
    } catch (Exception e) {

        e.printStackTrace();
    }

}

static void readLines() throws Exception {

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

    }

    // close file
    inFile.close();

}

public static void txtAnalysis(String line) {
    DrawingPanel panel = new DrawingPanel(600, 600);
    Graphics g = panel.getGraphics();
    Picture<Shape> pictures = new Picture<Shape>("All Pictures");

    if (line.startsWith("start picture")) {
        String name = line.split(" ")[2];
        pictures.setName(name);

        System.out.println("start");
    }

    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]);

        Circle c1 = new Circle("circ", x, y, z); // small and upper left
        // c1.draw(g, 0, 0);
        // c1.dance(g, 0, 0, 600, 600);

        pictures.add(c1);

        System.out.println("circle");

    }
    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]);
        System.out.println("rectangle");
        Rectangle r1 = new Rectangle("circ", x, y, z);
        // r1.draw(g, 0, 0);
        // r1.dance(g, 0, 0, 600, 600);

        pictures.add(r1);
        System.out.println("rectangle");

    }

    if (line.startsWith("end picture")) {

        line = null;

        }

        else {
            System.out.println("end of loop");

        }

    }

}

and this is what I have so far in Picture class.

import java.util.*;



 class Picture <E extends Shape>  {
 private ArrayList<Shape> pictures;
 private String name;



public Picture(String n) {

    name = n;
    pictures = new ArrayList<Shape>();
}

   public String getName() {
       return name;
   }


   public void setName(String name) {
      this.name = name;
  }



 public boolean add(E newA) {
    boolean b = pictures.add(newA);
    return b;

}

}
4

1 回答 1

2

You are actually off to a good start. A Picture is a collection of Shapes, and each Shape knows how to draw itself, dance, and whatever ridiculous stuff your professor has contrived.

You want every Shape to handle its business. So Rectangle knows it is a box with sides whose length it knows at 90-degree angles. Circle knows to draw around from the center for length radius, which it knows. And so on.

Then you get a collection of them together inside Picture.

You probably want

class Picture<Shape>

And then something inside Picture like

private ArrayList<Shape> shapes;

So when someone calls picture.draw() in Homework1, you will do something like this:

public void draw() {
  for (Shape shape : shapes) {
    shape.draw();          
  }
}

So basically, when you tell Picture to draw, Picture goes down the line and delegates to each Shape to draw itself.

Hope that helps.

于 2013-10-12T15:36:20.317 回答