I'm developing a new Look and Feel that is based on animation ,
so I've decided to make animated text caret too !
The caret works as expected every thing is fine except that am worried about how I defined my animation rules in making the babySteps method and repaint in the movementTimer not the caret one but the component one !!
at the installation of the Caret I add PropertyChangeListener witch invokes prepareColorsList if the property is "caretColor" with the new value
private void prepareColorsList(Color c) {
Color end = reFactorAlphaBy(c, 0);
ar.clear();
ar.add(c);
while (!ColorsEquals(c, end)) {
ar.add((c = step(c, end)));
}
}
ar is a List<Color> and reFactorAlphaBy just returns a new color with same red,green and blue but with the specified int as alpha .
the stepColor just call the babySteps method on each value of the color and return the new color .
I did this because I didn't want to invoke the step method in the timer , am afraid this will be bad practice.
The stepsColor(int from, int to, int max) provide the maximum addition to add it to from to be to but the return will be less than max
public static int babySteps(int from, int to, int max) {
int ret = 0;
if (from > to) {
for (int j = 0; j <= max; j++) {
if (from >= to + j) {
ret = -j;
} else {
break;
}
}
} else if (from < to) {
for (int j = 0; j <= max; j++) {
if (from + j <= to) {
ret = j;
} else {
break;
}
}
}
return ret;
}
I'm afraid of the usage of the for loops (I haven't faced a performance issue with them in any project yet but if this project made it to the light it gonna have a lot of users and i need to predict if it's gonna be issue).
Last thing, it's also about the babySteps as am using it in the movement timer too.
This is a brief explanation of what I am doing:
- I have two
Rectangleobjects: one to draw and the other to get the location of where that drawing rect should go . - I have overridden the
setDotmethod to change the second rect bounds . - The movement timer has an
ActionListenerthat moves the bounds of the first rect to the other one using thebabyStepsmethod
@Override
public void actionPerformed(ActionEvent e) {
draw.height += babySteps(draw.height, follow.height, 2);
draw.width += babySteps(draw.width, follow.width, 2);
draw.x += babySteps(draw.x, follow.x, 2);
draw.y += babySteps(draw.y, follow.y, 2);
getComponent().repaint();
}
Here I am calling the component repaint instead of the class one because the class repaint implementation will repaint the caret rect only witch it will lead in the old carets stick to the screen !!
Am just afraid of the for loops that is inside Timer objects would make an heavy effect to ui and making crashes if the user have a lot of JTextComponent and I don't know even how to predict what will happen.