• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Event driven programming

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone give me a really good example of event driven program(ming)? I have tried to find videos and tutorials about it but they don't explain it in a way that i could understand.
Here is what i understand:
1) Event trigger - it triggers a event on witch the program will react
2) Event handler - the reaction of program to event

I know keyboard events and mouse event etc. But it seems that there is more to it than just keyboard and mouse events.

Is it something like so:
1) make one game step/tick
2) handle keyboard and mouse
3) handle environment events(triggered by location, time... whatever)
??

And how do i trigger the events? And where do i handle events/change states?
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Event driven programming is a paradigm where your execution path dies out. Yes, that means as far as things you are doing, everything in your program has come to completion. So when that happens you have to have entry points into your program that are driven by system or user defined events; such as, keyboard action, mouse action, or a timer triggering.

Started out in the old days when there wasn't "event driven" programming and your program never relinquished control, it polled the devices it wanted to use for input, and was basically in a continual loop until you decided to exit the program. Today in programming languages like your execution paths complete or your program is hopelessly crippled by lack of resources to be able to process the events that are triggered--all the resources are being directed in your loop and not having the time to process the events, they are ignored.

So you have to quit thinking linearly: a keyboard event can happen at any time, a timer event will happen on a scheduled interval, a mouse event will happen when the mouse moves; can you guarantee when that will be and in what order? No, you cannot. Further more: you should not.

I have a small example I will post later, when I get over to my other computer to post it, but when I came to the point that I realized those things I just put forth to you, it was an astounding epiphany in my knowledge and career development. Think about them for a while and let it really sink in--your execution paths end. You have to have entry points for the events to be processed, but you do not control the order the events happen.
 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it would be something like this:

while(true){
while(userevents.notEmpty()) handleUserEvent();
move user
move ai
enviroment events
some other stuff needed to be done(rendering or whatever)
}
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NO! That is actually not a good scenario in event driven programming. Remember you code execution paths NEED to complete, in a closed infinite loop, while(true), you are always in a code execution path that you control--it may cause that you do not have resources to process events.

Marko Taht wrote:So it would be something like this:

while(true){
while(userevents.notEmpty()) handleUserEvent();
move user
move ai
enviroment events
some other stuff needed to be done(rendering or whatever)
}

 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marko,

I have posted this code before and it is heavily commented, please read the comments to see what is happening.


This code uses combinations of events to make the animation happen.

1 - the timer event checks the variable instance variable "key" and sets the movement appropriately.
2 - repaint is called to request the repaint action take place (when resources are available)
3 - if the appropriate key was pressed then the instance variable "key" is set appropriately, and when released "key" will be reset.

Note: because "key" is set by a keyboard event, keyPressed, and only reset when the event keyReleased is processed every timer event will cause a simulated key repeat to happen as the variable "key" will stay set until the keyRelease event handler resets it. Also take note that the execution paths end and the program is in a "waiting" or "dead" state as it has no way to generate an event on it's own and all execution paths have completed. The only thing driving the program is the key events and timer event. (And the request to repaint at the end of the action listerer)
 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So by adding stuff to actionperformed i can control AI and environment too, right? While everything does happen with events, it still seems to me that at some deeper lower level it boils down to a while loop, because something needs to trigger the timer event and that means something needs to keep on checking the timer.
 
Sheriff
Posts: 28362
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marko Taht wrote:While everything does happen with events, it still seems to me that at some deeper lower level it boils down to a while loop, because something needs to trigger the timer event and that means something needs to keep on checking the timer.



Yes, that's right. Although it's probably more complicated than a simple while-loop. In Swing, what you described there is called the Event Dispatch Thread. Somehow it monitors for a variety of events and dispatches calls to code which has registered to listen for those events.
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marko,

The thing that you have to remember is that the OS stays active. Look were the Timer is declared, then look down the execution path, the timer has to be started. There is your tie into the OS through the Java VM. It takes care of the when to fire the event and just how to trigger it.

