-2

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 :)

user3077551
  • 69
  • 2
  • 9
  • 2
    Check if this gives you some idea: http://stackoverflow.com/questions/9803658/delayed-text-color-change-in-swing-text-field – Rahul Tripathi Apr 19 '15 at 13:33
  • 2
    Your question is lacking one important feature -- an example of your attempt to solve the problem. In the future, when you run into similar problems you'll want to show your best good faith attempt to solve this, then tell us what problems you are having with your code. This will A) give us a much better understanding of what you're trying to do and prevent all this trouble extracting exact information from you, and B) give us a much better understanding of what you might be doing wrong, what you need help with. – Hovercraft Full Of Eels Apr 19 '15 at 13:36

3 Answers3

3

First of all, you wouldn't hard-code your color use like this:

g.setColor(colors.ORANGE);
g.fillOval(160, 70, 50, 50);

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:

private Color ovalColor = SOME_DEFAULT_COLOR; // some starting color

And then use that color to draw with:

g.setColor(ovalColor);
g.fillOval(160, 70, 50, 50);

I'd then give my class an array of Color or ArrayList<Color> and an int index field:

private static final Color[] COLORS = {Color.black, Color.blue, Color.red, 
       Color.orange, Color.cyan};
private int index = 0;
private Color ovalColor = COLORS[index]; // one way to set starting value

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();

index++;
index %= COLORS.length;
ovalColor = COLORS[index];
repaint();

Also here's a somewhat similar example.
Also please look at the Swing Timer Tutorial.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

maybe this would help:

public class object{
Color color = Color.GREEN;
Timer timer;
public object() {

timer = null;
timer = new Timer(5000, new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        if (color.equals(Color.GREEN)) {
            color = Color.RED;
            timer.setDelay(2000);
        } else {
            color = Color.GREEN;
            timer.setDelay(8000);
        }
        repaint();
    }
});
timer.start();}}
programmer21
  • 173
  • 11
-1

I think a paint method would work.like this:

public void paint(Graphics g){
super.paint(g);

g.setColor(Color.green);
g.filloval(30,40,50,50);
 }
programmer21
  • 173
  • 11
  • this should be right after object costroctor in class object. – programmer21 Apr 19 '15 at 14:30
  • yes add paint.repaint() so the new components are redrawn over the previous ones. – programmer21 Apr 19 '15 at 14:39
  • Please don't recommend this -- a terrible recommendation. You're overriding the wrong method -- he should override `paintComponent` not paint, and you're hard-coding colors, something I've already recommended he not do. And recommending `paint.repaint()`? WTF? That makes no sense at all. – Hovercraft Full Of Eels Apr 19 '15 at 14:48
  • your right. paintComponent is truly a better recommendation. so whats your suggestion for repaint error? – programmer21 Apr 19 '15 at 14:56
  • My suggestion is find out why it's happening. @user3077551, the original poster (OP), has not shown us his code or his attempt (as per the comment to his question) which prevents us from knowing what he's doing wrong. For all we know his class is probably not extending JPanel, and so calling `repaint()` is meaningless. He has to improve his question. Also what is with your recommendation that he call `paint.repaint();`? How will that work? That's not even valid Java. – Hovercraft Full Of Eels Apr 19 '15 at 15:16