2 Java Questions

Status
Not open for further replies.
Q.1 Say you create a blank class that extends JApplet called BallApplet.

In BallApplet you create an Instance of a 'Ball'. The constructor for Ball takes a reference to the applet which created it (BallApplet) Is it possible for an Instance of Ball to paint onto the applet. I can only seem to do any painting actually in the void paint(Graphics g) method of the BallApplet class. If I insert this into the Ball.class it doesn't work.

appletThatCreatedBall.getGraphics().drawRect(10,10,10,10);

appletThatCreatedBall being the reference passed to the Ball's constructor.

Any ideas?


Q.2 If you had a loop:

while (go)

go being a boolean set to true. If the while loop were to perform some very long process, I can't quit the loop by having the KeyListener detect a kep press and changed go to false.

Will I need multiple threads? I presume the key press event isn't being fired because the loop is running in the same thread, therefore not terminating the loop.

What is the simplest way to terminate a long loop by user intervention? Obviously I could limit the iterations with a for loop, but I want it to run until the user intervenes.

multiple classes still run on a single thread don't they?


Thanks alot

Dom.
 
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public final class Thread extends JApplet implements KeyListener
{

boolean go = true;

public void init()
{
addKeyListener(this);

while(go)
{
}

}


public void keyTyped(KeyEvent e)
{
go = false;
}
public void keyPressed(KeyEvent e)
{
go = false;
}
public void keyReleased(KeyEvent e)
{
go = false;
}



The while loop never terminates.
 

qball

Ars Tribunus Militum
2,238
Something there looks very wrong. I think you have done exactly as Nathan speculated. You put an infinite loop into the event dispatching thread, so the next event will never be processed because the thread will hang. Whatever you are doing, I suspect you are doing it the wrong way. IIRC, you are supposed to avoid putting long loops in the event dispatching thread because processing them will cause your program to appear sluggish to the user.

-Q
 
You wouldn't want to put an infinite loop in the init() method
anyway. Applets remain little grey boxes until init() runs and
returns, and some browsers (I forget if it's IE or Netscape) will
remain as a little grey box until start() also runs and returns.

Usually one has Applet implement Runnable and have start do something
like so...

code:<HR>
Code:
public void start() {
    new Thread(this).start();
}<P>public void run() {
    // whatever code you want the applet to do
}

~Kevin
:)
 
Thanks alot I understand now.

Is there any way of being notified when the applet has finished it's initialising methods and has free'd up the event thread? Normal command apps and swing apps can run a loop in their main(), why isn't a similar function called in applets after it has initialised?

The Problem
I am trying to create a 'Ball' object which can be created by an applet, the ball then bounces around inside the applet (calculate new position and renering methods INCLUDED in the balls code) I can have the ball calculate it's position but I can't get it to draw itslef on the applet.

because the ball is created in either the 'init()' or 'start()' method of the applet, it can't draw on the applet as it hasn't started fully. I tried running the drawing code in the ball on a timer, this has some weird side effects:


code:<HR>
Code:
appletThatCreatedBall.getGraphics.drawOval(1,1,1,1);

This throws up a null pointer exception at runtime (i presume because applet hasn't fully started) but does actually draw onto the applet, problem is the method .setColor() refuses to work so everything is drawn in black. How can I get the Ball object to draw itself perfectly onto the applet, I need the ball to be informed when the applet has finished loading.


Thanks

Dom.
 
Status
Not open for further replies.