简体   繁体   中英

Change color of a rectangle inside a class, like a status

So far i tried so many different things to make this work. I can't seem to understand why this shouldn't work. I have a class called StatusRect.java . This class returns a rectangle when a new object is being made with the method makeRectangleStatus . The idea is to color this rectangle every time an integer becomes a certain value. In the class StatusRect.java the method changeIntFlag is invoked from another class. Here the integer is being changed. That works.

Now I just want the color of the rectangle to change in this StatusRect.java class. The main question is actually can this color be set inside this StatusRect.java class, or can it only be done outside this class? The rectangle object is being made in the Stage of the application like below. There the color red is given as a parameter. Any help here is greatly appreciated.

public void start(Stage stage) throws Exception { 

    Rectangle rec = new StatusRect().makeRectangleStatus(50, 700, 20, 20, "red", "black", "btnObj1", 7, 0);

}

StatusRect class:

    public class StatusRect {

        private String ColorStatusOn;
        private String ColorStatusOff;
        private int IntFlag;
        Rectangle rec = new Rectangle(); 


        public Rectangle makeRectangleStatus (double x, double y, double Witdh, double Height, String ColorStatOn, String ColorStatOff, String BtnId, int SetIntStatus, int Current){
            rec.setLayoutX(x);
            rec.setLayoutY(y);
            rec.setWidth(Witdh);
            rec.setHeight(Height);                   
            ColorStatusOn = ColorStatOn;
            return rec;
        }  

        public void changeIntFlag(int iEnabled) {
            if(IntFlag == iEnabled) return;
            IntFlag = iEnabled;
            System.out.println("VALUE CHANGED!!!: " + IntFlag);
            if (IntFlag == 7){

                //this is being triggerd every time the int Flag value becomes "7"
                System.out.println("SAME NUMBER: SET COLOR RECTANGLE TO red"); 

                //Why doesnt the color change here?? 
                rec.setStyle("-fx-fill:" + ColorStatusOn); 
            }                 
        }
    }
}

You can change your makeRectangleStatus method (and if it is necessary your Rectangle class adding some setters/getters) and set the colour of this.rec directly inside makeRectangleStatus . For instance, if you want that your Rectangle instance goes to ColorStatOn string, try this:

public void makeRectangleStatus (double x, double y, double Witdh, double Height, String ColorStatOn, String ColorStatOff, String BtnId, int SetIntStatus, int Current){
    rec.setLayoutX(x);
    rec.setLayoutY(y);
    rec.setWidth(Witdh);
    rec.setHeight(Height); 
    rec.setColorStatOn(ColorStatOn);
} 

Besides pay attention: you don't need to return anything in makeRectangleStatus since you are using this.rec object.

What I noticed is that all the changes of styles from objects, rectangles buttons,etc work with events. Like action event, move, touch etc. So when an action is true then something is changed. Just making a set method and setting a value doesnt do anything. You have to evaluate the value and add for example a changeproperty listener to it. Like with a slider, when the value of the property of the slider changes, and binding it. Objects are created only once. I tried to do this like below. I understood that the method called "changed" is invoked when the value of the property changes, but sadly that doesnt work.
I think this is the way it should work, but Iam no expert.

IntegerProperty currentvalue = new SimpleIntegerProperty(IntFlag);
currentvalue.addListener(new ChangeListener<Number>(){
@Override
public void changed(ObservableValue <? extends Number>
observableValue, Number oldValue, Number newValue){
System.out.println("CHANGED, LISTENER TRIGGERD!!!!" +newValue);
}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM