Figures

 

The applet "figures" illustrates the possibilities of object-oriented programming. Here geometric objects (balls and rectangles) are generated, which can be enabled or disabled by clicking with the mouse. Activated figures can be moved with the arrow keys. Circles and rectangles are both geometric figures. So it is natural to define circles and rectangles as subclasses of a class Figure. One can of this upper class Figure, however - unlike ball, billiard ball in the last example - do not create objects, because a figure can be either a circle or a rectangle (it is said that a variable of the class Figure is polymorphic, multiform) but not both at the same time. What I would therefore produce if I could create a Figure-object? To ensure that no object can be created from the class Figure, I define the class as abstract (keyword abstract). A figure should be able to be drawn. But since it requires quite different commands to draw a circle or a rectangle, the method paint in the class figure remains empty (the method will also be declared as abstract).

public abstract class Figure {
    private Color hasColor;
    private int zX, zY;
    private int zWidth;
    private boolean zActive;
    private boolean zVisible;
    
    public Figure(int pX, int pY,int pWidth, Color pColor){
        zX=pX;zY=pY;zWidth=pWidth;
        zActive=false;
        zVisible=true;
        hasColor=pColor;
    }
    
    public void setX(int pX){
        zX=pX;
    }
    
    public void setY(int pY){
        zY=pY;
    }
    
    public void setWidth(int pWidth){
        zWidth=pWidth;
    }
    
    public void setActive(){
        zActive=true;
    }
    
    public void setInactive(){
        zActive=false;
    }
    
    public void setVisible(){
        zVisible=true;
    }
    
    public void setInvisible(){
        zVisible=false;
    }
    
    public void setColor(Color pColor){
        hasColor=pColor;
    }
    
    public int getX(){
        return zX;
    }
    
    public int getY(){
        return zY;
    }
    
    public int getWidth(){
        return zWidth;
    }
    
    public boolean getActive(){
        return zActive;
    }
    
    public boolean getVisible(){
        return zVisible;
    }
    
    public Color getColor(){
        return hasColor;
    }
    
    public void move(Graphics pG, int pX, int pY){
        clear(pG);
        zX+=pX;
        zY+=pY;
        pause(20);
        paint(pG);
    }
    protected void pause(int ms){
        try {
            Thread.sleep(ms);
        }
        catch(InterruptedException e){}
    }
    
    public abstract void paint(Graphics pG);
    public abstract void clear(Graphics pG);
    public abstract boolean hit(int pX, int pY);
}