I have an applet in class 'A'.
I have another class = B.
Now, I have created a set(...) method in A and this set method will be called by B after instantiating A and all the parameters required to build the applet wll be set.
And after that, i have called the init() method of A using the same A's object.
This is not working. A nullPointerException is present. I am not able to call the applet after setting the parameters of the applet.
Am i doing it the wrong way?
The init() method is called after the object is created. Before this time the applet has no access to anything that makes it an applet. Especially methods like getCodeBase(), getDocumentBase() and getParameter(String) are not available until init() has been called.
Can you perhaps show us the exception, and the part of code where the exception is thrown?
Obviously the reference you're using on line 21 is null. That reference is b. So we trace back, and notice that it's bounds - a variable that isn't initialized until init() is called, which happens after you call set and create the balls. However, that will still give you a NullPointerException since Component.createImage, which you are calling on line 80 will return null if the component is not displayable. Your applet isn't like this because it's not contained in a web page, not shown in appletviewer and not shown at all. But I don't see the point in this entire Image though - it will create an off-screen image that you never display.
That whole code is a horrible mess though. I'm sure you've been told before to use a javax.swing.Timer instead of a thread. You are violating the rules specified in Concurrency in Swing. You are calling deprecated methods of Thread that you really shouldn't (stop() and resume()). You are using the results of getGraphics() which you never should.
I am sorry for the trouble but i did read about timers. The thing is I do not have an ActionPerformed in my program.
The nth program i am trying (given below) does work fine but it is not the smooth motion i wanted to generate.
If there is a way to generate smooth motion using timers in this program, do mention.
And do note, i want the ball displayed in the frame to move every 20seconds and then stop for few seconds, say, 4 seconds. This continues till the window is closed.
posted 9 years ago
And i want to display this window when an "OK" button is pressed in another class, which i am not able to do.
posted 9 years ago
So sorry again. I have used timer here, though i dont think the circle is stopping at all!
And the circle is just too fast.
And do tell how to open this window with the press of a button in another class.