hello i am having trouble trying to understand swing timers. to help me could someone show me a simple flickering animation? i have looked arround on the internet but still dont fully understand how they work. it would be so helpful if someone could give me an example like this:
say if i created a circle:
g.setColor(colors.ORANGE);
g.fillOval(160, 70, 50, 50);
how could i then use a swing timer to change the color from orange to say Gray using a swing timer with a delay?
thank you so much for helping me understand :)
First of all, you wouldn't hard-code your color use like this:
Since this prevents all ability to change the color's state. Instead use a class field to hold the Color used, and call it something like
ovalColor
:And then use that color to draw with:
I'd then give my class an array of Color or
ArrayList<Color>
and an int index field:Then in the Swing Timer's ActionListener I'd increment the index, I'd mod it by the size of the array or of the ArrayList, I'd get the Color indicated by the index and call
repaint()
;Also here's a somewhat similar example.
Also please look at the Swing Timer Tutorial.
maybe this would help:
I think a paint method would work.like this: