gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:35:05 25 4
gpt4 key购买 nike

我正在尝试在父类中拥有三个值(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com