I'm stuck with an assignment. It consists of bouncing footballs. Everytime I click on the screen I create a new bouncing football. (soccer). I have four classes: FotBollsPlan which is the footballfield that has an ArrayList consisting of footballs and a timer. FotBoll that draws the football . It actually picks it from reflibrary where I've put them. It has also a timer. FootballMain that extends JFrame. And last UppDatera that updates the footballfield in intervalls, for example 1000 ms, with a timer. And it is here(Uppdatera ) where I have my problem. Here is the code:
So, everything works fine til the last class - uppDatera. I e I'm able to click on the panel and all these footballs appears and they are bouncing around. But as a last issue to do I have to update the footballfield at intervalls and all footballs have to been drawn on the panel at that moment and I have to have a piece of text in the bottom of the frame (panel?, label?) that says how many footballs that are on the field right now. Maybe I don't need this class uppDatera at all. But at line 219 I get an NullPointerException. And I can't understand how to get the size from the ArrayList?
I've been working with the problem today and I believe that I don't need the class uppDatera. So, now the class FotBollsPlan looks like this:
With this I'm quite close to the final code. But now I've got another problem were I'm stuck. It is in the method actionPerformed.
The balls are bouncing around OK and after a while they stop which I want them to. This happends since I stop the timer and setDelay to 3000 ms.(line 47 - 52) But as you can see I restart() the timer again and I thought that the timer should jump back again to 50 ms - so that the balls starts to bounce fast again, but they don't. I've tried different scenarios here with setInitialDelay and so on but nothing works correct. How do I get back to a delay in the timer of 50 ms?
This happends since I stop the timer and setDelay to 3000 ms.(line 47 - 52) But as you can see I restart() the timer again and I thought that the timer should jump back again to 50 ms - so that the balls starts to bounce fast again, but they don't. I've tried different scenarios here with setInitialDelay and so on but nothing works correct. How do I get back to a delay in the timer of 50 ms?
Anders
You set it.
In your code, you set the delay to 50 ms in the constuctor. When you have done 100 loops, you stop the timer t on line 8 in the code snippet above. Then you set the instance variable delay = 3000 on line 9 and use that in line 10 to set the delay on the timer. You then set the loopslot to 0 again and call restart on the timer. This means that the timer now cleans out any pending events and starts with the new value of 3000.
You could have some fun with this class.
Do like this.
Add the interface java.awt.event.MouseWheelListener to your FotbollsPlan class.
Add addMouseWheelListener(this); on line 28, under the mouse listener.
Add this method to the class:
You could comment out the if-loopslot section in actionPerformed if you want to.
What I see is that if I for example add 2 balls to the
footballfield I see that I have 2 + 1 = 3 events going on. And when loopslot = 50
only one event changes to 3000? Sholudn't all events change to 3000? I mean it's
one timer-object that I've changed to 3000. But only one loop gets affected.
Yes, I do... It is doing exactly what you tell it to do.
I have commented in the code.
If we write down, in plain swenglish, what we want to do in the code would look like this:
I always write a "story" in comments in my methods before I start writing the program. That way it is easier to get the right logic. If you can't write it in plain english (eller på svenska för den delen) then it is hard to write it in code. At least initially.
Thank you for putting some effort into my problem. But I'm sorry, this doesn't work out. I don't think that I've explained the issue properly.
At evenly intervalls(jämna mellanrum) the footballfield shall be updated and all the balls shall be painted out with a piece of text that shows how many balls that we have on the field for the moment.
( We don't have to bother about the text now.)
So, for a small period of time , say 3000ms, the balls will stand still and thereafter they will start bouncing again, i e the delay will go back to 50 ms. But they don't do that now. If I set the delay back to 50 ms the program doesn't take the 3000 ms.
And if I read your code it looks fine, but it doesn't work the way I want it to do.
So, the code stays at 3000 ms when it should jump back to 50, why?
For every ball that I create, there will be an event. Maybe I have to change more into the code?
I think the problem is in the way you interpret the logic. When you are in the actionPerformed-method, that is were you call the repainting of the balls. When you set the timer delay to something else, you are only setting the intervals at where you trigger the actionPerformed to be called.
The question now is if you have multiple timers or multiple actionPerformed?
Could you post the complete code as it is now again??
So, I managed to solve it. I googled it - it is kind of a simple answer. But I do not understand why the setDelay method doesn't work. ??
Here is the code:
OK, but I do not understand how to do this, since I just couldn't get this to work with setDelay and the other swing timer functions. Thank you anyway. Or do you have a suggestion how to do it?
Anders
Post by:autobot
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
a bit of art, as a gift, that will fit in a stocking