Other than that, NO--there is not a while loop someplace as far as you program is concerned, "magic" happens and an event is thrown. That is all you have to consider: an event happens and you have to have an entry point for it, if you want to capitalize on the event. Give up the idea of controlling the event, you do NOT in event driven programs--you make entry points to process events as they are thrown. Don't try to control that, just plan for them to happen.

Have you ever seen the movie: "The Field of Dreams"? Same: build it and they will come.

Les

Marko Taht wrote:So by adding stuff to actionperformed i can control AI and environment too, right? While everything does happen with events, it still seems to me that at some deeper lower level it boils down to a while loop, because something needs to trigger the timer event and that means something needs to keep on checking the timer.

 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct, but watch out thinking like that; in the example given you have 25 ms to do all processing before the next event fires and during that time a keyboard event may be fired off and it has to be processed or nothing is going to work right--the shape will just keep running in the same direction. Think as a minimalist--what do I have to do to allow processing to happen, then complete--just a code segment, that is all that is needed and link it into the appropriate event handler.

Also please take not that the Timer is a Swing timer and not the AWT timer, that is significant, as all SWING timers process on the same thread, causing a lot less overhead.

If you find yourself wanting to add a while loop, for loop, or do recursion; you are probably thinking too linearly controlling again and you should stop and look at the problem and start thinking is code segments for processing and not control.

Marko Taht wrote:So by adding stuff to actionperformed i can control AI and environment too, right?

 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do not have real time animation, continuously moving objects, then you don't need a timer: key off some other event. In a checkers game, you would key off of the mouse event for the drag or possibly the click, then you have time to do pretty much what ever you want to do and can even put up a dialog saying that the AI is thinking.

But if you are in a real time scenario, then you need to multithread. Use the Swing workers to get out of the event dispatch thread so your game can continue processing needed events. You also have to be especially careful here so that your AI will not work too long and if you go over timer boundaries, you have to make the action appropriate for the elapsed time.

example: 30 ships are firing on your base, the AI is trying to decide which to shoot, but for some reason the decision went across a movement cycle, so it fires and nothing is there to hit. Or perhaps your other NPCs decide to step away from the bar, and are out the door, and your bartender says: "What will it be?"

Marko Taht wrote: ... i can control AI ...

 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i think im starting to understand the concept. Since AI and environment stuff take time to process and there is a possibility for a event to happen at any given time, would it be logical to try and maybe make multiple eventhandler? Like one for player to handle player realted events, like moving and stuff. One for Environment and AI and one for drawing out whatever is currently done. I think this is starting to sound like multi threading .

Im just trying to find a good technique to progam a game. So my first steps are designing the basic game consept , and researching about different programming methods and stuff. While not a question for this topic but could you point me to a good site or tutorial that could help me to laydown some basic hiearchi in the game code?
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marko,

I can see where the dawn is peaking over the horizon, but you still have a lot of sorting out to do: event handlers are linked into your GUI or other physical program objects. They are not interrupts, but are OS/Java level generated actions that you have to register a handler to take advantage.

For instance, in the example, the Timer is created. At the time of creation we tell Java the method that object that is going to handle the event, and that object has to implement ActionListener. Once the Timer is created, then it has to be started. Once it is created and started, then ActionEvents will be handled by your ActionListener.

The same type of creation and registration goes on for all of the event handlers you want to use, and notably these event handlers are bound, by implementation or assignment, to objects which then listen to the event.

What you are speaking of in your post where you have multiple "event handlers" for different players, if I understand your comment correctly, is more of a processing control structure where you might have something like:

1 - take inventory of surroundings
2 - take note of any alerts from #1
3 - if no actions from #1 do something random from appropriate list

The processing of those things may be controlled by a Timer, or other event, but they in themselves are not event handlers. Also please take note--you can run multiple timers off the Swing Timer and not significantly increase overhead, in that case you would need to have multiple event handlers because you would have multiple timer events--or you could make your object do actions on "tick" counts--each timer event would represent a "tick" and each object would have a threshold before processing.

