利用继承和多态来绘制图像?

Utilizing both inheritance and polymorphism to draw images?

我正在尝试,所以我可以在父 class 中拥有三个值(img、LocX、LocY),这些值应该能够根据它们所在的子 class 进行更改。

我想要它,所以当我尝试使用该方法绘制玩家时,它会绘制玩家。但是,我希望能够在用于绘制敌人时使用相同的方法来绘制敌人。我需要这样做,因为我需要使用继承和多态性。

我没有遇到任何语法错误,但是当我尝试 运行 我的游戏时遇到了错误(在我开始尝试这样做之前它是有效的)。这是父子 classes 所在位置的代码:

public abstract class Entity { //hold things such as player, and values realted to that specific entity (nice especially for organization purposes), both player and enemy the same health and speed, however they have different images that go along with them

    protected MazeModel model = new MazeModel();
    protected Image i = new Image();

    //both enemy and player have images, and have locations, so it was easy to get both inheritance and polymorphism in this one if they shared an abstract image, and set of coordinates

    abstract BufferedImage img(); //was looking at ways to extend a single BufferedImage to multiple classes for inheritance, I ended up finding out that they way to do it also covers polymorphism (originally did not have an idea for how to apply polymorphism)
    abstract int LocX();
    abstract int LocY();
    protected BufferedImage image; //they all have images, however they are different in each class
    //these are what is in common between player and enemy, these are currently not in use, however it is still inheritism
    protected int health = 100; //these two are not implemented yet, but are there to show inheritance
    protected int speed = 50;
}

class Player extends Entity{ //Inheritance
    BufferedImage img(){
        return i.getPlayer(); //img is the player's image in this case
    }

    int LocX(){
        return model.getPlayerX(); //gets the player's x
    }

    int LocY(){
        return model.getPlayerY(); //gets the player's y
    }

    private int playerHealth = health;
    private int playerSpeed = speed;
    //setters and getters to use the player's values
    public void setPlayerHealth(int playerHealth){
        this.playerHealth = playerHealth;
    }
    public int getPlayerHealth(){
        return playerHealth;
    }
    //  
    public Player(MazeModel model){ //constructor
        this.model = model;
    }
    private BufferedImage image = i.getPlayer();
    //for setting the player's initial starting point
    public void setPlayerStart(int x, int y){
        model.setPlayerX(x); //sets the starting x position of the player's image
        model.setPlayerY(y); //sets the starting y position of the player's image
        System.out.println("Your current coordinates are " + model.getPlayerX() + ", " + model.getPlayerY());
    }
    //for drawing the player
    public void paintImg(Entity e, Graphics g){ //polymorphism
        g.drawImage(e.img(), e.LocX(), e.LocY(), null);
    }

    public void playerPaint(Graphics g){
        g.drawImage(image, model.getPlayerX(), model.getPlayerY(), null); //creates the user
    }
}
//these next two I will possibly add later if I have the time
class Enemy extends Entity{
    BufferedImage img(){
        return i.getEnemy();
    }

    int LocX(){
        return model.getEnemyX(); //gets the player's x
    }

    int LocY(){
        return model.getEnemyY(); //gets the player's y
    }

    private int enemyHealth = health;
    private int enemySpeed = speed;
    private int enemyX = 400;
    private int enemyY = 50;
    private BufferedImage image = i.getEnemy();

    public void paintImg(Entity e, Graphics g){
        g.drawImage(e.img(), e.LocX(), e.LocY(), null);
    }

    public void paintEnemy(Graphics g){
        g.drawImage(image, model.getEnemyX(), model.getEnemyY(), null); //enemies location, fixed position for now
    }
}

这是执行所有绘图的 JPanel 的代码:

public class MazePanel extends JPanel {
    private Maze maze = new Maze(); //so I can paint the maze
    private MazeModel model;
    private Player p;
    private Enemy e = new Enemy();
    private Entity ent;

    public MazePanel(MazeModel model){ //constructor
        p = new Player(model);
        p.setPlayerStart(50, 50); //sets the player's starting coordinates
        setPreferredSize(new Dimension(500, 500)); //map size
        setBackground(Color.DARK_GRAY); //ground color
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        maze.paintMaze(g); //creates the walls
        e.paintImg(ent, g);
        p.paintImg(ent, g);
    }
}

我相信我快要搞定了,但出于某种原因,我无法弄清楚到底出了什么问题。我再次希望能够以这种方式进行,因为我需要以某种方式同时使用继承和多态性。

Player 中的这个看起来有问题:

//for drawing the player
public void paintImg(Entity e, Graphics g){ //polymorphism
    g.drawImage(e.img(), e.LocX(), e.LocY(), null);
}

public void playerPaint(Graphics g){
    g.drawImage(image, model.getPlayerX(), model.getPlayerY(), null); //creates the user
}

第一种方法,paintImg 不需要对象的任何状态,也不会改变状态,可以是静态的。我不确定为什么您将其评论为多态,因为它绝对不是多态。

事实上,我会合并上面的两个方法并将它们放在实体 class 中。要获得多态性,你必须继承方法,你的Entityclass有none。我会创建一个方法 paintImg 或更好的方法 `paintEntity,并让它使用 Graphics 参数和它自己的状态来绘制自己,然后让你的其他对象继承这个方法并在需要时修改它。