开发者

Java: Implementing a drawable class

I'm trying to make a maze game in Java.

The Explorer class represents the user, and the DrawableExplorer is the code that graphically represents the user. DrawableExplorer implements the Drawable interface which contains:

    import java.awt.Graphics;

    public abstract interface Drawable
    {
      public abstract void draw(Graphics paramGraphics);
    }

this compiles successfully however, I cannot figure out why my DrawableExplorer class isn't:

    import java.awt.*;
    public class DrawableExplorer extends Explorer implements Drawable

{

    public DrawableExpolorer(Square location, Maze maze, String name)
        {
            public void draw(Graphics g)
                {
                    Square location = location();
                    get.setColor(Color.BLUE);
            开发者_开发技巧        g.fillOval(loc.x() + 10, loc.y() + 10, 30, 30);

                }
        }
}

It's asking for a return type but isn't my method void?

The compiler error message says "invalid method declaration; return type required"


You need to declare the class as:

public class DrawableExplorer extends Explorer implements Drawable

i.e. The extends clause has to come before the implements clause.

The other error is that you've declared your draw method within the body of the constructor for DrawableExplorer. Given that you've defined a constructor that takes three arguments you would typically want to process these within the constructor body (you currently ignore them); e.g. by assigning them to instance variables.


I m pretty sure that you can t declare method inside constructor. Compiler doesn t even know what there should be so it gives you first obvious error fix that it can give you. Compiler doesn t see big picture what you want to do. It just gives solutions for smallest problem that it encounters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