Also in Object Oriented Programming, you should create objects that are aware, thus cutting down on any type of external control structure significantly or doing away with it all together, in other words, your objects should know how they should interact with their environment, either other, and any NPC's they may encounter.

Marko Taht wrote: ... make multiple eventhandler?

 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marko,

I always recommend people start in these places: Java Tutorials and The Java Really Big Index. Once you understand what is there, then you can apply almost anything you read and adapt it to Java and your specific need.

Les

Marko Taht wrote: So my first steps are designing the basic game consept , and researching about different programming methods and stuff.

 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can program java, i have done some pretty big stuff in it. I just havent dealt with game programming so mutch, so im trying to find a good concept on programming game.

Les Morgan wrote:Marko,

I always recommend people start in these places: Java Tutorials and The Java Really Big Index. Once you understand what is there, then you can apply almost anything you read and adapt it to Java and your specific need.

 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marko,
Much of my game programming knowledge had come from Assembler and C. I have used the tutorials and a few books for the era to help translate that knowledge to Java implementations, but in reality, the tutorials that I gave you do talk about animation, graphics, beans, and object oriented concepts; all of which you will need to be more than familiar with to develop games in Java. From your comments, you do not understand event driven programming, and you are going to need a tutorial on that. Unless you are also VERY familiar with Java 2D and 3D graphics you are going to need a tutorial on those too. So I really do suggest you grab those tutorials and take a look at them and supplement your Java knowledge and skill to include all of the basics in game development before you grab one of the books on Java Game programming.
Les

Marko Taht wrote:I can program java, i have done some pretty big stuff in it. I just havent dealt with game programming so mutch, so im trying to find a good concept on programming game.

 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My main confusion about event driven programming currently is, how can i handle events and movement and other stuff that are not connected to player, like environment. The main event handler handler render events while no player event has happened or handles event that player has caused. But how and where do i handle the environment events and AI events, so that it wont interfere with player event handling. This is the point i'm not so sure about.

About graphics, i have used SDL in C++ and OpenGl in Java, but i always have a main loop that handles everything or i have some thread system that substitutes main loop. So i though maybe learn something new and try event driven programming, but the tutorials about it are not very well made. Your code example and the things you have explained have been much more helpful than a dozen of tutorials.
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marko Taht wrote:My main confusion about event driven programming currently is, how can i handle events and movement and other stuff that are not connected to player, like environment. The main event handler handler render events while no player event has happened or handles event that player has caused. But how and where do i handle the environment events and AI events, so that it wont interfere with player event handling. This is the point i'm not so sure about.


Marko,

For basic game overhead you have to make a control routine that is called by the action listener driven by the timer. You have to realize that actions needed may happen on different time intervals than a single timer tick, so you can make multiple timers using the Swing Timer, or, my preference, is to make the Timer fire off more frequently than needed and use a "tick" based approach for having all of these type of actions happen. So, for instance, take Runescape and ore regeneration--each ore regenerates on a different schedule, so each timer tick a counter is incremented in the object until the ore object see's that it is supposed to regenerate.

Also you cannot keep one event from interfering with the handling of another event, you could be in process from a timer event, and somebody does keyboard activity, it happens, so don't freak out and lose too much sleep over that, your execution path is restored and the EDT when you get a return from the current processing execution path. The main thing you have to deal with is--DO NOT BLOCK THE EVENT DISPATCH THREAD (EDT), keep processing segments as short as possible, make aware game objects (each object in the game should have enough "intelligence" to process for itself when it is appropriate to do so), use Swing worker threads to process so you do not block the EDT, and if you really have to do so you can turn off the Timer and restart it as you need (not in a real time animation game) and you can turn off handling of user events also by making a flagged exit before the processing happens (if(myBoolean) return; or my preference if(!myBoolean){and processing is done in this block}).

Marko Taht wrote:About graphics, i have used SDL in C++ and OpenGl in Java, but i always have a main loop that handles everything or i have some thread system that substitutes main loop. So i though maybe learn something new and try event driven programming, but the tutorials about it are not very well made. Your code example and the things you have explained have been much more helpful than a dozen of tutorials.


