Acmlm's Board - I2 Archive - Programming - Forcing paint event in Java applets
User | Post |
Squash Monster
Posts: 490/677 |
Well, I just tried your method, and another where Stepper takes a reference to the main class and tries to run its paint method. However, neither worked. Paint needs a Graphics class as an argument, so what I tried to do was to store a reference to the last one that was used for calling it. I'm thinking that the only Graphics class that works is the one automatically provided to the applet.
This is starting to look like more effort than it's worth. All I'm using the applet for is getting a Graphics context for painting, and starting the program. Could anyone give me a crash course in a better way to set everything up? |
neotransotaku
Posts: 1918/4016 |
yes, that is right, you need to use a try-catch block. Not unless you have your main function throw Exception, then you do not need try-catch anywhere--but that is bad programming style (which I'm a victim of since I did that with my Graphics Engine ) |
HyperLamer
Posts: 2568/8210 |
Surprisingly, I actually know what that means. But don't you have to use a Try/Catch statement when you start a new thread? |
neotransotaku
Posts: 1903/4016 |
(1) have your class implement Runnable interface (2) move all your drawing code to void run() { while(true) { // paint code } -- replacing the comment with your paint code (3) insert the following code into main: (new Thread(this)).start();
basically, what I'm having you do is move all your painting code into a function that will run on its own thread. thus, giving you the refresh you want. i'm not sure if there is a better way to do with with AWT but if you were using Swing, it would be a little easier. To switch to Swing, you need to extend JPanel and override its paintComponent() method
|
Squash Monster
Posts: 489/677 |
I'm building it on java.awt. and java.applet, I think. The book I'm learning from glosses over and doesn't explain anything on the topics . |
neotransotaku
Posts: 1899/4016 |
what class are you using as your drawing surface? If you use Swing to build your applet, a simple call to repaint() should be enough... |
Squash Monster
Posts: 487/677 |
So I'm coding this Java applet type dealy, a game. I've got all the main code in a class, called Stepper, which has an object generated by the main class (the one with paint and init). The Stepper object does event handling for keyboard input and a timer that the main class sets up.
Now, whenever the Stepper event fires from the timer, it does a good deal of stuff, and thus I should redraw the screen. Unfortunately, I don't know how to come back to the main class and call the paint event or force it to paint or anything good like that, and this is a problem, becouse the screen needs to be redrawn when the game is being played.
Anyone know how to deal with this? Do I need to try to explain this in a different way? |
|