Small Java Applet freezing web browser

R

raider.adam

I am curious if anyone can help point out what I am doing wrong. I
assume I am not clearing a resource correctly which is causing the
hangup. I am just experimenting with applets right now and decided to
make one that does projectile motion for practice.

About 9 out of 10 times the applet runs, I have to force quit the
browser to get it to close (jsp server is linux tomcat, desktop is
windows XP IE and mozilla).

I appreciate the help.

package applets;

import java.awt.*;
import java.applet.*;

public class ProjectileAnimation extends Applet {
double Xo = 0.0;
double Yo = 0.0;
double Vo = 0.0;
double x = 0.0;
double y = 0.0;
double theta = 0.0;
int t = 0;

public void init() {
setBackground(Color.white);
}

public void start() {

}

public void paint( Graphics g ) {
Vo = Double.parseDouble(getParameter("Vo"));
theta = Double.parseDouble(getParameter("theta"));
g.setColor(Color.black);
g.drawLine(0,0,0,410);
g.drawLine(0,409,710,409);
while ((int)Math.round(y) >= 0)
{
x = Vo * Math.cos(Math.toRadians(theta)) * t;
y = .5 * -9.8 * Math.pow(t, 2) + Vo * t + Yo;
y = 500 - y;
g.fillOval((int)Math.round(x), (int)Math.round(y), 5,
5);
t++;

try
{
Thread.sleep(1000);
Thread.sleep(0);
} catch (InterruptedException e)
{
}
}
}

public void update ( Graphics g ) {

paint(g);

}
}
 
O

Oliver Wong

I am curious if anyone can help point out what I am doing wrong. I
assume I am not clearing a resource correctly which is causing the
hangup. I am just experimenting with applets right now and decided to
make one that does projectile motion for practice.

About 9 out of 10 times the applet runs, I have to force quit the
browser to get it to close (jsp server is linux tomcat, desktop is
windows XP IE and mozilla).

I appreciate the help.

package applets;

import java.awt.*;
import java.applet.*;

public class ProjectileAnimation extends Applet {
double Xo = 0.0;
double Yo = 0.0;
double Vo = 0.0;
double x = 0.0;
double y = 0.0;
double theta = 0.0;
int t = 0;

public void init() {
setBackground(Color.white);
}

public void start() {

}

public void paint( Graphics g ) {
Vo = Double.parseDouble(getParameter("Vo"));
theta = Double.parseDouble(getParameter("theta"));
g.setColor(Color.black);
g.drawLine(0,0,0,410);
g.drawLine(0,409,710,409);
while ((int)Math.round(y) >= 0)
{
x = Vo * Math.cos(Math.toRadians(theta)) * t;
y = .5 * -9.8 * Math.pow(t, 2) + Vo * t + Yo;
y = 500 - y;
g.fillOval((int)Math.round(x), (int)Math.round(y), 5,
5);
t++;

try
{
Thread.sleep(1000);
Thread.sleep(0);
} catch (InterruptedException e)
{
}
}
}

The way to do animations is to draw a single frame, and then exit the
paint method. Then, the next time you enter the paint method, draw the next
frame, and so on. Instead, you're staying in the paint method for the entire
duration of the animation, paint all the frames, and then exiting the
method.

- Oliver
 
R

raider.adam

Oliver said:
The way to do animations is to draw a single frame, and then exit the
paint method. Then, the next time you enter the paint method, draw the next
frame, and so on. Instead, you're staying in the paint method for the entire
duration of the animation, paint all the frames, and then exiting the
method.

- Oliver

That was it, thanks. The following code works like a charm.

package applets;

import java.awt.*;
import java.applet.*;

public class ProjectileAnimation extends Applet implements Runnable
{
double Xo = 0.0;
double Yo = 0.0;
double Vo = 0.0;
double x = 0.0;
double y = 0.0;
double theta = 0.0;
double t = 0.0;
Thread animator;

public void init() {
setBackground(Color.white);
}

public void start() {
Vo = Double.parseDouble(getParameter("Vo"));
theta = Double.parseDouble(getParameter("theta"));

animator = new Thread(this);
animator.start();
}

public void run() {
while ((Thread.currentThread() == animator) &&
((int)Math.round(y) >= 0)) {
// Display the next frame of animation.
repaint();

// Delay for a while
try {
Thread.sleep(500);
} catch (InterruptedException e) {
break;
}
}
}

public void paint( Graphics g )
{
g.drawLine(0,0,0,410);
g.drawLine(0,409,710,409);
g.setColor(Color.black);

x = Vo * Math.cos(Math.toRadians(theta)) * t;
y = .5 * -9.8 * Math.pow(t, 2) + Vo * t + Yo;
y = 500 - y;
g.fillOval((int)Math.round(x), (int)Math.round(y), 5,
5);
t += .5;
}

public void update ( Graphics g ) {

paint(g);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top