All that is in the code is a basic animation loop, and that is covered in the Java tutorials I listed--as a matter of fact, that is where I learned it.

Les
 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. So basically i have a EDT that handles events and sends it to workers who deal with the events. EDT itself just gets the event and forwards it. But to make it so that the game speed wouldnt be directly connected to the FPS i should make another thread that only render the image to screen. This looks quite a lot like a multi threading only that some threads work only when they get the signal to do something.
 
Bartender
Posts: 5584
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Marko,

In general, rendering to the screen is a thing that should be done on the EDT. But how to get that what needs to be rendered, that is another story.

Using separate threads, getting game speed independent of FPS (or even machine speed), there are many possibilities. So, to make it a bit more concrete: do you have any specific game in mind that we could discuss?
 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well im kinda going for a overkill atm. 2D top view game. Maybe some stealth and stuff, AI patrols, maybe some moving items.

All of this can be achieved with the most basic game loop. If rendering is done on EDT, wouldnt the FPS be whatever same as the Timer event tick?
 
Piet Souris
Bartender
Posts: 5584
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, there need not be any connection between game speed and FPS.

For instance, if you want to move an object from the left side of th screen to the right side, then you could either determine how much seconds that will take (determining the delta movement per frame), or the number of frames it will take (determining the delt per time unit).

But the most important aspect is: since the drawing is done on the EDT (assuming you are drawing within some JPanel or the like), the drawing must execute as fast as possible, otherwise you get a sluggish GUI.

Now, if the update of the game status involves relatively little work, and the redrawing is fast enough, then it can be done using a Swing Timer, within the action listener specified in it.

If the updating is much more work, and/or redrawing is time consuming, then the use of separate threads should be considered. A possible strategy is: create two BufferedImages, one for displaying, one for updating. This routine can be made to run at some fixed time interval, or taking whatever time it needs. A Swing Timer fires at the specified times, say, 50 times per second, and a 'paintComponent' method could then simply draw the Display BufferedImage. In this simple way your game speed is independent of the FPS.

As said, there are other possibilities, but what is the best or the simplest one to use, depends very much on the game itself. For instance: in a chess program, the emphasis is very much towards the calculation and not so much on the rendering, but when a mov is done, you might set a Timer that drives the animation.
 
Paul Clapham
Sheriff
Posts: 28362
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marko, I will let the others carry on talking about game development because they know what they're talking about. And they are taking you in the right direction. In the beginning you asked about "event driven programming" but it soon became clear that you were actually interested in developing a game. Sure, responding to events is part of what a game has to do but it's not the whole thing, and what this thread demonstrates is that looking at the problem from the top down (writing games) is a far more productive approach than looking at it from the bottom up (responding to events). It's a common thing we see here, people asking about how they want to do something rather than what they want to do... anyway, enough rambling from me. Carry on everybody.
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marko,
it is multitasking, not necessarily multithreading, and nothing happens until it gets a signal to do something. Remember ALL execution paths come to an end and are only active after an event is thrown and a handler processes it.
Les

Marko Taht wrote:This looks quite a lot like a multi threading only that some threads work only when they get the signal to do something.

 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marko,
It can be, but does not have to be: remember the timer event is just another event and has a handler to do what needs to be done; that could be some counter to keep track of ticks, so multiples ticks can be had. In the example code I posted we have 1000/FPS which will give the Timer delay. Let's say for convenience sake we change the FPS from 40, 25ms delay, to 50 for we will have a 20ms delay. Now suppose we want a base timer tick as 10ms, we could then send ticks out to all our objects on a 10ms basis and have each object calculate what action it should take on the basis of counter in the object. That would put our refresh at 100 FPS if we refreshed on the 10ms tick, so instead we need something to control our refresh request so we only refresh on a 20ms basis. Simple for what we have, we will either refresh or not, so we have a Boolean condition that we can use a Boolean flag to process.

