Melky
Melky

Reputation: 167

Shapes, Drawable"Shapename", Shape Inheritance and Interface

This is my last exercise in my headfirst book but when i run the application the shape is not being drawn, I'm pretty stumped cause there are no errors.

public class ShapesDriver extends Frame{ //You said Frame in the Tutorial4 Document
    private ArrayList<Drawable> drawable;
    public static void main(String args[]){
        ShapesDriver gui = new ShapesDriver();

        gui.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing (WindowEvent e){
                System.exit(0);
            }
        });    
    }

    public ShapesDriver(){
        super("Shapes");
        setSize(500, 500);
        setResizable(false);
        setVisible(true);
        show();
    }

    public void Paint (Graphics g){
        DrawableRectangle rect1 = new DrawableRectangle(150, 100);
        drawable.add(rect1);
        for(int i = 0; i < drawable.size(); i++){
            drawable.get(i).draw(g);
        }        
    }
}

DrawableRectangle Class

public class DrawableRectangle extends Rectangle implements Drawable{
    private int x, y;
    public DrawableRectangle(int height, int width){
        super(height, width);
    }

    @Override
    public void setColour(Color c) {
        this.setColour(c);
    }

    @Override
    public void setPosition(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public void draw(Graphics g) {
        g.drawRect(x, y, width, height);
    }
}

Rectangle Class

public abstract class Rectangle implements Shape {
    public int height, width;

    Rectangle(int Height, int Width){
        this.height = Height;
        this.width = Width;
    }

    @Override
    public double area() { return width * height; }
}

Shape Class

public interface Shape {
    public double area();
}

Upvotes: 0

Views: 1063

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

Lets start with, Java is case sensitive, so public void Paint (Graphics g){ is never going to be called by Java, as the method name is paint

Then lets move onto, you should rarely, if ever, extend a top level container like JFrame and especially override the paint method. paint does a lot of really important work and you should always call super.paint

Instead, you should extend from something JPanel and override the paintComponent method instead (remembering to call super.paintComponwnt)

And then I'd include Rohit's suggestions.

You might like to have a read through

Upvotes: 1

Related Questions