抽象类数组

时间:2014-09-30 13:04:23

标签: java arraylist abstract-class

我有四节课。它们是:模型,视图,龟和雪碧。 Sprite是一个抽象类,Turtle是从它扩展而来的。我已经解决了这个问题,但是我无法理解这一点而感到困惑和沮丧。我迫切需要一些帮助来理解为什么/为什么要做所需的事情。

我如何

  

将Model类中的ArrayList修改为ArrayList   而不是ArrayList正确,并将“setDestination”更改为   使用java.util.Random对象生成随机x坐标   介于0和宽度参数的值之间,以及随机的y   0和高度参数之间的坐标。然后它应该创建一个   点(x,y)处的新Turtle并将其添加到Sprites的ArrayList中。   最初不再创建任何海龟,但只创建它们   当用户点击鼠标时(即调用“setDestination”时)。

以下是我的文件:(我知道这很多,但我不确定有多少是完全必要的。)

Model.java:

import java.awt.Graphics;
import java.io.IOException;
import java.util.*;
class Model
{
    private List<Sprite> t = new ArrayList<>();
    private int turtleNumber = 3;

    Model() throws IOException
      {
           for (int i = 0; i <turtleNumber; i++)
            {
              int X = Math.round((float)Math.random()* 1000);
              int Y = Math.round((float)Math.random() *700);
              Sprite t1 = new Turtle(X,Y);
              t.Add(t1);
            }
        }

    public void update(Graphics g)
          {
              for(int i = 0; i <turtleNumber; i++)
              t.get(i).update(g);
          }

    public void setDestination(int x, int y, int width, int height) 
    {
        for(Turtle turt : t)
        {
            Turtle.setDest(x, y);
        }
    }
}

Turtle.java:

import java.awt.Graphics;

class Turtle extends Sprite
{

    private static int dest_x;
    private static int dest_y;

    Turtle(int x, int y) {
        super( x, y , "turtle.png");
    } 

    public int getX() { return x; }
    public int getY() { return y; }

    public void setX(int xIn) { x = xIn; }
    public void setY(int yIn) { y = yIn; }

    public void update(Graphics g) {
        // Move the turtle
        if (x < dest_x) {
            x += 1;
        } else if (x > dest_x) {
            x -= 1;
        }

        if (y < dest_y) {
            y += 1;
        } else if (y > dest_y) {
            y -= 1;
        }
    }

    public static void setDest(int x, int y) {
        dest_x = x;
        dest_y = y;
    }
}

Sprite.java:

import java.awt.Graphics;
import javax.imageio.ImageIO;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

public abstract class Sprite
{

int x;
int y;
String imageName;

Image image;

//Sprite(){}

public Sprite(int x1, int y1, String im)
{
//Store variables
    imageName = im;
     x = x1;
     y = y1;

    try {
        image = ImageIO.read(new File(imageName));
    } catch (IOException ioe) {
        System.out.println("Unable to load image file.");
    }

    // Draw the turtle
      g.drawImage(imageName, x, y, 100, 100, null);
}

public abstract void update(Graphics g);

public Image getImage()
{
    return image;
}
}

Controller.java:

import java.awt.Graphics;
import java.io.IOException;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.Timer;

class Controller implements MouseListener
{
    Model model;
    View view;

    Controller() throws IOException, Exception {
        model = new Model();
        view = new View(this);
        new Timer(50, view).start();
    }

    public void update(Graphics g) {
        model.update(g);
    }

    public void mousePressed(MouseEvent e) {
        model.setDestination(e.getX(), e.getY(), view.getWidth(), view.getHeight());
    }

    public void mouseReleased(MouseEvent e) {    }
    public void mouseEntered(MouseEvent e) {    }
    public void mouseExited(MouseEvent e) {    }
    public void mouseClicked(MouseEvent e) {    }

    public static void main(String[] args) throws Exception {
        //  Use the following line to determine which directory your program
        //  is being executed from, since that is where the image files will
        //  need to be.
        //System.out.println("cwd=" + System.getProperty("user.dir"));
        new Controller();
    }
}

0 个答案:

没有答案