This code can be placed in your panel object that you use for display, and thereby encapsulate your display object even more, so your display object is one of the many other objects that are doing tick based processing.
Les

Marko Taht wrote:If rendering is done on EDT, wouldnt the FPS be whatever same as the Timer event tick?

 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So... If i set my base timer to tick with 10ms interval. I can tell render panel to paint image every X ticks or render when something is complete and i can calculate the FPS rate on how many times repaint is called. Did i understand it correctly?

Secondly the operator =! look weird, i haven't seen one like this before. Inequality operator is != but what is =!?
 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote: Sure, responding to events is part of what a game has to do but it's not the whole thing, and what this thread demonstrates is that looking at the problem from the top down (writing games) is a far more productive approach than looking at it from the bottom up (responding to events).



Well while my final goal is making a game. I have made games before so, i can make my game using the methods i have done before, using a while loop to do everything and checking the event stack. But i want learn a new way of thinking for game making. And i have heard that event driven programming might be a good way to do so. And since it seems that the EDT replaces what i have done before i need to understand the event driven concept before i can do anything else.
 
Piet Souris
Bartender
Posts: 5584
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well,

Les has given many explanations and a working example, I've described some strategies, but Paul is right: let us focus on something concrete.

You wrote that you have programmed games before. So pick one of these programs, describe what it does, and let us discuss what the possibilities are to run that game in Java. I suggest not to pick the most complex one!
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you understood that correctly. Check your desired FPS and set the display's counter accordingly for your tick rate.

Marko Taht wrote:So... If i set my base timer to tick with 10ms interval. I can tell render panel to paint image every X ticks or render when something is complete and i can calculate the FPS rate on how many times repaint is called. Did i understand it correctly?


Marko,
Yes, after I posted that I saw what I had done. I call that, my own jargon, "overloading the comparison". What basically happens is this: We have a Boolean comparison and an assignment operation combined into one statement.

then it sees

and evaluates the right side and makes the appropriate assignment. So effectively it's a flip-flop or a divide by 2 without having to reset.
Les

Marko Taht wrote:Secondly the operator =! look weird, i haven't seen one like this before. Inequality operator is != but what is =!?

 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marko,
I am inclined to agree with the others, time you should start putting some of what you learned into effect. How about making a basic animation loop using the "tick" type of approach? Then put a simple object in it and let it bounce off the walls a bit. Therein is the most basic start of a real time animation type of game.
Les
 
Marko Taht
Greenhorn
Posts: 21
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple small fast code where ball bounces around. Timer ticks 100 times per sec and rendering happens 50 per sec. Used some small bits from your example before. Hope you don't mind.

 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marko,
Not a problem. Now remove the fillOval and add multiple discrete objects to it, say 20 or so, and have each object change color on random ticks. I like to use an ArrayList to be the collection for the objects.
Les

Marko Taht wrote:Simple small fast code where ball bounces around. Timer ticks 100 times per sec and rendering happens 50 per sec. Used some small bits from your example before. Hope you don't mind.

 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Done. And for extra a lot of randomness. Thank you for your patience and time .

 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very cool indeed. Looks like you picked up on things fairly well.

Here are 3 more things you should look at in reference to your game making (probably more, but this is what I can think of off the top of my head):
-- look at the Java Shape Interface API, it can be a big help.
-- KeyBindings can be used instead of the whole KeyListener, I still prefer the KeyListener for how I use the keyboard input.
-- Thread notify/wait.

If there is anything else post a thread or do one of the mooseages.
Les

Marko Taht wrote:Done. And for extra a lot of randomness. Thank you for your patience and time .

 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 more thing. EDT pushes all events to other threads, but rendering is done on EDT, correct?
 
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great example! In the above code, RADIUS should have been named DIAMETER.
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is the basic idea.

Marko Taht wrote:1 more thing. EDT pushes all events to other threads, but rendering is done on EDT, correct?

 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg,
Good catch!
Les

Greg Pata wrote:Great example! In the above code, RADIUS should have been named DIAMETER.

 
It runs on an internal combustion engine. This ad does not:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic