Search code examples
androidruntimedrawdimensions

android draw rectangle with dimensions and modify them in runtime


I'm developing an engineering app for android. The thing is that I need to draw: rectangles, figures made of rectangles, and their dimensions. Then if you touch one extreme of one dimension you are able to make that dimension of the rectangle longer or shorter.

I am implementing the next scheme in order to achieve my goal:

class DrawFigureWithDimensions extends View{
    // implementation of the draw methods
    // ...
    class Rectangles{
        // characterization of the attributes needed for each "rectangle"
        // ...
        class DimensionPositionType{ ... }
        class DimensionExtremeType{ ... }
}
class DrawRectangleWithDimension extends DrawFiguresWithDimensions{ ... }
class DrawBoxWithDimension extends DrawFiguresWithDimensions{ ... }
...

Then, I have a problems implementing the inner classes of DimensionPositionType and DimensionExtremeType, I can't find out how to declare them in a suitable way. I need to be able to decide in the extended classes, like DrawRectangleWithDimensions, for example, what type of extreme of the dimension I need: fixed or movable. Something like this:

public class DrawRectangleWithDimensions extends DrawFiguresWithDimensions {

public DrawRectangleWithDimensions(Context context) {
    super(context);
}

public void setFigure(double width, double height) {
    figureRectangles = new Rectangle[1];
    figureRectangles[0] = new Rectangle(0, 0, width, height);
    figureRectangles[0].setHorizontalDimensionLeftExtremeType(FIXED);
    figureRectangles[0].setHorizontalDimensionRightExtremeType(MOVABLE);
}

For instance, this is the code that I have for the inner class DimensionExtremeType:

    class DimensionExtremeType{
        boolean FIXED;
        boolean MOVABLE;
        DimensionExtremeType(String arg){
            if(arg == "FIXED"){
                setFixedExtreme();
            }else if(arg == "MOVABLE"){
                setMovableExtreme();
            }
        }
        public void setFixedExtreme(){
            FIXED = true;
            MOVABLE = false;
        }
        public void setMovableExtreme(){
            FIXED = false;
            MOVABLE = true;
        }
        public String getDimensionExtremeType(){
            if(FIXED==true){
                return "FIXED";
            }else if(MOVABLE==true){
                return "MOVABLE";
            }else {
                return null;
            }
        }
    }

Solution

  • I just know about the existence of the class enum, which solves the problem of design that I had. It's a much easier way of carrying it out. According to the example that I wrote in the wording of the question, this is the code using the class enum:

    static class Rectangle{
        // ...
        DimensionExtremeType horizontalDimensionLeftExtremeType;
        DimensionExtremeType horizontalDimensionRightExtremeType;
        DimensionExtremeType verticalDimensionUpperExtremeType;
        DimensionExtremeType verticalDimensionLowerExtremeType;    
        // =-=-=-=     DIMENSION POSITION TYPES      =-=-=-=
        public enum DimensionExtremeType{FIXED, MOVABLE}
    

    }

    Then, in the extended classes, for instance DrawRectanglesWithDimensions:

    public class DrawRectangleWithDimensions extends DrawFiguresWithDimensions {
    
        public DrawRectangleWithDimensions(Context context) {
            super(context);
        }
    
        public void setFigure(float  width, float height) {
            figureRectangles = new Rectangle[1];
            figureRectangles[0] = new Rectangle(0, 0, width, height);
            figureRectangles[0].horizontalDimensionLeftExtremeType = Rectangle.DimensionExtremeType.FIXED;
            figureRectangles[0].horizontalDimensionRightExtremeType = Rectangle.DimensionExtremeType.MOVABLE;
            figureRectangles[0].verticalDimensionUpperExtremeType = Rectangle.DimensionExtremeType.MOVABLE;
            figureRectangles[0].horizontalDimensionRightExtremeType = Rectangle.DimensionExtremeType.MOVABLE;
        }
    }