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();
}
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 ispaintThen lets move onto, you should rarely, if ever, extend a top level container like
JFrameand especially override thepaintmethod.paintdoes a lot of really important work and you should always callsuper.paintInstead, you should extend from something
JPaneland override thepaintComponentmethod instead (remembering to callsuper.paintComponwnt)And then I’d include Rohit’s suggestions.
You might like to have a read through