1
\$\begingroup\$

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:

  1. I have two Rectangle objects: one to draw and the other to get the location of where that drawing rect should go .
  2. I have overridden the setDot method to change the second rect bounds .
  3. The movement timer has an ActionListener that moves the bounds of the first rect to the other one using the babySteps method
@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.

\$\endgroup\$
1
\$\begingroup\$

Hey Not 100% sure what's up with the babySteps function

"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"

Is a little confusing anyway I refactored

public static int babySteps(int from, int to, int max) {
    int j = 0;
    int currentPoint = to;
    for (;j <= max; j++) {
        if (!from >= currentPoint || !from <= currentPoint) {
            return j;
        }
    }
}

Can you send me the GitHub link so I can look into the code base thanks

\$\endgroup\$
  • \$\begingroup\$ thank you for the response ;) i started to think that i will have non , any way your code won't compile as a starter friend !! but any how my repo is private , not that i want to make it closed source but i want to at least reach 60% of my millage then make it public but if you want to take a look gimme your github name and i will invite you gladly ;). @AdamSever \$\endgroup\$ – OverLoadedBurden May 1 at 11:04
  • \$\begingroup\$ Whats the compile error? \$\endgroup\$ – Adam Sever May 2 at 22:15
  • \$\begingroup\$ Oh I see it trying to use the var j before i init it \$\endgroup\$ – Adam Sever May 2 at 22:16
  • \$\begingroup\$ Its avacadoadam on github aswell \$\endgroup\$ – Adam Sever May 2 at 22:17
  • \$\begingroup\$ i just invited you check your email. \$\endgroup\$ – OverLoadedBurden May 2 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.