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

标签 java image inheritance polymorphism paint

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

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

我没有收到任何语法错误,但当我尝试运行我的游戏时却收到错误(在我开始尝试这样做之前它就可以工作)。以下是父类和子类所在位置的代码:

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不使用要求对象的任何状态,也不改变状态,并且可以是静态的。我不知道为什么你将它评论为多态,因为它绝对不是多态。

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

关于java - 利用继承和多态来绘制图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30088003/

相关文章:

image - 到对象网络摄像头 C920HD 的距离或使用 OpenCV calibrate.py

java - 如何使用 Java 调整图像大小?

swift - 如何使用 Swift 协议(protocol)扩展来创建可以修改私有(private)属性的共享代码?

ruby - 处理复合记录的 Rails 模型

java - XMPP - Facebook - 我的客户端实现如何接收其他客户端实现发送的消息?

Java 11 包 javax.xml.bind 不存在

html - 建议将图像和文本编码到已部署的 HTML 页面...

Java 继承 - 尝试创建通用方法从 Guava EventBus 库获取总线

java - 用Java文件方法删除Jar快捷方式

java - 将从 Word 粘贴到 Firefox 富文本编辑器的文本转换为 xsl-fo,同时保留格式 (Java) (Apache FOP)